mod polygon_draw; use iced::Subscription; use iced::widget::canvas; use iced::widget::{Checkbox, button, checkbox, column, row, text}; use iced::{ Task, time::{self, Duration}, }; use polygon_draw::PolygonFrame; use std::f32::consts::PI; use std::time::Instant; use tokio::time::sleep; fn main() -> iced::Result { iced::application("My App", MyApp::update, MyApp::view) .subscription(MyApp::subscription) .run_with(MyApp::new) } #[derive(Debug, Clone)] enum Message { ButtonPressedIncrement, ButtonPressedDecrement, Tick, } struct MyApp { polys: PolygonFrame, time_last_frame: Instant, revo_per_sec: f32, } impl MyApp { fn new() -> (Self, Task) { ( Self { revo_per_sec: 1.0 / 2.0, time_last_frame: Instant::now(), polys: PolygonFrame { radius: 128.0, teta: 0.0, }, }, Task::none(), ) } fn update(&mut self, message: Message) { match message { Message::ButtonPressedIncrement => self.revo_per_sec *= 2.0, Message::ButtonPressedDecrement => self.revo_per_sec /= 2.0, Message::Tick => { let time_btw = Instant::now().duration_since(self.time_last_frame); self.polys.teta += 2.0 * PI * self.revo_per_sec * (time_btw.as_millis() as f32 / 1_000.0); self.polys.teta %= 2.0 * PI; println!("Teta : {}", self.polys.teta); self.time_last_frame = Instant::now(); } } } fn view(&self) -> iced::Element { column![ text(self.polys.radius), row![ button("Increment").on_press(Message::ButtonPressedIncrement), button("Decrement").on_press(Message::ButtonPressedDecrement), ], canvas(&self.polys).height(500).width(500), ] .into() } fn subscription(&self) -> iced::Subscription { time::every(Duration::from_millis(1)).map(|_| Message::Tick) } }