add ngon and change sound on polygon
This commit is contained in:
153
src/main.rs
153
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::<u8>() {
|
||||
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<Message> {
|
||||
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<Element<Message>> = 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::<Result<Vec<_>, 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<String> = (5..=42).map(|sides| format!("Ngon{}", sides)).collect();
|
||||
let all_options: Vec<String> = [
|
||||
"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,
|
||||
],
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user