From 8f8ae5eb53bfff1b4f0d15e0013cab41208b2bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 28 Sep 2021 18:00:41 +0200 Subject: [PATCH] Piano: Create controller widgets for processor parameters These widgets attach to a processor parameter and keep the two sides in sync. They will become very useful for smart processor interfaces. --- Userland/Applications/Piano/CMakeLists.txt | 2 +- Userland/Applications/Piano/KnobsWidget.h | 2 +- .../Piano/ProcessorParameterWidget/Dropdown.h | 58 +++++++++++++++++++ .../Slider.cpp} | 5 +- .../Slider.h} | 9 +-- .../WidgetWithLabel.h | 22 +++++++ 6 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 Userland/Applications/Piano/ProcessorParameterWidget/Dropdown.h rename Userland/Applications/Piano/{ProcessorParameterSlider.cpp => ProcessorParameterWidget/Slider.cpp} (94%) rename Userland/Applications/Piano/{ProcessorParameterSlider.h => ProcessorParameterWidget/Slider.h} (73%) create mode 100644 Userland/Applications/Piano/ProcessorParameterWidget/WidgetWithLabel.h diff --git a/Userland/Applications/Piano/CMakeLists.txt b/Userland/Applications/Piano/CMakeLists.txt index 9b86a99345..19717985e8 100644 --- a/Userland/Applications/Piano/CMakeLists.txt +++ b/Userland/Applications/Piano/CMakeLists.txt @@ -17,7 +17,7 @@ set(SOURCES RollWidget.cpp SamplerWidget.cpp WaveWidget.cpp - ProcessorParameterSlider.cpp + ProcessorParameterWidget/Slider.cpp ) serenity_app(Piano ICON app-piano) diff --git a/Userland/Applications/Piano/KnobsWidget.h b/Userland/Applications/Piano/KnobsWidget.h index 7fa6b6b8e7..48c4c5f364 100644 --- a/Userland/Applications/Piano/KnobsWidget.h +++ b/Userland/Applications/Piano/KnobsWidget.h @@ -7,7 +7,7 @@ #pragma once -#include "ProcessorParameterSlider.h" +#include "ProcessorParameterWidget/Slider.h" #include #include diff --git a/Userland/Applications/Piano/ProcessorParameterWidget/Dropdown.h b/Userland/Applications/Piano/ProcessorParameterWidget/Dropdown.h new file mode 100644 index 0000000000..2394ae1f51 --- /dev/null +++ b/Userland/Applications/Piano/ProcessorParameterWidget/Dropdown.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021, kleines Filmröllchen . + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include "WidgetWithLabel.h" +#include +#include +#include +#include +#include +#include + +template +requires(IsEnum) class ProcessorParameterDropdown : public GUI::ComboBox { + C_OBJECT(ProcessorParameterDropdown); + +public: + ProcessorParameterDropdown(LibDSP::ProcessorEnumParameter& parameter, Vector modes) + : ComboBox() + , m_parameter(parameter) + , m_modes(move(modes)) + { + auto model = GUI::ItemListModel>::create(m_modes); + set_model(model); + set_only_allow_values_from_model(true); + set_model_column(0); + set_selected_index(0); + m_parameter.set_value(static_cast(0)); + + on_change = [this]([[maybe_unused]] auto name, GUI::ModelIndex model_index) { + auto value = static_cast(model_index.row()); + m_parameter.set_value_sneaky(value, LibDSP::Detail::ProcessorParameterSetValueTag {}); + }; + m_parameter.did_change_value = [this](auto new_value) { + set_selected_index(static_cast(new_value)); + }; + } + + // Release focus when escape is pressed + virtual void keydown_event(GUI::KeyEvent& event) override + { + if (event.key() == Key_Escape) { + if (is_focused()) + set_focus(false); + event.accept(); + } else { + GUI::ComboBox::keydown_event(event); + } + } + +private: + LibDSP::ProcessorEnumParameter& m_parameter; + Vector m_modes; +}; diff --git a/Userland/Applications/Piano/ProcessorParameterSlider.cpp b/Userland/Applications/Piano/ProcessorParameterWidget/Slider.cpp similarity index 94% rename from Userland/Applications/Piano/ProcessorParameterSlider.cpp rename to Userland/Applications/Piano/ProcessorParameterWidget/Slider.cpp index e83648f48c..d76c638739 100644 --- a/Userland/Applications/Piano/ProcessorParameterSlider.cpp +++ b/Userland/Applications/Piano/ProcessorParameterWidget/Slider.cpp @@ -4,12 +4,13 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "ProcessorParameterSlider.h" +#include "Slider.h" +#include "WidgetWithLabel.h" ProcessorParameterSlider::ProcessorParameterSlider(Orientation orientation, LibDSP::ProcessorRangeParameter& parameter, RefPtr value_label) : Slider(orientation) + , WidgetWithLabel(move(value_label)) , m_parameter(parameter) - , m_value_label(move(value_label)) { set_range(m_parameter.min_value().raw(), m_parameter.max_value().raw()); set_value(m_parameter.value().raw()); diff --git a/Userland/Applications/Piano/ProcessorParameterSlider.h b/Userland/Applications/Piano/ProcessorParameterWidget/Slider.h similarity index 73% rename from Userland/Applications/Piano/ProcessorParameterSlider.h rename to Userland/Applications/Piano/ProcessorParameterWidget/Slider.h index 22ed68341e..8093279939 100644 --- a/Userland/Applications/Piano/ProcessorParameterSlider.h +++ b/Userland/Applications/Piano/ProcessorParameterWidget/Slider.h @@ -6,19 +6,20 @@ #pragma once +#include "WidgetWithLabel.h" #include #include #include #include -class ProcessorParameterSlider : public GUI::Slider { +class ProcessorParameterSlider + : public GUI::Slider + , public WidgetWithLabel { C_OBJECT(ProcessorParameterSlider); public: ProcessorParameterSlider(Orientation, LibDSP::ProcessorRangeParameter&, RefPtr); - RefPtr value_label() { return m_value_label; } -private: +protected: LibDSP::ProcessorRangeParameter& m_parameter; - RefPtr m_value_label; }; diff --git a/Userland/Applications/Piano/ProcessorParameterWidget/WidgetWithLabel.h b/Userland/Applications/Piano/ProcessorParameterWidget/WidgetWithLabel.h new file mode 100644 index 0000000000..9286cb29bb --- /dev/null +++ b/Userland/Applications/Piano/ProcessorParameterWidget/WidgetWithLabel.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021, kleines Filmröllchen . + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +class WidgetWithLabel { +public: + WidgetWithLabel(RefPtr value_label) + : m_value_label(move(value_label)) + { + } + RefPtr value_label() { return m_value_label; } + +protected: + RefPtr m_value_label; +};