better setting for revolution

This commit is contained in:
2025-05-29 15:02:00 +02:00
parent 116a07ba27
commit 3a83937ced
2 changed files with 19 additions and 14 deletions

View File

@@ -1,15 +1,14 @@
mod polygon_draw; mod polygon_draw;
use iced::Subscription;
use iced::widget::canvas;
use iced::widget::{Checkbox, button, checkbox, column, row, text};
use iced::{ use iced::{
Task, Task,
time::{self, Duration}, time::{self, Duration},
widget::{button, canvas, column, row, text},
}; };
use polygon_draw::PolygonFrame; use polygon_draw::PolygonFrame;
use std::f32::consts::PI; use std::f32::consts::PI;
use std::time::Instant; use std::time::Instant;
use tokio::time::sleep;
fn main() -> iced::Result { fn main() -> iced::Result {
iced::application("My App", MyApp::update, MyApp::view) iced::application("My App", MyApp::update, MyApp::view)
@@ -27,14 +26,14 @@ enum Message {
struct MyApp { struct MyApp {
polys: PolygonFrame, polys: PolygonFrame,
time_last_frame: Instant, time_last_frame: Instant,
revo_per_sec: f32, revo_per_sec: u32,
} }
impl MyApp { impl MyApp {
fn new() -> (Self, Task<Message>) { fn new() -> (Self, Task<Message>) {
( (
Self { Self {
revo_per_sec: 1.0 / 2.0, revo_per_sec: 1,
time_last_frame: Instant::now(), time_last_frame: Instant::now(),
polys: PolygonFrame { polys: PolygonFrame {
radius: 128.0, radius: 128.0,
@@ -46,22 +45,29 @@ impl MyApp {
} }
fn update(&mut self, message: Message) { fn update(&mut self, message: Message) {
match message { match message {
Message::ButtonPressedIncrement => self.revo_per_sec *= 2.0, Message::ButtonPressedIncrement => self.revo_per_sec += 1,
Message::ButtonPressedDecrement => self.revo_per_sec /= 2.0, Message::ButtonPressedDecrement => {
if self.revo_per_sec > 1 {
self.revo_per_sec -= 1
}
}
Message::Tick => { Message::Tick => {
let time_btw = Instant::now().duration_since(self.time_last_frame); let time_btw = Instant::now().duration_since(self.time_last_frame);
self.polys.teta += self.polys.teta += 2.0
2.0 * PI * self.revo_per_sec * (time_btw.as_millis() as f32 / 1_000.0); * PI
* (1.0 / self.revo_per_sec as f32)
* (time_btw.as_millis() as f32 / 1_000.0);
self.polys.teta %= 2.0 * PI; self.polys.teta %= 2.0 * PI;
println!("Teta : {}", self.polys.teta); //println!("Teta : {}", self.polys.teta);
self.time_last_frame = Instant::now(); self.time_last_frame = Instant::now();
} }
} }
} }
fn view(&self) -> iced::Element<Message> { fn view(&self) -> iced::Element<Message> {
let txt_nb_rev = format!("Revolution per second : {}", self.revo_per_sec);
column![ column![
text(self.polys.radius), text(txt_nb_rev),
row![ row![
button("Increment").on_press(Message::ButtonPressedIncrement), button("Increment").on_press(Message::ButtonPressedIncrement),
button("Decrement").on_press(Message::ButtonPressedDecrement), button("Decrement").on_press(Message::ButtonPressedDecrement),
@@ -71,6 +77,6 @@ impl MyApp {
.into() .into()
} }
fn subscription(&self) -> iced::Subscription<Message> { fn subscription(&self) -> iced::Subscription<Message> {
time::every(Duration::from_millis(1)).map(|_| Message::Tick) time::every(Duration::from_millis(16)).map(|_| Message::Tick)
} }
} }

View File

@@ -6,7 +6,6 @@ use iced::widget::canvas;
use iced::widget::canvas::Stroke; use iced::widget::canvas::Stroke;
use iced::widget::canvas::Style; use iced::widget::canvas::Style;
use iced::{Color, Rectangle, Renderer, Theme}; use iced::{Color, Rectangle, Renderer, Theme};
use std::time::SystemTime;
pub trait RotationExt { pub trait RotationExt {
fn rotate(&mut self, teta: f32) -> Self; fn rotate(&mut self, teta: f32) -> Self;