Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
subalterngames committed Dec 7, 2023
1 parent 9512504 commit a0e2ce8
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 140 deletions.
2 changes: 0 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ use view::View;
mod edit_mode;
pub mod music_panel_field;
pub use edit_mode::*;
mod selectable;
mod selection;
pub use selectable::Selectable;
pub use selection::Selection;
mod piano_roll_mode;
pub use piano_roll_mode::PianoRollMode;
Expand Down
3 changes: 3 additions & 0 deletions common/src/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct Music {
pub midi_tracks: Vec<MidiTrack>,
/// The index of the selected track.
pub selected: Option<usize>,
/// If true, at least one note or effect has been changed.
#[serde(skip_serializing, skip_deserializing)]
pub dirty: bool,
}

impl Music {
Expand Down
24 changes: 0 additions & 24 deletions common/src/selectable.rs

This file was deleted.

22 changes: 11 additions & 11 deletions common/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ impl Selection {

/// Returns the selected notes and effects.
pub fn get_selection<'a>(&self, music: &'a Music) -> Option<(Vec<&'a Note>, Vec<&'a Effect>)> {
match music.get_selected_track() {
None => None,
Some(track) => Some((
music.get_selected_track().map(|track| {
(
self.notes.iter().map(|i| &track.notes[*i]).collect(),
self.effects.iter().map(|i| &track.effects[*i]).collect(),
)),
}
)
})
}

/// Returns mutable selected notes and effects.
Expand Down Expand Up @@ -187,10 +186,11 @@ impl Selection {
pub fn get_dt(&self, music: &Music) -> Option<(u64, u64)> {
match self.get_events(music) {
Some(events) => match events.iter().map(|s| s.get_start_time()).min() {
Some(min) => match events.iter().map(|s| s.get_end_time()).max() {
Some(max) => Some((min, max)),
None => None,
},
Some(min) => events
.iter()
.map(|s| s.get_end_time())
.max()
.map(|max| (min, max)),
None => None,
},
None => None,
Expand All @@ -213,7 +213,7 @@ impl Selection {
.map(|(index, effect)| Event::Effect { effect, index }),
);
// Sort the selectables by start time.
events.sort_by(|a, b| a.get_start_time().cmp(&b.get_start_time()));
events.sort_by_key(|e| e.get_start_time());
Some(events)
}
None => None,
Expand All @@ -237,7 +237,7 @@ impl Selection {
.enumerate()
.map(|(index, effect)| Event::Effect { effect, index }),
);
events.sort_by(|a, b| a.get_start_time().cmp(&b.get_start_time()));
events.sort_by_key(|e| e.get_start_time());
Some(events)
}
None => None,
Expand Down
8 changes: 7 additions & 1 deletion data/text.csv
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,10 @@ LINKS_PANEL_INPUT_TTS_2,\0 to open an invite link to the Cacophony Discord serve
LINKS_PANEL_INPUT_TTS_3,\0 to open an Cacophony repo.
LINKS_PANEL_INPUT_TTS_4,\0 to close this panel.
EXPORT_PANEL_APPENDING_DECAY,Appending decay...
EXPORT_PANEL_WRITING,Writing to disk...
EXPORT_PANEL_WRITING,Writing to disk...
EFFECT_TYPE_CHORUS,Chorus
EFFECT_TYPE_PAN,Pan
EFFECT_TYPE_REVERB,Reverb
EFFECT_TYPE_PITCH_BEND,Pitch Bend
EFFECT_TYPE_CHANNEL_PRESSURE,Channel Pressure
EFFECT_TYPE_POLYPHONIC_KEY_PRESSURE,Aftertouch
3 changes: 3 additions & 0 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ impl IO {
return false;
}

// Mark the music as not dirty.
state.music.dirty = false;

// Alphanumeric input.
if state.input.alphanumeric_input {
// Get the focused panel.
Expand Down
16 changes: 9 additions & 7 deletions io/src/piano_roll/piano_roll_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ enum CopiedEvent {
Note(Note),
}

impl CopiedEvent {
fn from(event: Event<'_>) -> Self {
match event {
Event::Effect { effect, index: _ } => Self::Effect(effect.clone()),
Event::Note { note, index: _ } => Self::Note(note.clone()),
impl From<Event<'_>> for CopiedEvent {
fn from(value: Event) -> Self {
match value {
Event::Effect { effect, index: _ } => Self::Effect(*effect),
Event::Note { note, index: _ } => Self::Note(*note),
}
}
}

impl CopiedEvent {
fn get_start_time(&self) -> u64 {
match self {
Self::Effect(effect) => effect.time,
Expand Down Expand Up @@ -458,12 +460,12 @@ impl Panel for PianoRollPanel {
for event in self.copy_buffer.iter() {
match event {
CopiedEvent::Effect(effect) => {
let mut effect = effect.clone();
let mut effect = *effect;
effect.time += state.time.cursor;
track.effects.push(effect);
}
CopiedEvent::Note(note) => {
let mut note = note.clone();
let mut note = *note;
let dt = note.end - note.start;
note.start = (note.start - min_time) + state.time.cursor;
note.end = note.start + dt;
Expand Down
1 change: 1 addition & 0 deletions io/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Snapshot {
/// - `from_state` The initial state of the delta. This is usually a clone of a `State` prior to modifying the primary `State`.
/// - `to_state` The final state of the delta. This is a reference to the primary `State`.
pub fn from_states(from_state: State, to_state: &mut State) -> Self {
to_state.music.dirty = true;
Self {
from_state: Some(from_state),
to_state: Some(to_state.clone()),
Expand Down
2 changes: 1 addition & 1 deletion render/src/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ impl Panels {
self.quit_panel.popup.late_update(state, renderer);
self.links_panel.popup.late_update(state, renderer);
self.main_menu.late_update(renderer, conn);
self.piano_roll_panel.late_update(state, renderer);
self.piano_roll_panel.late_update(state, conn, renderer);
}
}
98 changes: 55 additions & 43 deletions render/src/piano_roll_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod multi_track;
mod top_bar;
mod viewable_notes;
mod volume;
use common::{SelectMode, State, U64orF32, NOTE_NAMES, PPQ_U};
use common::{State, U64orF32, NOTE_NAMES, PPQ_U};
use hashbrown::HashSet;
use multi_track::MultiTrack;
use text::ppq_to_string;
Expand Down Expand Up @@ -43,6 +43,8 @@ pub struct PianoRollPanel {
time_horizontal_line_y: f32,
/// The bottom y coordinates for time lines in single- and multi- track modes.
time_line_bottoms: [f32; 2],
/// The notes currently in the viewport.
viewable_notes: ViewableNotes,
}

impl PianoRollPanel {
Expand Down Expand Up @@ -108,10 +110,23 @@ impl PianoRollPanel {
volume,
multi_track,
time_line_bottoms,
viewable_notes: ViewableNotes::default(),
}
}

pub fn late_update(&mut self, state: &State, renderer: &Renderer) {
pub fn late_update(&mut self, state: &State, conn: &Conn, renderer: &Renderer) {
if state.music.dirty {
let (_, focus) = self.get_panel_and_focus(state);
let dt = Self::get_view_dt(state, conn).map(U64orF32::from);
self.viewable_notes = ViewableNotes::new(
self.piano_roll_rows_rect[0],
self.piano_roll_rows_rect[2],
state,
conn,
focus,
dt,
);
}
self.piano_roll_rows.late_update(state, renderer);
}

Expand Down Expand Up @@ -195,16 +210,21 @@ impl PianoRollPanel {
fn get_play_state(play_state: &SharedPlayState) -> PlayState {
*play_state.lock()
}
}

impl Drawable for PianoRollPanel {
fn update(&self, renderer: &Renderer, state: &State, conn: &Conn, text: &Text, _: &PathsState) {
fn get_panel_and_focus(&self, state: &State) -> (&Panel, bool) {
let panel = if state.view.single_track {
&self.panel_single_track
} else {
&self.panel_multi_track
};
let focus = panel.has_focus(state);
(panel, focus)
}
}

impl Drawable for PianoRollPanel {
fn update(&self, renderer: &Renderer, state: &State, conn: &Conn, text: &Text, _: &PathsState) {
let (panel, focus) = self.get_panel_and_focus(state);

// Panel background.
panel.update(focus, renderer);
Expand All @@ -223,17 +243,9 @@ impl Drawable for PianoRollPanel {
if state.view.single_track {
// Piano roll rows.
self.piano_roll_rows.update(renderer);
// Get the viewable notes.
let notes = ViewableNotes::new(
self.piano_roll_rows_rect[0],
self.piano_roll_rows_rect[2],
state,
conn,
focus,
dt,
);
// Draw the selection background.
let selected = notes
let selected = self
.viewable_notes
.notes
.iter()
.filter(|n| n.selected && n.in_pitch_range)
Expand All @@ -251,7 +263,7 @@ impl Drawable for PianoRollPanel {
};
let x1 = ViewableNotes::get_note_x(
select_1.note.end,
notes.pulses_per_pixel,
self.viewable_notes.pulses_per_pixel,
self.piano_roll_rows_rect[0],
&dt,
);
Expand All @@ -263,8 +275,12 @@ impl Drawable for PianoRollPanel {
}
}

let in_pitch_range: Vec<&ViewableNote> =
notes.notes.iter().filter(|n| n.in_pitch_range).collect();
let in_pitch_range: Vec<&ViewableNote> = self
.viewable_notes
.notes
.iter()
.filter(|n| n.in_pitch_range)
.collect();
let selected_pitches: Vec<u8> = selected
.iter()
.map(|n| n.note.note)
Expand All @@ -274,14 +290,13 @@ impl Drawable for PianoRollPanel {

// Draw the notes.
for note in in_pitch_range.iter() {
let w = notes.get_note_w(note);
// Get the y value from the pitch.
let y = self.piano_roll_rows_rect[1]
+ ((state.view.dn[0] - note.note.note) as f32) * self.cell_size[1];
renderer.rectangle_pixel([note.x, y], [w, self.cell_size[1]], &note.color)
renderer.rectangle_pixel([note.x, y], [note.w, self.cell_size[1]], &note.color)
}
// Volume.
self.volume.update(&notes, renderer, state);
self.volume.update(&self.viewable_notes, renderer, state);
// Note names.
let note_name_color = if focus {
&ColorKey::Separator
Expand Down Expand Up @@ -339,36 +354,33 @@ impl Drawable for PianoRollPanel {
let playback_string_width = playback_string.chars().count() as u32;
let playback_line_x0 = playback_x + playback_string_width / 2;
let selection_x = playback_x + playback_string_width + TIME_PADDING;
let (selection_string, selected) = match &state.select_mode {
SelectMode::Single(index) => match index {
Some(index) => {
let note = &state.music.get_selected_track().unwrap().notes[*index];
let (selection_string, selected) = match state.selection.get_events(&state.music) {
Some(events) => {
if state.selection.single {
(
text.get_with_values(
"PIANO_ROLL_PANEL_SELECTED_SINGLE",
&[note.get_name(), &(note.start / PPQ_U).to_string()],
&[
&text.get_event_name(&events[0]),
&(events[0].get_start_time() / PPQ_U).to_string(),
],
),
true,
)
}
None => (text.get("PIANO_ROLL_PANEL_SELECTED_NONE"), false),
},
SelectMode::Many(indices) => match indices {
Some(_) => {
let mut notes = state.select_mode.get_notes(&state.music).unwrap();
notes.sort();
let min = notes[0].start / PPQ_U;
let max = notes.last().unwrap().end / PPQ_U;
(
text.get_with_values(
"PIANO_ROLL_PANEL_SELECTED_MANY",
&[&min.to_string(), &max.to_string()],
} else {
match state.selection.get_dt(&state.music) {
Some((min, max)) => (
text.get_with_values(
"PIANO_ROLL_PANEL_SELECTED_MANY",
&[&min.to_string(), &max.to_string()],
),
true,
),
true,
)
None => (text.get("PIANO_ROLL_PANEL_SELECTED_NONE"), false),
}
}
None => (text.get("PIANO_ROLL_PANEL_SELECTED_NONE"), false),
},
}
None => (text.get("PIANO_ROLL_PANEL_SELECTED_NONE"), false),
};
let playback_label = Label {
text: playback_string,
Expand Down
2 changes: 1 addition & 1 deletion render/src/piano_roll_panel/multi_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ impl MultiTrack {
// Draw some notes.
for note in notes.notes.iter() {
let note_y = note_y + (1.0 - ((note.note.note - MIN_NOTE) as f32) / DN_F) * h;
let mut note_w = notes.get_note_w(note);
// Clamp the note.
let note_x0 = note.x.clamp(self.rect_f[0], x1);
let mut note_w = note.w;
let note_x1 = note.x + note_w;
if note_x1 > x1 {
note_w = x1 - note_x0;
Expand Down
11 changes: 6 additions & 5 deletions render/src/piano_roll_panel/top_bar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::panel::*;
use common::{EditMode, IndexedEditModes, PianoRollMode, SelectMode};
use common::{EditMode, IndexedEditModes, PianoRollMode};
use hashbrown::HashMap;
use text::ppq_to_string;

Expand Down Expand Up @@ -165,10 +165,11 @@ impl TopBar {
// Edit mode.
let edit_mode = match state.piano_roll_mode {
PianoRollMode::Edit => Self::get_edit_mode_text(&state.edit_mode, text),
PianoRollMode::Select => match state.select_mode {
SelectMode::Single(_) => text.get_ref("PIANO_ROLL_PANEL_EDIT_MODE_SINGLE"),
SelectMode::Many(_) => text.get_ref("PIANO_ROLL_PANEL_EDIT_MODE_MANY"),
},
PianoRollMode::Select => text.get_ref(if state.selection.single {
"PIANO_ROLL_PANEL_EDIT_MODE_SINGLE"
} else {
"PIANO_ROLL_PANEL_EDIT_MODE_MANY"
}),
PianoRollMode::Time => Self::get_edit_mode_text(&state.time.mode, text),
PianoRollMode::View => Self::get_edit_mode_text(&state.view.mode, text),
};
Expand Down
Loading

0 comments on commit a0e2ce8

Please sign in to comment.