gitignore and optimisation

This commit is contained in:
2025-07-06 11:39:11 +02:00
parent 85c2234033
commit 8d95d65558
5 changed files with 58 additions and 101 deletions

1
gitignore Normal file
View File

@@ -0,0 +1 @@
saves/*

View File

@@ -1,22 +0,0 @@
{
"poly_frame": {
"teta": 1.0739278,
"polygons": [
{
"global_teta": 0.0,
"points_teta": [
0.0,
1.5707964,
3.1415927,
4.712389
],
"sound_name": "E_MI.ogg",
"name": "Square",
"color_name": "Blue"
}
]
},
"nb_sec_for_rev": 5.0,
"file_name": "TEST_SAVE.json",
"show_save_panel": true
}

View File

@@ -1,40 +0,0 @@
{
"poly_frame": {
"teta": 4.3590484,
"polygons": [
{
"global_teta": 0.48619887,
"points_teta": [
1.0471976,
1.2566371,
2.5132742,
3.7699113,
5.0265484,
5.2359877
],
"sound_name": "B_SI.ogg",
"name": "Nr6In30",
"color_name": "Blue"
},
{
"global_teta": 0.0,
"points_teta": [
0.44879895,
1.3463968,
2.0943952,
2.2439947,
4.039191,
4.1887903,
4.9367886,
5.8343863
],
"sound_name": "D_RE.ogg",
"name": "Nr8In42",
"color_name": "Cyan"
}
]
},
"nb_sec_for_rev": 3.5,
"file_name": "polymusic.json",
"show_save_panel": true
}

View File

@@ -1,9 +0,0 @@
{
"poly_frame": {
"teta": 3.7117934,
"polygons": []
},
"nb_sec_for_rev": 4.0,
"file_name": "test_save.json",
"show_save_panel": true
}

View File

@@ -6,8 +6,9 @@ use utils::string_to_color;
use std::fs;
use iced::{Application, Element, Settings, Subscription, window};
use iced::{
Color, Element, Length, Task, Theme,
Color, Length, Task, Theme,
time::{self, Duration},
widget::{Column, TextInput, button, canvas, column, container, pick_list, row, slider, text},
};
@@ -30,6 +31,7 @@ fn main() -> iced::Result {
#[derive(Debug, Clone)]
enum Message {
WindowEvent(window::Event),
ButtonPressedIncrement,
ButtonPressedDecrement,
Tick,
@@ -56,6 +58,11 @@ struct MyApp {
default_sound: StaticSoundData,
file_name: String,
show_save_panel: bool,
paused: bool,
#[serde(skip, default = "load_path_sounds")]
all_sounds: Vec<String>,
#[serde(skip, default = "load_path_saves")]
all_saves: Vec<String>,
}
impl MyApp {
@@ -71,6 +78,9 @@ impl MyApp {
default_sound: sound_data.clone(),
file_name: "polymusic.json".to_string(),
show_save_panel: true,
paused: false,
all_sounds: load_path_sounds(),
all_saves: load_path_saves(),
poly_frame: PolygonFrame {
teta: 0.0,
polygons: vec![
@@ -84,6 +94,9 @@ impl MyApp {
}
fn update(&mut self, message: Message) {
match message {
Message::WindowEvent(window::Event::Resized(size)) => {
println!("Resize detected: {}x{}", size.width, size.height);
}
Message::ButtonPressedIncrement => self.nb_sec_for_rev += 0.5,
Message::ButtonPressedDecrement => {
if self.nb_sec_for_rev > 0.5 {
@@ -151,22 +164,24 @@ impl MyApp {
self.poly_frame.polygons.push(poly);
}
Message::Tick => {
let time_btw = Instant::now().duration_since(self.time_last_frame);
let teta_temp = self.poly_frame.teta;
self.poly_frame.teta += 2.0
* PI
* (1.0 / self.nb_sec_for_rev)
* (time_btw.as_millis() as f32 / 1_000.0);
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(sound.clone())
.expect("Error to play sound");
if !self.paused {
let time_btw = Instant::now().duration_since(self.time_last_frame);
let teta_temp = self.poly_frame.teta;
self.poly_frame.teta += 2.0
* PI
* (1.0 / self.nb_sec_for_rev)
* (time_btw.as_millis() as f32 / 1_000.0);
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(sound.clone())
.expect("Error to play sound");
}
self.time_last_frame = Instant::now();
}
self.time_last_frame = Instant::now();
}
Message::Remove(i) => {
self.poly_frame.polygons.remove(i - 1);
@@ -197,6 +212,7 @@ impl MyApp {
}
Message::ToggleSavePanel => self.show_save_panel = !self.show_save_panel,
Message::FileNameChanged(s) => self.file_name = s,
_ => {}
}
}
@@ -204,6 +220,7 @@ impl MyApp {
let txt_nb_rev = format!("Number of second for revolution : {}", self.nb_sec_for_rev);
let mut i = 0;
let entries = self.all_sounds.clone();
//Create all polygon options
let polygon_rows: Vec<Element<Message>> = self
.poly_frame
@@ -212,12 +229,6 @@ impl MyApp {
.map(|polygon| {
let current_index = i;
i += 1;
let mut entries: Vec<String> = fs::read_dir("./assets")
.unwrap()
.filter_map(|res| res.ok())
.map(|e| e.path().file_name().unwrap().to_str().unwrap().to_string())
.collect();
entries.sort();
column![
row![
text(&polygon.name),
@@ -229,7 +240,7 @@ impl MyApp {
Some(&polygon.color_name),
move |s| { Message::ChangeColor(current_index, s) }
),
pick_list(entries, Some(&polygon.sound_name), move |s| {
pick_list(entries.clone(), Some(&polygon.sound_name), move |s| {
Message::ChangeSound(current_index, s)
}),
]
@@ -268,11 +279,6 @@ impl MyApp {
.on_press(Message::ToggleSavePanel)
.into(),
];
let mut entries_saves: Vec<String> = fs::read_dir("./saves")
.unwrap()
.filter_map(|res| res.ok())
.map(|e| e.path().file_name().unwrap().to_str().unwrap().to_string())
.collect();
if self.show_save_panel {
save_panel.push(
@@ -282,7 +288,7 @@ impl MyApp {
);
save_panel.push(button("Save").on_press(Message::Save).into());
save_panel.push(
pick_list(entries_saves, Some(&self.file_name), move |s| {
pick_list(self.all_saves.clone(), Some(&self.file_name), move |s| {
Message::FileNameChanged(s)
})
.into(),
@@ -325,7 +331,10 @@ impl MyApp {
.into()
}
fn subscription(&self) -> iced::Subscription<Message> {
time::every(Duration::from_millis(16)).map(|_| Message::Tick)
iced::Subscription::batch([
window::events().map(|(_id, event)| Message::WindowEvent(event)),
iced::time::every(std::time::Duration::from_millis(16)).map(|_| Message::Tick),
])
}
}
@@ -339,3 +348,21 @@ fn dummy_audio_manager() -> AudioManager {
AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())
.expect("Error to load AudioManager")
}
fn load_path_sounds() -> Vec<String> {
let mut entries: Vec<String> = fs::read_dir("./assets")
.unwrap()
.filter_map(|res| res.ok())
.map(|e| e.path().file_name().unwrap().to_str().unwrap().to_string())
.collect();
entries.sort();
entries
}
fn load_path_saves() -> Vec<String> {
fs::read_dir("./saves")
.unwrap()
.filter_map(|res| res.ok())
.map(|e| e.path().file_name().unwrap().to_str().unwrap().to_string())
.collect()
}