From 3d3853c766882284c62abe29095b15ba6039e9e4 Mon Sep 17 00:00:00 2001 From: subalterngames Date: Sun, 7 Jan 2024 15:28:40 -0500 Subject: [PATCH] Reset the player, view, and time when a file is loaded or a new file is created --- audio/src/conn.rs | 8 ++++++++ common/src/time.rs | 5 +++++ common/src/view.rs | 16 +++++++++++++++- io/src/lib.rs | 9 ++++++++- io/src/open_file_panel.rs | 2 ++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/audio/src/conn.rs b/audio/src/conn.rs index e4921c27..227bfe7c 100644 --- a/audio/src/conn.rs +++ b/audio/src/conn.rs @@ -228,6 +228,14 @@ impl Conn { *self.export_state.lock() != ExportState::NotExporting } + /// When a new save file is loaded or a new file is opened, stop playing music if any music is playing. + pub fn on_new_file(&mut self, state: &State) { + let play_state = *self.play_state.lock(); + if let PlayState::Playing(_) = play_state { + self.stop_music(&state.music); + } + } + /// Schedule MIDI events and start to play music. fn start_music(&mut self, state: &State) { // Get the start time. diff --git a/common/src/time.rs b/common/src/time.rs index a8ff7d60..95283411 100644 --- a/common/src/time.rs +++ b/common/src/time.rs @@ -47,6 +47,11 @@ impl Time { pub fn samples_to_ppq(&self, samples: u64, framerate: f32) -> u64 { ((self.bpm.get_f() * samples as f32) / (BPM_TO_SECONDS * framerate) * PPQ_F) as u64 } + + pub fn reset(&mut self) { + self.cursor = 0; + self.playback = 0; + } } impl Default for Time { diff --git a/common/src/view.rs b/common/src/view.rs index 6fff0772..2438ae30 100644 --- a/common/src/view.rs +++ b/common/src/view.rs @@ -29,6 +29,8 @@ pub struct View { zoom_increments: HashMap, /// The default zoom index. initial_zoom_index: usize, + #[serde(skip)] + initial_dn: [u8; 2], } impl View { @@ -95,6 +97,7 @@ impl View { zoom_index, zoom_increments, initial_zoom_index, + initial_dn: dn, } } @@ -172,7 +175,18 @@ impl View { } // Get the time delta. let dt = self.zoom_levels[self.zoom_index.get()]; - self.dt = [self.dt[0], dt]; + self.dt = [self.dt[0], self.dt[0] + dt]; + } + + /// Reset the view. + pub fn reset(&mut self) { + // Reset the zoom. + self.zoom_index.set(self.initial_zoom_index); + // Reset the time. + let dt = self.zoom_levels[self.initial_zoom_index]; + self.dt = [0, dt]; + // Reset the notes. + self.dn = self.initial_dn; } /// Returns the note delta. diff --git a/io/src/lib.rs b/io/src/lib.rs index 5d4d05f4..470f0894 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -283,7 +283,14 @@ impl IO { // New file. if input.happened(&InputEvent::NewFile) { paths_state.saves.filename = None; + // Stop playing music. + conn.on_new_file(state); + // Reset the music. state.music = Music::default(); + // Reset the view. + state.view.reset(); + // Reset the time. + state.time.reset(); } // Open file. else if input.happened(&InputEvent::OpenFile) { @@ -416,7 +423,7 @@ impl IO { false } - /// Open a save file from a path. + /// Open a save file from a path. This is called from main.rs pub fn load_save( &self, save_path: &Path, diff --git a/io/src/open_file_panel.rs b/io/src/open_file_panel.rs index 402db72e..7aec5adf 100644 --- a/io/src/open_file_panel.rs +++ b/io/src/open_file_panel.rs @@ -279,6 +279,8 @@ impl Panel for OpenFilePanel { if let Some(selected) = paths_state.children.selected { // Disable the panel. self.disable(state); + // Stop the music. + conn.on_new_file(state); // Get the path. let path = paths_state.children.children[selected].path.clone(); // Read the save file.