move all music data to struct, save this data now
This commit is contained in:
167
src/main.rs
167
src/main.rs
@@ -1,6 +1,9 @@
|
||||
mod polygon_draw;
|
||||
use polygon_draw::{Polygon, PolygonFrame};
|
||||
|
||||
mod music;
|
||||
use music::Music;
|
||||
|
||||
mod utils;
|
||||
use utils::string_to_color;
|
||||
|
||||
@@ -48,23 +51,16 @@ enum Message {
|
||||
FileNameChanged(String),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct MyApp {
|
||||
poly_frame: PolygonFrame,
|
||||
#[serde(skip, default = "dummy_instant")]
|
||||
music: Music,
|
||||
time_last_frame: Instant,
|
||||
nb_sec_for_rev: f32,
|
||||
#[serde(skip, default = "dummy_audio_manager")]
|
||||
audio_manager: AudioManager,
|
||||
#[serde(skip, default = "dummy_sound")]
|
||||
default_sound: StaticSoundData,
|
||||
file_name: String,
|
||||
show_save_panel: bool,
|
||||
paused: bool,
|
||||
#[serde(skip, default = "load_path_sounds")]
|
||||
audio_manager: AudioManager,
|
||||
default_sound: StaticSoundData,
|
||||
all_sounds: Vec<String>,
|
||||
#[serde(skip, default = "load_path_saves")]
|
||||
all_saves: Vec<String>,
|
||||
current_delta: f32,
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
@@ -74,22 +70,15 @@ impl MyApp {
|
||||
let sound_data = StaticSoundData::from_file("assets/tick.ogg").expect("Fail to load audio");
|
||||
(
|
||||
Self {
|
||||
nb_sec_for_rev: 4.0,
|
||||
time_last_frame: Instant::now(),
|
||||
audio_manager: manager,
|
||||
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![
|
||||
//Polygon::n_gon(0.0, 12),
|
||||
//Polygon::triangle(0.0),
|
||||
],
|
||||
},
|
||||
music: Music::default(),
|
||||
current_delta: 0.0,
|
||||
},
|
||||
Task::none(),
|
||||
)
|
||||
@@ -99,133 +88,73 @@ impl MyApp {
|
||||
Message::WindowEvent(window::Event::Resized(size)) => {
|
||||
println!("Resize detected: {}x{}", size.width, size.height);
|
||||
}
|
||||
Message::ButtonPressedIncrement => self.nb_sec_for_rev += 0.5,
|
||||
Message::ButtonPressedIncrement => self.music.nb_sec_for_rev += 0.5,
|
||||
Message::ButtonPressedDecrement => {
|
||||
if self.nb_sec_for_rev > 0.5 {
|
||||
self.nb_sec_for_rev -= 0.5;
|
||||
if self.music.nb_sec_for_rev > 0.5 {
|
||||
self.music.nb_sec_for_rev -= 0.5;
|
||||
}
|
||||
}
|
||||
Message::AddPolygon(s) => {
|
||||
let mut poly: Polygon;
|
||||
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;
|
||||
}
|
||||
} 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()),
|
||||
}
|
||||
}
|
||||
self.poly_frame.polygons.push(poly);
|
||||
self.music.add_polygon(self.current_delta, s);
|
||||
}
|
||||
Message::Tick => {
|
||||
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.current_delta += time_btw.as_millis() as f32 / 1000.0;
|
||||
self.music
|
||||
.apply_tick(self.current_delta, time_btw, &mut self.audio_manager);
|
||||
self.time_last_frame = Instant::now();
|
||||
}
|
||||
}
|
||||
Message::Remove(i) => {
|
||||
self.poly_frame.polygons.remove(i - 1);
|
||||
self.music.remove_polygon(self.current_delta, i - 1);
|
||||
}
|
||||
Message::ChangeTeta(i, teta) => {
|
||||
self.poly_frame.polygons[i].global_teta = teta;
|
||||
self.music.set_teta(self.current_delta, i, teta);
|
||||
}
|
||||
Message::ChangeColor(i, s) => {
|
||||
let c = string_to_color(&s);
|
||||
self.poly_frame.polygons[i].color = c;
|
||||
self.poly_frame.polygons[i].color_name = s;
|
||||
self.music.set_color(self.current_delta, i, s);
|
||||
}
|
||||
Message::ChangeSound(i, s) => {
|
||||
self.poly_frame.polygons[i].sound =
|
||||
StaticSoundData::from_file(format!("./assets/{s}"))
|
||||
.expect("Fail to load audio");
|
||||
self.poly_frame.polygons[i].sound_name = s;
|
||||
let sound = StaticSoundData::from_file(format!("./assets/{s}"))
|
||||
.expect("Fail to load audio");
|
||||
self.music.set_sound(self.current_delta, i, sound, s);
|
||||
}
|
||||
Message::Save => {
|
||||
let json = serde_json::to_string_pretty(&self).unwrap();
|
||||
fs::write(format!("./saves/{0}", &self.file_name), json).unwrap();
|
||||
let json = serde_json::to_string_pretty(&self.music).unwrap();
|
||||
fs::write(format!("./saves/{0}", &self.music.file_name), json).unwrap();
|
||||
}
|
||||
Message::Load => {
|
||||
let json = fs::read_to_string(format!("./saves/{0}", &self.file_name)).unwrap();
|
||||
let decoded: MyApp = serde_json::from_str(&json).unwrap();
|
||||
*self = decoded;
|
||||
self.poly_frame.update();
|
||||
let json = fs::read_to_string(format!("./saves/{0}", &self.music.file_name));
|
||||
match json {
|
||||
Ok(j) => {
|
||||
let decoded: Music = serde_json::from_str(&j).unwrap();
|
||||
self.music = decoded;
|
||||
self.music.update_frame(self.current_delta);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error, no saves with this name to load, {e} ");
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::ToggleSavePanel => self.show_save_panel = !self.show_save_panel,
|
||||
Message::FileNameChanged(s) => self.file_name = s,
|
||||
Message::FileNameChanged(s) => self.music.file_name = s,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<Message> {
|
||||
let txt_nb_rev = format!("Number of second for revolution : {}", self.nb_sec_for_rev);
|
||||
let txt_nb_rev = format!(
|
||||
"Number of second for revolution : {}",
|
||||
self.music.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
|
||||
.music
|
||||
.current_frame(self.current_delta)
|
||||
.polygons
|
||||
.iter()
|
||||
.map(|polygon| {
|
||||
@@ -284,15 +213,17 @@ impl MyApp {
|
||||
|
||||
if self.show_save_panel {
|
||||
save_panel.push(
|
||||
TextInput::new("Name File", &self.file_name)
|
||||
TextInput::new("Name File", &self.music.file_name)
|
||||
.on_input(|new_value| Message::FileNameChanged(new_value))
|
||||
.into(),
|
||||
);
|
||||
save_panel.push(button("Save").on_press(Message::Save).into());
|
||||
save_panel.push(
|
||||
pick_list(self.all_saves.clone(), Some(&self.file_name), move |s| {
|
||||
Message::FileNameChanged(s)
|
||||
})
|
||||
pick_list(
|
||||
self.all_saves.clone(),
|
||||
Some(&self.music.file_name),
|
||||
move |s| Message::FileNameChanged(s),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
@@ -303,7 +234,7 @@ impl MyApp {
|
||||
row(save_panel).spacing(20),
|
||||
row![
|
||||
container(
|
||||
canvas(&self.poly_frame)
|
||||
canvas(self.music.current_frame(self.current_delta))
|
||||
.height(Length::FillPortion(1))
|
||||
.width(Length::FillPortion(1))
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user