diff --git a/assets/ah.ogg b/assets/ah.ogg new file mode 100644 index 0000000..62c5a1e Binary files /dev/null and b/assets/ah.ogg differ diff --git a/src/main.rs b/src/main.rs index f246653..b6bcf45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ mod polygon_draw; use iced::{ - Task, Theme, + Element, Task, Theme, time::{self, Duration}, - widget::{button, canvas, column, row, text}, + widget::{Column, button, canvas, column, pick_list, row, slider, text}, }; use polygon_draw::{Polygon, PolygonFrame}; @@ -27,10 +27,12 @@ enum Message { ButtonPressedIncrement, ButtonPressedDecrement, Tick, + AddPolygon(String), + ChangeTeta(usize, f32), } struct MyApp { - polys: PolygonFrame, + poly_frame: PolygonFrame, time_last_frame: Instant, nb_sec_for_rev: u32, audio_manager: AudioManager, @@ -47,14 +49,12 @@ impl MyApp { nb_sec_for_rev: 4, time_last_frame: Instant::now(), audio_manager: manager, - default_sound: sound_data, - polys: PolygonFrame { + default_sound: sound_data.clone(), + poly_frame: PolygonFrame { teta: 0.0, polygons: vec![ //Polygon::n_gon(0.0, 12), //Polygon::triangle(0.0), - //Polygon::square(PI / 4.0), - Polygon::nr_6_in_30(1.5), ], }, }, @@ -69,39 +69,92 @@ impl MyApp { self.nb_sec_for_rev -= 1; } } + Message::AddPolygon(s) => { + let mut poly: Polygon; + match s.as_str() { + "segment" => { + poly = Polygon::segment(0.0, self.default_sound.clone()); + poly.name = "segment".to_string() + } + "triangle" => { + poly = Polygon::triangle(0.0, self.default_sound.clone()); + poly.name = "triangle".to_string() + } + "square" => { + poly = Polygon::square(0.0, self.default_sound.clone()); + poly.name = "square".to_string() + } + _ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()), + } + self.poly_frame.polygons.push(poly); + } Message::Tick => { let time_btw = Instant::now().duration_since(self.time_last_frame); - let teta_temp = self.polys.teta; - self.polys.teta += 2.0 + let teta_temp = self.poly_frame.teta; + self.poly_frame.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; - if self - .polys - .have_point_polygon_btw(teta_temp, self.polys.teta) - { + self.poly_frame.teta %= 2.0 * PI; + let sound_to_play = self + .poly_frame + .all_sound_to_play_btw(teta_temp, self.poly_frame.teta); + for sound in sound_to_play { self.audio_manager - .play(self.default_sound.clone()) + .play(sound.clone()) .expect("Error to play sound"); } self.time_last_frame = Instant::now(); } + Message::ChangeTeta(i, teta) => self.poly_frame.polygons[i].global_teta = teta, } } fn view(&self) -> iced::Element { let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev); + let mut i = 0; + let polygon_rows: Vec> = self + .poly_frame + .polygons + .iter() + .map(|polygon| { + let current_index = i; + i += 1; // Incrémenter l'index pour le prochain polygone + + row![ + text(&polygon.name), + slider( + 0.0..=2.0 * std::f32::consts::PI, + polygon.global_teta, + move |f| { Message::ChangeTeta(current_index, f) } + ) + .step(2.0 * PI / 42f32) + ] + .into() + }) + .collect(); + + // Utiliser Column::with_children pour ajouter dynamiquement les boutons + let polygon_column = Column::with_children(polygon_rows); column![ text("Polymusic").size(32.0), row![ - canvas(&self.polys).height(500).width(500), + canvas(&self.poly_frame).height(500).width(500), column![ text(txt_nb_rev), row![ button("Increment").on_press(Message::ButtonPressedIncrement), button("Decrement").on_press(Message::ButtonPressedDecrement), ], + text("Polygon options"), + pick_list( + ["segment", "triangle", "square"] + .map(|s| s.to_string()) + .to_vec(), + Some("Chose polygon".to_string()), + |s| { Message::AddPolygon(s) } + ), + polygon_column, ], ] ] diff --git a/src/polygon_draw.rs b/src/polygon_draw.rs index 1e1d19f..8ece829 100644 --- a/src/polygon_draw.rs +++ b/src/polygon_draw.rs @@ -6,6 +6,10 @@ use iced::widget::canvas; use iced::widget::canvas::Stroke; use iced::widget::canvas::Style; use iced::{Color, Rectangle, Renderer, Theme}; +use kira::PlaySoundError; +use kira::{ + AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData, +}; use std::time::Instant; pub trait RotationExt { @@ -26,13 +30,16 @@ pub struct PolygonFrame { } impl PolygonFrame { - pub fn have_point_polygon_btw(&self, before: f32, after: f32) -> bool { + pub fn all_sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> { + let mut all_sound: Vec<&StaticSoundData> = vec![]; for poly in &self.polygons { + all_sound.extend(poly.sound_to_play_btw(before, after)); + /* if poly.have_points_btw(before, after) { return true; - } + }*/ } - false + all_sound } } @@ -103,26 +110,33 @@ impl canvas::Program for PolygonFrame { } } +#[derive(Debug)] pub struct Polygon { pub global_teta: f32, pub points_teta: Vec, + pub sound: StaticSoundData, + pub name: String, } #[warn(dead_code)] impl Polygon { - pub fn have_points_btw(&self, before: f32, after: f32) -> bool { + pub fn sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> { + let mut sound_to_play: Vec<&StaticSoundData> = vec![]; if after < before { - return self.have_points_btw(before, 2.0 * PI) || self.have_points_btw(0.0, after); + sound_to_play = self.sound_to_play_btw(before, 2.0 * PI); + sound_to_play.extend(self.sound_to_play_btw(0.0, after)); + return sound_to_play; } let mut p_g; for p in self.points_teta.clone() { p_g = (p + self.global_teta) % (2.0 * PI); if before <= p_g && p_g <= after { - return true; + sound_to_play.push(&self.sound); } } - false + sound_to_play } - pub fn n_gon(teta: f32, n_side: u8) -> Self { + + pub fn n_gon(teta: f32, n_side: u8, sound: StaticSoundData) -> Self { let mut v: Vec = Vec::with_capacity(n_side as usize); for i in 0..n_side { v.push((i as f32 * 2.0 * PI) / n_side as f32); @@ -130,22 +144,26 @@ impl Polygon { Polygon { global_teta: teta, points_teta: v, + sound: sound, + name: "".to_string(), } } - pub fn segment(teta: f32) -> Self { - Polygon::n_gon(teta, 2) + pub fn segment(teta: f32, sound: StaticSoundData) -> Self { + Polygon::n_gon(teta, 2, sound) } - pub fn triangle(teta: f32) -> Self { - Polygon::n_gon(teta, 3) + pub fn triangle(teta: f32, sound: StaticSoundData) -> Self { + Polygon::n_gon(teta, 3, sound) } - pub fn square(teta: f32) -> Self { - Polygon::n_gon(teta, 4) + pub fn square(teta: f32, sound: StaticSoundData) -> Self { + Polygon::n_gon(teta, 4, sound) } - pub fn nr_6_in_30(teta: f32) -> Self { + pub fn nr_6_in_30(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 2.0 * 5.0 * PI / 30.0, @@ -157,8 +175,10 @@ impl Polygon { ], } } - pub fn nr_7_in_30(teta: f32) -> Self { + pub fn nr_7_in_30(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 0.0, @@ -171,8 +191,10 @@ impl Polygon { ], } } - pub fn nr_8_in_30(teta: f32) -> Self { + pub fn nr_8_in_30(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 2.0 * PI / 30.0, @@ -186,8 +208,10 @@ impl Polygon { ], } } - pub fn nr_9_in_30(teta: f32) -> Self { + pub fn nr_9_in_30(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 0.0, @@ -202,8 +226,10 @@ impl Polygon { ], } } - pub fn nr_8_in_42(teta: f32) -> Self { + pub fn nr_8_in_42(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 2.0 * 3.0 * PI / 42.0, @@ -217,8 +243,10 @@ impl Polygon { ], } } - pub fn nr_9_in_42(teta: f32) -> Self { + pub fn nr_9_in_42(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 0.0, @@ -233,8 +261,10 @@ impl Polygon { ], } } - pub fn nr_10a_in_42(teta: f32) -> Self { + pub fn nr_10a_in_42(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 2.0 * 6.0 * PI / 42.0, @@ -250,8 +280,10 @@ impl Polygon { ], } } - pub fn nr_10b_in_42(teta: f32) -> Self { + pub fn nr_10b_in_42(teta: f32, sound: StaticSoundData) -> Self { Polygon { + sound: sound, + name: "".to_string(), global_teta: teta, points_teta: vec![ 0.0,