Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
326 lines (256 sloc) 9.84 KB
/*
==============================================================================
LabeledComponent.cpp
Created: 25 Nov 2018 3:28:40pm
Author: Output Dev Laptop 2
==============================================================================
*/
#include "LabeledComponent.h"
#include "GlobalConstants.h"
LabeledComponent::LabeledComponent(LabeledComponentStyle inStyle,
AudioProcessorValueTreeState& state,
const String& parameterId)
: mStyle(inStyle)
{
switch(mStyle)
{
case(kLabeledComponentStyle_Button):{
constructButton(state, parameterId);
} break;
case(kLabeledComponentStyle_Slider):{
constructSlider(state, parameterId);
} break;
case(kLabeledComponentStyle_ComboBox):{
constructComboBox(state, parameterId);
} break;
}
setText(mName);
setTextLabelPosition(Justification::centredTop);
setSize(componentWidth, componentHeight);
setLookAndFeel(&labeledComponentLookAndFeel); //Sets lookand feel for labeledslider, label, and group
}
LabeledComponent::~LabeledComponent()
{
switch(mStyle)
{
case(kLabeledComponentStyle_Button):{
mButton->setLookAndFeel(nullptr);
} break;
case(kLabeledComponentStyle_Slider):{
mSlider->setLookAndFeel(nullptr);
} break;
case(kLabeledComponentStyle_ComboBox):{
mComboBox->setLookAndFeel(nullptr);
} break;
}
setLookAndFeel(nullptr);
}
void LabeledComponent::resized()
{
switch(mStyle)
{
case(kLabeledComponentStyle_Button):{
buttonResized();
} break;
case(kLabeledComponentStyle_Slider):{
sliderResized();
} break;
case(kLabeledComponentStyle_ComboBox):{
comboBoxResized();
} break;
}
}
void LabeledComponent::sliderValueChanged (Slider* slider)
{
// updateLabelText();
}
void LabeledComponent::labelTextChanged (Label* labelThatHasChanged)
{
const float value = labelThatHasChanged->getText().getFloatValue();
if(mSlider){
mSlider->setValue(value, sendNotification);
} else {
// slider doesn't exist!
jassertfalse;
}
}
void LabeledComponent::buttonClicked(Button* button)
{
/*if (button->getState() = true) {
mButton->setState false;
}
else {}
}*/
// do something..
}
void LabeledComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
// do something..
}
void LabeledComponent::setNumDecimalPlacesToDisplay(int decimals)
{
mNumberOfDecimalsToDisplay = decimals;
updateLabelText();
}
void LabeledComponent::setInterval(float inNewInterval)
{
Slider* slider = mSlider.get();
if(slider){
slider->setRange (mRange.getRange().getStart(),
mRange.getRange().getEnd(),
inNewInterval);
} else {
// what are you doing, this is not a slider!
jassertfalse;
}
}
void LabeledComponent::setSkewFactorFromMidPoint(double skewMidPoint)
{
Slider* slider = mSlider.get();
if (slider) {
slider->setSkewFactorFromMidPoint(skewMidPoint);
}
else {
// what are you doing, this is null!
jassertfalse;
}
}
void LabeledComponent::constructSlider(AudioProcessorValueTreeState& state,
const String& parameterId)
{
mParameter =
state.getParameter(parameterId);
mName = mParameter->name;
const float defaultValue = mParameter->getValue();
mRange = state.getParameterRange(parameterId);
// DBG("defaultValue: " << defaultValue);
Slider* slider = new Slider();
slider->setName(mName);
slider->setTextBoxStyle(Slider::TextEntryBoxPosition::NoTextBox, false, 0, 0);
slider->setSliderStyle(Slider::SliderStyle::RotaryVerticalDrag); // OR RotaryHorizontalVerticalDrag
slider->setValue(defaultValue, sendNotification);
slider->setDoubleClickReturnValue(true, defaultValue);
slider->setScrollWheelEnabled(false);
slider->setRange(mRange.getRange().getStart(), mRange.getRange().getEnd(), 0.001);
slider->addListener(this);
mSlider = std::unique_ptr<Slider>(slider);
addAndMakeVisible(slider);
mLabel = std::unique_ptr<Label>(new Label());
mLabel->setEditable(true);
mLabel->addListener(this);
addAndMakeVisible(mLabel.get());
AudioProcessorValueTreeState::SliderAttachment* attachment =
new AudioProcessorValueTreeState::SliderAttachment(state, parameterId, *slider);
mSliderAttachment = std::unique_ptr<AudioProcessorValueTreeState::SliderAttachment>(attachment);
}
void LabeledComponent::constructButton(AudioProcessorValueTreeState& state,
const String& parameterId)
{
mParameter =
state.getParameter(parameterId);
mName = mParameter->name;
mRange = state.getParameterRange(parameterId);
const bool defaultValue = (bool)mParameter->getValue();
TextButton* button = new TextButton(); //TextButton(mName); for having button named.
button->setClickingTogglesState(true);
button->setToggleState(defaultValue, dontSendNotification);
mButton = std::unique_ptr<TextButton>(button);
addAndMakeVisible(button);
AudioProcessorValueTreeState::ButtonAttachment* attachment =
new AudioProcessorValueTreeState::ButtonAttachment(state, parameterId, *button);
mButtonAttachment = std::unique_ptr<AudioProcessorValueTreeState::ButtonAttachment>(attachment);
}
void LabeledComponent::constructComboBox(AudioProcessorValueTreeState& state,
const String& parameterId)
{
mParameter =
state.getParameter(parameterId);
mName = mParameter->name;
mRange = state.getParameterRange(parameterId);
const int defaultValue = mParameter->getValue() * mRange.getRange().getLength();
ComboBox* combo = new ComboBox();
combo->setEditableText(false);
combo->setJustificationType(Justification::centred);
combo->setSelectedItemIndex(defaultValue, dontSendNotification);
mComboBox = std::unique_ptr<ComboBox>(combo);
addAndMakeVisible(combo);
AudioProcessorValueTreeState::ComboBoxAttachment* attachment =
new AudioProcessorValueTreeState::ComboBoxAttachment(state, parameterId, *combo);
mComboBoxAttachment = std::unique_ptr<AudioProcessorValueTreeState::ComboBoxAttachment>(attachment);
}
void LabeledComponent::sliderResized()
{
juce::Rectangle<int> sliderBounds = juce::Rectangle<int>(0, 0, getWidth(), getHeight()).reduced(componentMargin);
//NEED TO CREATE ANOTHER RECTANGLE eg. knobBounds that is derived from a percent of sliderbounds
juce::Rectangle<int> knobBounds = juce::Rectangle<int>(componentMargin,
componentMargin,
sliderBounds.getWidth(),
(sliderBounds.getHeight() - textBoxHeight)*0.9);
juce::Rectangle<int> labelBounds = juce::Rectangle<int> ((sliderBounds.getWidth() * 0.5) - (textBoxWidth * 0.5) + componentMargin,
(sliderBounds.getHeight() - textBoxHeight)*0.95,
textBoxWidth,
textBoxHeight);
Slider* slider = mSlider.get();
slider->setBounds(knobBounds);
Label* label = mLabel.get();
label->setBounds(labelBounds);
}
void LabeledComponent::buttonResized()
{
juce::Rectangle<int> generalBounds = juce::Rectangle<int>(0, 0, getWidth(), getHeight()).reduced(componentMargin);
float buttonWidthPercent = 0.7f;
float buttonHeight = 16.0f;
float buttonWidth = 22.0;
float buttonVerticalShift = 2.0f;
juce::Rectangle<int> buttonBounds = juce::Rectangle<int>(
//(generalBounds.getWidth() * 0.5) - (generalBounds.getWidth() * buttonWidthPercent * 0.5) + componentMargin,
(generalBounds.getWidth() * 0.5) - (buttonWidth * 0.5) + componentMargin,
(generalBounds.getHeight() * 0.5 - (buttonHeight * 0.5)) + componentMargin + buttonVerticalShift,
//generalBounds.getWidth() * buttonWidthPercent,
buttonWidth,
buttonHeight);
TextButton* button = mButton.get();
button->setBounds(buttonBounds);
}
void LabeledComponent::comboBoxResized()
{
juce::Rectangle<int> generalBounds = juce::Rectangle<int>(0, 0, getWidth(), getHeight()).reduced(componentMargin);
float comboWidthPercent = 0.9f;
float comboHeight = 32.f; //16
float comboWidth = 45.f;
float comboVerticalShift = 2.0f;
juce::Rectangle<int> comboBoxBounds = juce::Rectangle<int>(
(generalBounds.getWidth() * 0.5) - (comboWidth * 0.5) + componentMargin,
(generalBounds.getHeight() * 0.5 - (comboHeight * 0.5)) + componentMargin + comboVerticalShift,
comboWidth,
comboHeight);
ComboBox* combo = mComboBox.get();
combo->setBounds(comboBoxBounds);
}
void LabeledComponent::updateLabelText()
{
String value = String(mSlider->getValue(),
mNumberOfDecimalsToDisplay);
DBG("parameter: "
<< mName
<< " - value: "
<< value);
mLabel->setText(value, dontSendNotification);
}
void LabeledComponent::setComboBoxItems(StringArray inItemsArray, bool inShouldUpdate)
{
ComboBox* combo = mComboBox.get();
if(combo){
// set items
combo->addItemList(inItemsArray, 1);
// update
if(inShouldUpdate){
const int value = mParameter->getValue() * mRange.getRange().getLength();
combo->setSelectedItemIndex(value,
dontSendNotification);
}
} else {
// what are you doing!
jassertfalse;
}
}