mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
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.
This commit is contained in:
parent
481f7d6afa
commit
8f8ae5eb53
6 changed files with 90 additions and 8 deletions
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "WidgetWithLabel.h"
|
||||
#include <AK/Vector.h>
|
||||
#include <LibDSP/ProcessorParameter.h>
|
||||
#include <LibGUI/ComboBox.h>
|
||||
#include <LibGUI/ItemListModel.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/ModelIndex.h>
|
||||
|
||||
template<typename EnumT>
|
||||
requires(IsEnum<EnumT>) class ProcessorParameterDropdown : public GUI::ComboBox {
|
||||
C_OBJECT(ProcessorParameterDropdown);
|
||||
|
||||
public:
|
||||
ProcessorParameterDropdown(LibDSP::ProcessorEnumParameter<EnumT>& parameter, Vector<String> modes)
|
||||
: ComboBox()
|
||||
, m_parameter(parameter)
|
||||
, m_modes(move(modes))
|
||||
{
|
||||
auto model = GUI::ItemListModel<EnumT, Vector<String>>::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<EnumT>(0));
|
||||
|
||||
on_change = [this]([[maybe_unused]] auto name, GUI::ModelIndex model_index) {
|
||||
auto value = static_cast<EnumT>(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<int>(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<EnumT>& m_parameter;
|
||||
Vector<String> m_modes;
|
||||
};
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Slider.h"
|
||||
#include "WidgetWithLabel.h"
|
||||
|
||||
ProcessorParameterSlider::ProcessorParameterSlider(Orientation orientation, LibDSP::ProcessorRangeParameter& parameter, RefPtr<GUI::Label> value_label)
|
||||
: Slider(orientation)
|
||||
, WidgetWithLabel(move(value_label))
|
||||
, m_parameter(parameter)
|
||||
{
|
||||
set_range(m_parameter.min_value().raw(), m_parameter.max_value().raw());
|
||||
set_value(m_parameter.value().raw());
|
||||
set_step((m_parameter.min_value() - m_parameter.max_value()).raw() / 128);
|
||||
set_tooltip(m_parameter.name());
|
||||
m_value_label->set_text(String::formatted("{:.2f}", static_cast<double>(m_parameter)));
|
||||
|
||||
on_change = [this](auto value) {
|
||||
LibDSP::ParameterFixedPoint real_value;
|
||||
real_value.raw() = value;
|
||||
m_parameter.set_value_sneaky(real_value, LibDSP::Detail::ProcessorParameterSetValueTag {});
|
||||
if (m_value_label) {
|
||||
double value = static_cast<double>(m_parameter);
|
||||
String label_text = String::formatted("{:.2f}", value);
|
||||
// FIXME: This is a magic value; we know that with normal font sizes, the label will disappear starting from approximately this length.
|
||||
// Can we do this dynamically?
|
||||
if (label_text.length() > 7)
|
||||
m_value_label->set_text(String::formatted("{:.0f}", value));
|
||||
else
|
||||
m_value_label->set_text(label_text);
|
||||
}
|
||||
};
|
||||
m_parameter.did_change_value = [this](auto value) {
|
||||
set_value(value.raw());
|
||||
};
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "WidgetWithLabel.h"
|
||||
#include <LibDSP/ProcessorParameter.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/Slider.h>
|
||||
#include <LibGfx/Orientation.h>
|
||||
|
||||
class ProcessorParameterSlider
|
||||
: public GUI::Slider
|
||||
, public WidgetWithLabel {
|
||||
C_OBJECT(ProcessorParameterSlider);
|
||||
|
||||
public:
|
||||
ProcessorParameterSlider(Orientation, LibDSP::ProcessorRangeParameter&, RefPtr<GUI::Label>);
|
||||
|
||||
protected:
|
||||
LibDSP::ProcessorRangeParameter& m_parameter;
|
||||
};
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibGUI/Label.h>
|
||||
|
||||
class WidgetWithLabel {
|
||||
public:
|
||||
WidgetWithLabel(RefPtr<GUI::Label> value_label)
|
||||
: m_value_label(move(value_label))
|
||||
{
|
||||
}
|
||||
RefPtr<GUI::Label> value_label() { return m_value_label; }
|
||||
|
||||
protected:
|
||||
RefPtr<GUI::Label> m_value_label;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue