109 lines
3.3 KiB
Plaintext
109 lines
3.3 KiB
Plaintext
mod polygon_draw;
|
|
|
|
use iced::{
|
|
Task, Theme,
|
|
time::{self, Duration},
|
|
widget::{button, canvas, column, row, text},
|
|
};
|
|
use polygon_draw::{Polygon, PolygonFrame};
|
|
|
|
use std::f32::consts::PI;
|
|
use std::io::BufReader;
|
|
use std::time::Instant;
|
|
|
|
fn main() -> iced::Result {
|
|
iced::application("My App", MyApp::update, MyApp::view)
|
|
.theme(|_| Theme::Dark)
|
|
.subscription(MyApp::subscription)
|
|
.run_with(MyApp::new)
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum Message {
|
|
ButtonPressedIncrement,
|
|
ButtonPressedDecrement,
|
|
Tick,
|
|
}
|
|
|
|
struct MyApp {
|
|
polys: PolygonFrame,
|
|
time_last_frame: Instant,
|
|
nb_sec_for_rev: u32,
|
|
}
|
|
|
|
impl MyApp {
|
|
fn new() -> (Self, Task<Message>) {
|
|
(
|
|
Self {
|
|
nb_sec_for_rev: 4,
|
|
time_last_frame: Instant::now(),
|
|
polys: PolygonFrame {
|
|
teta: 0.0,
|
|
polygons: vec![
|
|
//Polygon::n_gon(0.0, 12),
|
|
Polygon::nr_10a_in_42(0.0),
|
|
Polygon::nr_10b_in_42(0.0),
|
|
],
|
|
},
|
|
},
|
|
Task::none(),
|
|
)
|
|
}
|
|
fn update(&mut self, message: Message) {
|
|
match message {
|
|
Message::ButtonPressedIncrement => self.nb_sec_for_rev += 1,
|
|
Message::ButtonPressedDecrement => {
|
|
if self.nb_sec_for_rev > 1 {
|
|
self.nb_sec_for_rev -= 1;
|
|
}
|
|
}
|
|
Message::Tick => {
|
|
let time_btw = Instant::now().duration_since(self.time_last_frame);
|
|
let teta_temp = self.polys.teta;
|
|
self.polys.teta += 2.0
|
|
* PI
|
|
* (1.0 / self.nb_sec_for_rev as f32)
|
|
* (time_btw.as_millis() as f32 / 1_000.0);
|
|
self.polys.teta %= 2.0 * PI;
|
|
//println!("Teta : {}", self.polys.teta);
|
|
if self
|
|
.polys
|
|
.have_point_polygon_btw(teta_temp, self.polys.teta)
|
|
{
|
|
do_sound("assets/tick.mp3");
|
|
}
|
|
self.time_last_frame = Instant::now();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self) -> iced::Element<Message> {
|
|
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
|
|
column![
|
|
text("Polymusic").size(32.0),
|
|
row![
|
|
canvas(&self.polys).height(500).width(500),
|
|
column![
|
|
text(txt_nb_rev),
|
|
row![
|
|
button("Increment").on_press(Message::ButtonPressedIncrement),
|
|
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
|
],
|
|
],
|
|
]
|
|
]
|
|
.into()
|
|
}
|
|
fn subscription(&self) -> iced::Subscription<Message> {
|
|
time::every(Duration::from_millis(16)).map(|_| Message::Tick)
|
|
}
|
|
fn do_sound(file_name: &str) {
|
|
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
|
|
let sink = rodio::Sink::try_new(&handle).unwrap();
|
|
|
|
let file = std::fs::File::open(file_name).unwrap();
|
|
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
|
|
sink.sleep_until_end();
|
|
}
|
|
}
|