summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2023-02-09 15:26:26 +0100
committerAndrew Kaster <andrewdkaster@gmail.com>2023-02-25 20:49:41 -0700
commit7b3b743f888b57e9bb07867e7fe41fd1eb5a737a (patch)
tree38eda451d1ba49bfac1ea8eb406823895bcfab2d
parent376e7243a95fa19c0f425a77d1cff648f9536647 (diff)
downloadserenity-7b3b743f888b57e9bb07867e7fe41fd1eb5a737a.zip
Piano: Move octave controls into main widget
This is not related to the track controls and it may move into another separate widget in the future. The move also allows to simplify the octave slider callback logic.
-rw-r--r--Userland/Applications/Piano/MainWidget.cpp36
-rw-r--r--Userland/Applications/Piano/MainWidget.h9
-rw-r--r--Userland/Applications/Piano/TrackControlsWidget.cpp33
-rw-r--r--Userland/Applications/Piano/TrackControlsWidget.h8
4 files changed, 36 insertions, 50 deletions
diff --git a/Userland/Applications/Piano/MainWidget.cpp b/Userland/Applications/Piano/MainWidget.cpp
index db990ef11e..e08423e269 100644
--- a/Userland/Applications/Piano/MainWidget.cpp
+++ b/Userland/Applications/Piano/MainWidget.cpp
@@ -17,6 +17,7 @@
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Menu.h>
+#include <LibGUI/Slider.h>
#include <LibGUI/TabWidget.h>
ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create(TrackManager& manager, AudioPlayerLoop& loop)
@@ -55,6 +56,28 @@ ErrorOr<void> MainWidget::initialize()
m_keys_widget = TRY(m_keys_and_knobs_container->try_add<KeysWidget>(m_track_manager.keyboard()));
+ m_octave_container = TRY(m_keys_and_knobs_container->try_add<GUI::Widget>());
+ m_octave_container->set_preferred_width(GUI::SpecialDimension::Fit);
+ TRY(m_octave_container->try_set_layout<GUI::VerticalBoxLayout>());
+ auto octave_label = TRY(m_octave_container->try_add<GUI::Label>("Octave"));
+ octave_label->set_preferred_width(GUI::SpecialDimension::Fit);
+ m_octave_value = TRY(m_octave_container->try_add<GUI::Label>(DeprecatedString::number(m_track_manager.keyboard()->virtual_keyboard_octave())));
+ m_octave_value->set_preferred_width(GUI::SpecialDimension::Fit);
+
+ // FIXME: Implement vertical flipping in GUI::Slider, not here.
+ m_octave_knob = TRY(m_octave_container->try_add<GUI::VerticalSlider>());
+ m_octave_knob->set_preferred_width(GUI::SpecialDimension::Fit);
+ m_octave_knob->set_tooltip("Z: octave down, X: octave up");
+ m_octave_knob->set_range(octave_min - 1, octave_max - 1);
+ m_octave_knob->set_value((octave_max - 1) - (m_track_manager.keyboard()->virtual_keyboard_octave() - 1));
+ m_octave_knob->set_step(1);
+ m_octave_knob->on_change = [this](int value) {
+ int new_octave = octave_max - value;
+ set_octave_via_slider(new_octave);
+ VERIFY(new_octave == m_track_manager.keyboard()->virtual_keyboard_octave());
+ m_octave_value->set_text(DeprecatedString::number(new_octave));
+ };
+
m_knobs_widget = TRY(m_keys_and_knobs_container->try_add<TrackControlsWidget>(m_track_manager, *this));
m_roll_widget->set_keys_widget(m_keys_widget);
@@ -72,8 +95,6 @@ ErrorOr<void> MainWidget::add_track_actions(GUI::Menu& menu)
turn_off_pressed_keys();
m_player_widget->next_track();
turn_on_pressed_keys();
-
- m_knobs_widget->update_knobs();
})));
return {};
@@ -131,10 +152,10 @@ bool MainWidget::special_key_action(int key_code)
{
switch (key_code) {
case Key_Z:
- set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Down);
+ change_octave_via_keys(DSP::Keyboard::Direction::Down);
return true;
case Key_X:
- set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Up);
+ change_octave_via_keys(DSP::Keyboard::Direction::Up);
return true;
case Key_Space:
m_player_widget->toggle_paused();
@@ -164,22 +185,21 @@ void MainWidget::turn_on_pressed_keys()
}
}
-void MainWidget::set_octave_and_ensure_note_change(int octave)
+void MainWidget::set_octave_via_slider(int octave)
{
turn_off_pressed_keys();
MUST(m_track_manager.keyboard()->set_virtual_keyboard_octave(octave));
turn_on_pressed_keys();
- m_knobs_widget->update_knobs();
m_keys_widget->update();
}
-void MainWidget::set_octave_and_ensure_note_change(DSP::Keyboard::Direction direction)
+void MainWidget::change_octave_via_keys(DSP::Keyboard::Direction direction)
{
turn_off_pressed_keys();
m_track_manager.keyboard()->change_virtual_keyboard_octave(direction);
turn_on_pressed_keys();
- m_knobs_widget->update_knobs();
+ m_octave_knob->set_value(octave_max - m_track_manager.keyboard()->virtual_keyboard_octave());
m_keys_widget->update();
}
diff --git a/Userland/Applications/Piano/MainWidget.h b/Userland/Applications/Piano/MainWidget.h
index 2ec91dfc7f..197198b16a 100644
--- a/Userland/Applications/Piano/MainWidget.h
+++ b/Userland/Applications/Piano/MainWidget.h
@@ -30,8 +30,8 @@ public:
ErrorOr<void> add_track_actions(GUI::Menu&);
- void set_octave_and_ensure_note_change(DSP::Keyboard::Direction);
- void set_octave_and_ensure_note_change(int);
+ void change_octave_via_keys(DSP::Keyboard::Direction);
+ void set_octave_via_slider(int octave);
private:
explicit MainWidget(TrackManager&, AudioPlayerLoop&);
@@ -60,6 +60,11 @@ private:
RefPtr<TrackControlsWidget> m_knobs_widget;
RefPtr<PlayerWidget> m_player_widget;
+ RefPtr<GUI::Widget> m_octave_container;
+ RefPtr<GUI::Slider> m_octave_knob;
+ RefPtr<GUI::Label> m_octave_value;
+ bool m_octave_change_in_progress { false };
+
// Not the piano keys, but the computer keyboard keys!
bool m_keys_pressed[key_code_count] { false };
};
diff --git a/Userland/Applications/Piano/TrackControlsWidget.cpp b/Userland/Applications/Piano/TrackControlsWidget.cpp
index f1d904940e..8479559e31 100644
--- a/Userland/Applications/Piano/TrackControlsWidget.cpp
+++ b/Userland/Applications/Piano/TrackControlsWidget.cpp
@@ -13,7 +13,6 @@
#include <LibDSP/ProcessorParameter.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Label.h>
-#include <LibGUI/Slider.h>
#include <LibGfx/Orientation.h>
TrackControlsWidget::TrackControlsWidget(TrackManager& track_manager, MainWidget& main_widget)
@@ -21,27 +20,9 @@ TrackControlsWidget::TrackControlsWidget(TrackManager& track_manager, MainWidget
, m_main_widget(main_widget)
{
set_layout<GUI::HorizontalBoxLayout>();
+ set_preferred_width(GUI::SpecialDimension::Grow);
set_fill_with_background_color(true);
- m_octave_container = add<GUI::Widget>();
- m_octave_container->set_layout<GUI::VerticalBoxLayout>();
- m_octave_container->add<GUI::Label>("Octave");
- m_octave_value = m_octave_container->add<GUI::Label>(DeprecatedString::number(m_track_manager.keyboard()->virtual_keyboard_octave()));
-
- // FIXME: Implement vertical flipping in GUI::Slider, not here.
- m_octave_knob = m_octave_container->add<GUI::VerticalSlider>();
- m_octave_knob->set_tooltip("Z: octave down, X: octave up");
- m_octave_knob->set_range(octave_min - 1, octave_max - 1);
- m_octave_knob->set_value((octave_max - 1) - (m_track_manager.keyboard()->virtual_keyboard_octave() - 1));
- m_octave_knob->set_step(1);
- m_octave_knob->on_change = [this](int value) {
- int new_octave = octave_max - value;
- if (m_change_underlying)
- m_main_widget.set_octave_and_ensure_note_change(new_octave);
- VERIFY(new_octave == m_track_manager.keyboard()->virtual_keyboard_octave());
- m_octave_value->set_text(DeprecatedString::number(new_octave));
- };
-
for (auto& parameter : m_track_manager.current_track()->track_mastering()->parameters())
m_parameter_widgets.append(add<ProcessorParameterWidget>(parameter));
@@ -51,15 +32,3 @@ TrackControlsWidget::TrackControlsWidget(TrackManager& track_manager, MainWidget
for (auto& parameter : m_track_manager.current_track()->delay()->parameters())
m_parameter_widgets.append(add<ProcessorParameterWidget>(parameter));
}
-
-void TrackControlsWidget::update_knobs()
-{
- // FIXME: This is needed because when the slider is changed normally, we
- // need to change the underlying value, but if the keyboard was used, we
- // need to change the slider without changing the underlying value.
- m_change_underlying = false;
-
- m_octave_knob->set_value(octave_max - m_track_manager.keyboard()->virtual_keyboard_octave());
-
- m_change_underlying = true;
-}
diff --git a/Userland/Applications/Piano/TrackControlsWidget.h b/Userland/Applications/Piano/TrackControlsWidget.h
index ff0fbdb560..6076e8c231 100644
--- a/Userland/Applications/Piano/TrackControlsWidget.h
+++ b/Userland/Applications/Piano/TrackControlsWidget.h
@@ -24,19 +24,11 @@ class TrackControlsWidget final : public GUI::Frame {
public:
virtual ~TrackControlsWidget() override = default;
- void update_knobs();
-
private:
TrackControlsWidget(TrackManager&, MainWidget&);
TrackManager& m_track_manager;
MainWidget& m_main_widget;
- RefPtr<GUI::Widget> m_octave_container;
- RefPtr<GUI::Slider> m_octave_knob;
- RefPtr<GUI::Label> m_octave_value;
-
NonnullRefPtrVector<ProcessorParameterWidget> m_parameter_widgets;
-
- bool m_change_underlying { true };
};