sound by polygons and ui for add polygons and change global rotation

This commit is contained in:
2025-06-02 14:43:10 +02:00
parent 051d4b66e5
commit 26d7c9c514
3 changed files with 123 additions and 38 deletions

View File

@@ -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<Message> {
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
let mut i = 0;
let polygon_rows: Vec<Element<Message>> = 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,
],
]
]