Piano: Highlight pressed key in roll widget

This commit is contained in:
Peter Elliott 2020-10-05 20:31:01 -07:00 committed by Andreas Kling
parent 0c5497829e
commit 01bff0141e
Notes: sideshowbarker 2024-07-19 01:55:52 +09:00
6 changed files with 25 additions and 1 deletions

View file

@ -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) {

View file

@ -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&);

View file

@ -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()

View file

@ -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);
}

View file

@ -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 };

View file

@ -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);
}