diff --git a/src/main.rs b/src/main.rs index 67312de..d635804 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod polygon_draw; +use std::{fs, io}; + use iced::{ Color, Element, Task, Theme, time::{self, Duration}, @@ -30,6 +32,7 @@ enum Message { ChangeTeta(usize, f32), Remove(usize), ChangeColor(usize, String), + ChangeSound(usize, String), } struct MyApp { @@ -72,52 +75,61 @@ impl MyApp { } 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(); + if s.starts_with("Ngon") { + if let Ok(sides) = s.trim_start_matches("Ngon").parse::() { + poly = Polygon::n_gon(0.0, sides, self.default_sound.clone()); + poly.name = format!("Ngon_{}", sides); + } else { + return; } - "Triangle" => { - poly = Polygon::triangle(0.0, self.default_sound.clone()); - poly.name = "Triangle".to_string(); + } else { + 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(); + } + "Nr6In30" => { + poly = Polygon::nr_6_in_30(0.0, self.default_sound.clone()); + poly.name = "Nr6In30".to_string(); + } + "Nr7In30" => { + poly = Polygon::nr_7_in_30(0.0, self.default_sound.clone()); + poly.name = "Nr7In30".to_string(); + } + "Nr8In30" => { + poly = Polygon::nr_8_in_30(0.0, self.default_sound.clone()); + poly.name = "Nr8In30".to_string(); + } + "Nr9In30" => { + poly = Polygon::nr_9_in_30(0.0, self.default_sound.clone()); + poly.name = "Nr9In30".to_string(); + } + "Nr8In42" => { + poly = Polygon::nr_8_in_42(0.0, self.default_sound.clone()); + poly.name = "Nr8In42".to_string(); + } + "Nr9In42" => { + poly = Polygon::nr_9_in_42(0.0, self.default_sound.clone()); + poly.name = "Nr9In42".to_string(); + } + "Nr10aIn42" => { + poly = Polygon::nr_10a_in_42(0.0, self.default_sound.clone()); + poly.name = "Nr10aIn42".to_string(); + } + "Nr10bIn42" => { + poly = Polygon::nr_10b_in_42(0.0, self.default_sound.clone()); + poly.name = "Nr10bIn42".to_string(); + } + _ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()), } - "Square" => { - poly = Polygon::square(0.0, self.default_sound.clone()); - poly.name = "Square".to_string(); - } - "Nr6In30" => { - poly = Polygon::nr_6_in_30(0.0, self.default_sound.clone()); - poly.name = "Nr6In30".to_string(); - } - "Nr7In30" => { - poly = Polygon::nr_7_in_30(0.0, self.default_sound.clone()); - poly.name = "Nr7In30".to_string(); - } - "Nr8In30" => { - poly = Polygon::nr_8_in_30(0.0, self.default_sound.clone()); - poly.name = "Nr8In30".to_string(); - } - "Nr9In30" => { - poly = Polygon::nr_9_in_30(0.0, self.default_sound.clone()); - poly.name = "Nr9In30".to_string(); - } - "Nr8In42" => { - poly = Polygon::nr_8_in_42(0.0, self.default_sound.clone()); - poly.name = "Nr8In42".to_string(); - } - "Nr9In42" => { - poly = Polygon::nr_9_in_42(0.0, self.default_sound.clone()); - poly.name = "Nr9In42".to_string(); - } - "Nr10aIn42" => { - poly = Polygon::nr_10a_in_42(0.0, self.default_sound.clone()); - poly.name = "Nr10aIn42".to_string(); - } - "Nr10bIn42" => { - poly = Polygon::nr_10b_in_42(0.0, self.default_sound.clone()); - poly.name = "Nr10bIn42".to_string(); - } - _ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()), } self.poly_frame.polygons.push(poly); } @@ -157,11 +169,16 @@ impl MyApp { } self.poly_frame.polygons[i].color = c; } + Message::ChangeSound(i, s) => { + self.poly_frame.polygons[i].sound = + StaticSoundData::from_file(s).expect("Fail to load audio") + } } } fn view(&self) -> iced::Element { let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev); + let mut i = 0; //Create all polygon options let polygon_rows: Vec> = self @@ -171,6 +188,11 @@ impl MyApp { .map(|polygon| { let current_index = i; i += 1; + let entries = fs::read_dir("./assets/") + .unwrap() + .map(|res| res.map(|e| e.path().to_str().unwrap().to_string())) + .collect::, io::Error>>() + .unwrap(); row![ text(&polygon.name), @@ -186,10 +208,31 @@ impl MyApp { Some("Color".to_string()), move |s| { Message::ChangeColor(current_index, s) } ), + pick_list(entries, Some("Sound".to_string()), move |s| { + Message::ChangeSound(current_index, s) + }), ] .into() }) .collect(); + let ngon_options: Vec = (5..=42).map(|sides| format!("Ngon{}", sides)).collect(); + let all_options: Vec = [ + "Segment", + "Triangle", + "Square", + "Nr6In30", + "Nr7In30", + "Nr8In30", + "Nr9In30", + "Nr8In42", + "Nr9In42", + "Nr10aIn42", + "Nr10bIn42", + ] + .iter() + .map(|s| s.to_string()) + .chain(ngon_options.into_iter()) + .collect(); let polygon_column = Column::with_children(polygon_rows); column![ @@ -203,25 +246,9 @@ impl MyApp { button("Decrement").on_press(Message::ButtonPressedDecrement), ], text("Polygon options"), - pick_list( - [ - "Segment", - "Triangle", - "Square", - "Nr6In30", - "Nr7In30", - "Nr8In30", - "Nr9In30", - "Nr8In42", - "Nr9In42", - "Nr10aIn42", - "Nr10bIn42" - ] - .map(|s| s.to_string()) - .to_vec(), - Some("Chose polygon".to_string()), - |s| { Message::AddPolygon(s) } - ), + pick_list(all_options, Some("Choose polygon".to_string()), |s| { + Message::AddPolygon(s) + }), polygon_column, ], ] diff --git a/src/polygon_draw.rs b/src/polygon_draw.rs index 256d59b..1564d98 100644 --- a/src/polygon_draw.rs +++ b/src/polygon_draw.rs @@ -6,11 +6,7 @@ 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; +use kira::sound::static_sound::StaticSoundData; pub trait RotationExt { fn rotate(&mut self, teta: f32) -> Self; @@ -55,6 +51,16 @@ impl canvas::Program for PolygonFrame { let radius = frame.size().width / 2.0 - 32.0; let mut vec = Vector::new(0.0, -radius); let c = frame.center(); + // Draw Circle + let circle = canvas::Path::circle(frame.center(), radius); + frame.stroke( + &circle, + Stroke { + width: 6.0, + style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)), + ..Stroke::default() + }, + ); // Draw all polygons by there points for poly in &self.polygons { @@ -77,16 +83,6 @@ impl canvas::Program for PolygonFrame { } } - // Draw Circle - let circle = canvas::Path::circle(frame.center(), radius); - frame.stroke( - &circle, - Stroke { - width: 6.0, - style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)), - ..Stroke::default() - }, - ); // Draw the red dot on the current position let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 5.0); frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));