diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2020-10-05 20:31:01 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-12 19:41:53 +0200 |
commit | 01bff0141e5410673b2dc9aa20399b178fa6cece (patch) | |
tree | 6c70209107af35e50914ae34db18aadf6d7854b8 | |
parent | 0c5497829e77d176e74af7df3c6d3eddfdbb08d2 (diff) | |
download | serenity-01bff0141e5410673b2dc9aa20399b178fa6cece.zip |
Piano: Highlight pressed key in roll widget
-rw-r--r-- | Applications/Piano/KeysWidget.cpp | 11 | ||||
-rw-r--r-- | Applications/Piano/KeysWidget.h | 1 | ||||
-rw-r--r-- | Applications/Piano/MainWidget.cpp | 2 | ||||
-rw-r--r-- | Applications/Piano/RollWidget.cpp | 5 | ||||
-rw-r--r-- | Applications/Piano/RollWidget.h | 5 | ||||
-rw-r--r-- | Libraries/LibGfx/Color.h | 2 |
6 files changed, 25 insertions, 1 deletions
diff --git a/Applications/Piano/KeysWidget.cpp b/Applications/Piano/KeysWidget.cpp index 3645c973ab..65fcdc39b5 100644 --- a/Applications/Piano/KeysWidget.cpp +++ b/Applications/Piano/KeysWidget.cpp @@ -63,6 +63,17 @@ void KeysWidget::set_key(int key, Switch switch_key) m_track_manager.set_note_current_octave(key, switch_key); } +bool KeysWidget::note_is_set(int note) const +{ + if (note < m_track_manager.octave_base()) + return false; + + if (note >= m_track_manager.octave_base() + note_count) + return false; + + return m_key_on[note - m_track_manager.octave_base()] != 0; +} + int KeysWidget::key_code_to_key(int key_code) const { switch (key_code) { diff --git a/Applications/Piano/KeysWidget.h b/Applications/Piano/KeysWidget.h index b5e4a066d4..d0b5a833ce 100644 --- a/Applications/Piano/KeysWidget.h +++ b/Applications/Piano/KeysWidget.h @@ -41,6 +41,7 @@ public: int mouse_note() const; void set_key(int key, Switch); + bool note_is_set(int note) const; private: explicit KeysWidget(TrackManager&); diff --git a/Applications/Piano/MainWidget.cpp b/Applications/Piano/MainWidget.cpp index 661eee8b4f..fbfafd81fb 100644 --- a/Applications/Piano/MainWidget.cpp +++ b/Applications/Piano/MainWidget.cpp @@ -69,6 +69,8 @@ MainWidget::MainWidget(TrackManager& track_manager) m_knobs_widget = m_keys_and_knobs_container->add<KnobsWidget>(track_manager, *this); m_knobs_widget->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); m_knobs_widget->set_preferred_size(350, 0); + + m_roll_widget->set_keys_widget(m_keys_widget); } MainWidget::~MainWidget() diff --git a/Applications/Piano/RollWidget.cpp b/Applications/Piano/RollWidget.cpp index 3c6b4b627d..255c5d0cbe 100644 --- a/Applications/Piano/RollWidget.cpp +++ b/Applications/Piano/RollWidget.cpp @@ -92,6 +92,8 @@ void RollWidget::paint_event(GUI::PaintEvent& event) for (int y = 0; y < notes_to_paint; ++y) { int y_pos = y * note_height; + + int note = (note_count - note_offset - 1) - y; for (int x = 0; x < horizontal_notes_to_paint; ++x) { // This is needed to avoid rounding errors. You can't just use // m_note_width as the width. @@ -105,6 +107,9 @@ void RollWidget::paint_event(GUI::PaintEvent& event) else painter.fill_rect(rect, Color::White); + if (keys_widget() && keys_widget()->note_is_set(note)) + painter.fill_rect(rect, note_pressed_color.with_alpha(128)); + painter.draw_line(rect.top_right(), rect.bottom_right(), Color::Black); painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::Black); } diff --git a/Applications/Piano/RollWidget.h b/Applications/Piano/RollWidget.h index 6c5acbf224..28910156cc 100644 --- a/Applications/Piano/RollWidget.h +++ b/Applications/Piano/RollWidget.h @@ -27,6 +27,7 @@ #pragma once +#include "KeysWidget.h" #include "Music.h" #include <LibGUI/ScrollableWidget.h> @@ -37,6 +38,9 @@ class RollWidget final : public GUI::ScrollableWidget { public: virtual ~RollWidget() override; + const KeysWidget* keys_widget() const { return m_keys_widget; } + void set_keys_widget(const KeysWidget* widget) { m_keys_widget = widget; } + private: explicit RollWidget(TrackManager&); @@ -45,6 +49,7 @@ private: virtual void mousewheel_event(GUI::MouseEvent&) override; TrackManager& m_track_manager; + const KeysWidget* m_keys_widget; int m_roll_width { 0 }; int m_num_notes { 0 }; diff --git a/Libraries/LibGfx/Color.h b/Libraries/LibGfx/Color.h index c0c5eeed78..09f4bf8529 100644 --- a/Libraries/LibGfx/Color.h +++ b/Libraries/LibGfx/Color.h @@ -117,7 +117,7 @@ public: m_value |= value; } - Color with_alpha(u8 alpha) + Color with_alpha(u8 alpha) const { return Color((m_value & 0x00ffffff) | alpha << 24); } |