add ngon and change sound on polygon

This commit is contained in:
2025-06-22 22:52:33 +02:00
parent c45a163f2c
commit 72fe90b901
2 changed files with 101 additions and 78 deletions

View File

@@ -1,5 +1,7 @@
mod polygon_draw; mod polygon_draw;
use std::{fs, io};
use iced::{ use iced::{
Color, Element, Task, Theme, Color, Element, Task, Theme,
time::{self, Duration}, time::{self, Duration},
@@ -30,6 +32,7 @@ enum Message {
ChangeTeta(usize, f32), ChangeTeta(usize, f32),
Remove(usize), Remove(usize),
ChangeColor(usize, String), ChangeColor(usize, String),
ChangeSound(usize, String),
} }
struct MyApp { struct MyApp {
@@ -72,52 +75,61 @@ impl MyApp {
} }
Message::AddPolygon(s) => { Message::AddPolygon(s) => {
let mut poly: Polygon; let mut poly: Polygon;
match s.as_str() { if s.starts_with("Ngon") {
"Segment" => { if let Ok(sides) = s.trim_start_matches("Ngon").parse::<u8>() {
poly = Polygon::segment(0.0, self.default_sound.clone()); poly = Polygon::n_gon(0.0, sides, self.default_sound.clone());
poly.name = "Segment".to_string(); poly.name = format!("Ngon_{}", sides);
} else {
return;
} }
"Triangle" => { } else {
poly = Polygon::triangle(0.0, self.default_sound.clone()); match s.as_str() {
poly.name = "Triangle".to_string(); "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); self.poly_frame.polygons.push(poly);
} }
@@ -157,11 +169,16 @@ impl MyApp {
} }
self.poly_frame.polygons[i].color = c; 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> { fn view(&self) -> iced::Element<Message> {
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev); let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
let mut i = 0; let mut i = 0;
//Create all polygon options //Create all polygon options
let polygon_rows: Vec<Element<Message>> = self let polygon_rows: Vec<Element<Message>> = self
@@ -171,6 +188,11 @@ impl MyApp {
.map(|polygon| { .map(|polygon| {
let current_index = i; let current_index = i;
i += 1; 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![ row![
text(&polygon.name), text(&polygon.name),
@@ -186,10 +208,31 @@ impl MyApp {
Some("Color".to_string()), Some("Color".to_string()),
move |s| { Message::ChangeColor(current_index, s) } move |s| { Message::ChangeColor(current_index, s) }
), ),
pick_list(entries, Some("Sound".to_string()), move |s| {
Message::ChangeSound(current_index, s)
}),
] ]
.into() .into()
}) })
.collect(); .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); let polygon_column = Column::with_children(polygon_rows);
column![ column![
@@ -203,25 +246,9 @@ impl MyApp {
button("Decrement").on_press(Message::ButtonPressedDecrement), button("Decrement").on_press(Message::ButtonPressedDecrement),
], ],
text("Polygon options"), text("Polygon options"),
pick_list( pick_list(all_options, Some("Choose polygon".to_string()), |s| {
[ Message::AddPolygon(s)
"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) }
),
polygon_column, polygon_column,
], ],
] ]

View File

@@ -6,11 +6,7 @@ use iced::widget::canvas;
use iced::widget::canvas::Stroke; use iced::widget::canvas::Stroke;
use iced::widget::canvas::Style; use iced::widget::canvas::Style;
use iced::{Color, Rectangle, Renderer, Theme}; use iced::{Color, Rectangle, Renderer, Theme};
use kira::PlaySoundError; use kira::sound::static_sound::StaticSoundData;
use kira::{
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
};
use std::time::Instant;
pub trait RotationExt { pub trait RotationExt {
fn rotate(&mut self, teta: f32) -> Self; fn rotate(&mut self, teta: f32) -> Self;
@@ -55,6 +51,16 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
let radius = frame.size().width / 2.0 - 32.0; let radius = frame.size().width / 2.0 - 32.0;
let mut vec = Vector::new(0.0, -radius); let mut vec = Vector::new(0.0, -radius);
let c = frame.center(); 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 // Draw all polygons by there points
for poly in &self.polygons { for poly in &self.polygons {
@@ -77,16 +83,6 @@ impl<Message> canvas::Program<Message> 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 // Draw the red dot on the current position
let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 5.0); 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)); frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));