Skip to content

Commit

Permalink
Piano: Add UI support for different lengths of notes
Browse files Browse the repository at this point in the history
  • Loading branch information
petelliott authored and awesomekling committed Oct 12, 2020
1 parent 27b990e commit b82f2df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
63 changes: 46 additions & 17 deletions Applications/Piano/RollWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,58 @@ void RollWidget::mousedown_event(GUI::MouseEvent& event)
if (!widget_inner_rect().contains(event.x(), event.y()))
return;

int y = (event.y() + vertical_scrollbar().value()) - frame_thickness();
m_note_drag_start = event.position();

int y = (m_note_drag_start.value().y() + vertical_scrollbar().value()) - frame_thickness();
y /= note_height;
m_drag_note = (note_count - 1) - y;

mousemove_event(event);
}

// There's a case where we can't just use x / m_note_width. For example, if
// your m_note_width is 3.1 you will have a rect starting at 3. When that
// leftmost pixel of the rect is clicked you will do 3 / 3.1 which is 0
// and not 1. We can avoid that case by shifting x by 1 if m_note_width is
// fractional, being careful not to shift out of bounds.
int x = (event.x() + horizontal_scrollbar().value()) - frame_thickness();
bool note_width_is_fractional = m_note_width - static_cast<int>(m_note_width) != 0;
bool x_is_not_last = x != widget_inner_rect().width() - 1;
if (note_width_is_fractional && x_is_not_last)
++x;
x /= m_note_width;

int note = (note_count - 1) - y;
u32 on_sample = roll_length * (static_cast<double>(x) / m_num_notes);
u32 off_sample = (roll_length * (static_cast<double>(x + 1) / m_num_notes)) - 1;
m_track_manager.current_track().set_roll_note(note, on_sample, off_sample);
void RollWidget::mousemove_event(GUI::MouseEvent& event)
{
if (!m_note_drag_start.has_value())
return;

if (m_note_drag_location.has_value()) {
// Clear previous note
m_track_manager.current_track().set_roll_note(m_drag_note, m_note_drag_location.value().on_sample, m_note_drag_location.value().off_sample);
}

auto get_note_x = [&](int x0) {
// There's a case where we can't just use x / m_note_width. For example, if
// your m_note_width is 3.1 you will have a rect starting at 3. When that
// leftmost pixel of the rect is clicked you will do 3 / 3.1 which is 0
// and not 1. We can avoid that case by shifting x by 1 if m_note_width is
// fractional, being careful not to shift out of bounds.
int x = (x0 + horizontal_scrollbar().value()) - frame_thickness();
bool note_width_is_fractional = m_note_width - static_cast<int>(m_note_width) != 0;
bool x_is_not_last = x != widget_inner_rect().width() - 1;
if (note_width_is_fractional && x_is_not_last)
++x;
x /= m_note_width;
return x;
};

int x0 = get_note_x(m_note_drag_start.value().x());
int x1 = get_note_x(event.x());

u32 on_sample = roll_length * (static_cast<double>(min(x0, x1)) / m_num_notes);
u32 off_sample = (roll_length * (static_cast<double>(max(x0, x1) + 1) / m_num_notes)) - 1;
m_track_manager.current_track().set_roll_note(m_drag_note, on_sample, off_sample);
m_note_drag_location = RollNote({ on_sample, off_sample });

update();
}

void RollWidget::mouseup_event(GUI::MouseEvent& event)
{
(void)event;
m_note_drag_start = {};
m_note_drag_location = {};
}

// FIXME: Implement zoom and horizontal scroll events in LibGUI, not here.
void RollWidget::mousewheel_event(GUI::MouseEvent& event)
{
Expand Down
6 changes: 6 additions & 0 deletions Applications/Piano/RollWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class RollWidget final : public GUI::ScrollableWidget {

virtual void paint_event(GUI::PaintEvent&) override;
virtual void mousedown_event(GUI::MouseEvent& event) override;
virtual void mousemove_event(GUI::MouseEvent& event) override;
virtual void mouseup_event(GUI::MouseEvent& event) override;
virtual void mousewheel_event(GUI::MouseEvent&) override;

TrackManager& m_track_manager;
Expand All @@ -55,4 +57,8 @@ class RollWidget final : public GUI::ScrollableWidget {
int m_num_notes { 0 };
double m_note_width { 0.0 };
int m_zoom_level { 1 };

Optional<Gfx::IntPoint> m_note_drag_start;
Optional<RollNote> m_note_drag_location;
int m_drag_note;
};
8 changes: 1 addition & 7 deletions Applications/Piano/Track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void Track::set_roll_note(int note, u32 on_sample, u32 off_sample)
sync_roll(note);
return;
}
if (it->on_sample == new_roll_note.on_sample && it->off_sample == new_roll_note.off_sample) {
if (it->on_sample <= new_roll_note.on_sample && it->off_sample >= new_roll_note.on_sample) {
if (m_time >= it->on_sample && m_time <= it->off_sample)
set_note(note, Off);
m_roll_notes[note].remove(it);
Expand All @@ -301,12 +301,6 @@ void Track::set_roll_note(int note, u32 on_sample, u32 off_sample)
it = m_roll_notes[note].begin();
continue;
}
if (it->on_sample < new_roll_note.on_sample && it->off_sample >= new_roll_note.on_sample) {
if (m_time >= new_roll_note.off_sample && m_time <= it->off_sample)
set_note(note, Off);
it->off_sample = new_roll_note.on_sample - 1;
ASSERT(it->length() >= 2);
}
++it;
}

Expand Down

0 comments on commit b82f2df

Please sign in to comment.