osci-render/Source/components/EffectComponent.cpp

121 wiersze
4.2 KiB
C++
Czysty Zwykły widok Historia

#include "EffectComponent.h"
2023-07-17 13:37:36 +00:00
EffectComponent::EffectComponent(Effect& effect, int index) : effect(effect), index(index) {
addAndMakeVisible(slider);
addAndMakeVisible(selected);
effect.addListener(index, this);
setupComponent();
2023-07-04 13:58:36 +00:00
}
2023-07-17 13:37:36 +00:00
EffectComponent::EffectComponent(Effect& effect, int index, bool checkboxVisible) : EffectComponent(effect, index) {
setCheckboxVisible(checkboxVisible);
}
2023-07-17 13:37:36 +00:00
EffectComponent::EffectComponent(Effect& effect) : EffectComponent(effect, 0) {}
EffectComponent::EffectComponent(Effect& effect, bool checkboxVisible) : EffectComponent(effect) {
setCheckboxVisible(checkboxVisible);
}
void EffectComponent::setupComponent() {
EffectParameter* parameter = effect.parameters[index];
2023-07-17 13:37:36 +00:00
slider.setRange(parameter->min, parameter->max, parameter->step);
slider.setValue(parameter->getValueUnnormalised(), juce::dontSendNotification);
2023-07-17 13:37:36 +00:00
slider.setSliderStyle(juce::Slider::LinearHorizontal);
slider.setTextBoxStyle(juce::Slider::TextBoxRight, false, 90, slider.getTextBoxHeight());
bool enabled = effect.enabled == nullptr || effect.enabled->getValue();
selected.setToggleState(enabled, juce::dontSendNotification);
2023-07-17 13:37:36 +00:00
min.textBox.setValue(parameter->min, juce::dontSendNotification);
max.textBox.setValue(parameter->max, juce::dontSendNotification);
2023-07-17 13:37:36 +00:00
min.textBox.onValueChange = [this]() {
2023-07-17 14:39:21 +00:00
double minValue = min.textBox.getValue();
double maxValue = max.textBox.getValue();
if (minValue >= maxValue) {
minValue = maxValue - effect.parameters[index]->step;
2023-07-17 14:39:21 +00:00
min.textBox.setValue(minValue, juce::dontSendNotification);
}
effect.parameters[index]->min = minValue;
slider.setRange(effect.parameters[index]->min, effect.parameters[index]->max, effect.parameters[index]->step);
2023-07-17 13:37:36 +00:00
};
max.textBox.onValueChange = [this]() {
2023-07-17 14:39:21 +00:00
double minValue = min.textBox.getValue();
double maxValue = max.textBox.getValue();
if (maxValue <= minValue) {
maxValue = minValue + effect.parameters[index]->step;
2023-07-17 14:39:21 +00:00
max.textBox.setValue(maxValue, juce::dontSendNotification);
}
effect.parameters[index]->max = maxValue;
slider.setRange(effect.parameters[index]->min, effect.parameters[index]->max, effect.parameters[index]->step);
2023-07-17 13:37:36 +00:00
};
popupLabel.setText(parameter->name + " Settings", juce::dontSendNotification);
2023-07-17 13:37:36 +00:00
popupLabel.setJustificationType(juce::Justification::centred);
popupLabel.setFont(juce::Font(14.0f, juce::Font::bold));
}
2023-07-04 13:58:36 +00:00
EffectComponent::~EffectComponent() {
effect.removeListener(index, this);
}
void EffectComponent::resized() {
2023-07-06 16:57:10 +00:00
auto sliderRight = getWidth() - 160;
auto bounds = getLocalBounds();
auto componentBounds = bounds.removeFromRight(25);
if (component != nullptr) {
component->setBounds(componentBounds);
}
slider.setBounds(bounds.removeFromRight(sliderRight));
if (checkboxVisible) {
bounds.removeFromLeft(2);
selected.setBounds(bounds.removeFromLeft(25));
} else {
bounds.removeFromLeft(5);
}
textBounds = bounds;
}
void EffectComponent::paint(juce::Graphics& g) {
g.fillAll(juce::Colours::black);
g.setColour(juce::Colours::white);
g.drawText(effect.parameters[index]->name, textBounds, juce::Justification::left);
2023-07-17 13:37:36 +00:00
}
void EffectComponent::mouseDown(const juce::MouseEvent& event) {
2023-07-17 14:39:21 +00:00
if (event.mods.isPopupMenu()) {
juce::PopupMenu menu;
2023-07-17 13:37:36 +00:00
2023-07-17 14:39:21 +00:00
menu.addCustomItem(1, popupLabel, 200, 30, false);
menu.addCustomItem(2, min, 160, 40, false);
menu.addCustomItem(3, max, 160, 40, false);
2023-07-17 13:37:36 +00:00
2023-07-17 14:39:21 +00:00
menu.showMenuAsync(juce::PopupMenu::Options(), [this](int result) {});
}
}
void EffectComponent::parameterValueChanged(int parameterIndex, float newValue) {
triggerAsyncUpdate();
}
void EffectComponent::parameterGestureChanged(int parameterIndex, bool gestureIsStarting) {}
void EffectComponent::handleAsyncUpdate() {
setupComponent();
}
void EffectComponent::setComponent(std::shared_ptr<juce::Component> component) {
this->component = component;
addAndMakeVisible(component.get());
}
void EffectComponent::setCheckboxVisible(bool visible) {
checkboxVisible = visible;
}