red dot revolution

This commit is contained in:
2025-05-29 13:01:58 +02:00
parent 54d87f99a6
commit 116a07ba27
4 changed files with 178 additions and 73 deletions

View File

@@ -1,96 +1,76 @@
use iced::mouse;
mod polygon_draw;
use iced::Subscription;
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};
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::run("My App", MyApp::update, MyApp::view)
iced::application("My App", MyApp::update, MyApp::view)
.subscription(MyApp::subscription)
.run_with(MyApp::new)
}
#[derive(Debug, Clone)]
enum Message {
ButtonPressedIncrement,
ButtonPressedDecrement,
Tick,
}
#[derive(Default)]
struct MyApp {
counter: f32,
polys: PolygonFrame,
time_last_frame: Instant,
revo_per_sec: f32,
}
impl MyApp {
fn new() -> (Self, Task<Message>) {
(
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.counter += 32.0,
Message::ButtonPressedDecrement => {
if self.counter > 0.0 {
self.counter -= 32.0
}
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<Message> {
column![
text(self.counter),
text(self.polys.radius),
row![
button("Increment").on_press(Message::ButtonPressedIncrement),
button("Decrement").on_press(Message::ButtonPressedDecrement),
],
canvas(Circle {
radius: self.counter
})
.height(500)
.width(500),
canvas(&self.polys).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()]
fn subscription(&self) -> iced::Subscription<Message> {
time::every(Duration::from_millis(1)).map(|_| Message::Tick)
}
}