first good start
This commit is contained in:
97
src/main.rs
97
src/main.rs
@@ -1,3 +1,96 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use iced::mouse;
|
||||
use iced::widget::canvas;
|
||||
use iced::widget::canvas::Stroke;
|
||||
use iced::widget::canvas::Style;
|
||||
use iced::widget::{button, column, row, text};
|
||||
use iced::{Color, Rectangle, Renderer, Theme};
|
||||
|
||||
fn main() -> iced::Result {
|
||||
iced::run("My App", MyApp::update, MyApp::view)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
ButtonPressedIncrement,
|
||||
ButtonPressedDecrement,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
counter: f32,
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::ButtonPressedIncrement => self.counter += 32.0,
|
||||
Message::ButtonPressedDecrement => {
|
||||
if self.counter > 0.0 {
|
||||
self.counter -= 32.0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<Message> {
|
||||
column![
|
||||
text(self.counter),
|
||||
row![
|
||||
button("Increment").on_press(Message::ButtonPressedIncrement),
|
||||
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
||||
],
|
||||
canvas(Circle {
|
||||
radius: self.counter
|
||||
})
|
||||
.height(500)
|
||||
.width(500),
|
||||
]
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
radius: f32,
|
||||
}
|
||||
|
||||
impl<Message> canvas::Program<Message> for Circle {
|
||||
// No internal state
|
||||
type State = ();
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
_state: &(),
|
||||
renderer: &Renderer,
|
||||
_theme: &Theme,
|
||||
bounds: Rectangle,
|
||||
_cursor: mouse::Cursor,
|
||||
) -> Vec<canvas::Geometry> {
|
||||
// We prepare a new `Frame`
|
||||
//println!("w:{}, h:{}", bounds.size().width, bounds.size().height);
|
||||
let mut frame = canvas::Frame::new(renderer, bounds.size());
|
||||
|
||||
// We create a `Path` representing a simple circle
|
||||
let circle = canvas::Path::circle(frame.center(), self.radius);
|
||||
let line = canvas::Path::line(frame.center(), iced::Point { x: 0.0, y: 0.0 });
|
||||
frame.stroke(
|
||||
&line,
|
||||
Stroke {
|
||||
width: 5.0,
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
// And fill it with some color
|
||||
frame.stroke(
|
||||
&circle,
|
||||
Stroke {
|
||||
width: 5.0,
|
||||
style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)),
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
// Then, we produce the geometry
|
||||
vec![frame.into_geometry()]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user