Skip to content

Commit

Permalink
Reset the player, view, and time when a file is loaded or a new file …
Browse files Browse the repository at this point in the history
…is created
  • Loading branch information
subalterngames committed Jan 7, 2024
1 parent 01e366d commit 3d3853c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions audio/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions common/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion common/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct View {
zoom_increments: HashMap<EditMode, usize>,
/// The default zoom index.
initial_zoom_index: usize,
#[serde(skip)]
initial_dn: [u8; 2],
}

impl View {
Expand Down Expand Up @@ -95,6 +97,7 @@ impl View {
zoom_index,
zoom_increments,
initial_zoom_index,
initial_dn: dn,
}
}

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions io/src/open_file_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3d3853c

Please sign in to comment.