Skip to content

Commit

Permalink
refactor ColourDropdownSelector to inherit from Component, implement …
Browse files Browse the repository at this point in the history
…picker button drawing
  • Loading branch information
vsicurella committed Jul 21, 2024
1 parent 783d530 commit a617f75
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
63 changes: 44 additions & 19 deletions Source/components/ColourDropdownSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,59 @@
#include "../LumatoneEditorStyleCommon.h"

ColourDropdownSelector::ColourDropdownSelector(juce::String name, bool editable)
: juce::ComboBox(name)
: juce::Component(name)
{
setEditableText(editable);
getProperties().set(LumatoneEditorStyleIDs::comboBoxEditorRestrictedChars, "0123456789ABCDEFabcdef");
getProperties().set(LumatoneEditorStyleIDs::comboBoxEditorRestrictedLength, 6);
getProperties().set(LumatoneEditorStyleIDs::comboBoxRenderColourItems, true);

onChange = [&](){ valueChangedCallback(); };
colourEditorBox = std::make_unique<juce::ComboBox>("ColourEditorBox");
colourEditorBox->setEditableText(editable);
colourEditorBox->getProperties().set(LumatoneEditorStyleIDs::comboBoxEditorRestrictedChars, "0123456789ABCDEFabcdef");
colourEditorBox->getProperties().set(LumatoneEditorStyleIDs::comboBoxEditorRestrictedLength, 6);
colourEditorBox->getProperties().set(LumatoneEditorStyleIDs::comboBoxRenderColourItems, true);
colourEditorBox->onChange = [&](){ valueChangedCallback(); };
addAndMakeVisible(colourEditorBox.get());

colourPickerButton = std::make_unique<juce::TextButton>(name + "_ColourPickerButton", juce::translate("Select colour by clicking on key"));
colourPickerButton->setButtonText("");
colourPickerButton->setClickingTogglesState(true);
colourPickerButton->onClick = [&](){ togglePickerListenForColour(colourPickerButton->getToggleState()); };
colourPickerButton->getProperties().set(LumatoneEditorStyleIDs::textButtonIconHashCode, LumatoneEditorIcon::ColourPicker);
addChildComponent(colourPickerButton.get());
}

ColourDropdownSelector::~ColourDropdownSelector()
{
colourPickerButton = nullptr;
colourEditorBox = nullptr;
}

void ColourDropdownSelector::paint(juce::Graphics &g)
{
juce::ComboBox::paint(g);

}

void ColourDropdownSelector::resized()
{
juce::ComboBox::resized();
if (showPicker)
{
colourEditorBox->setBounds(getLocalBounds().withTrimmedRight(getHeight()));
}
else
{
colourEditorBox->setBounds(getLocalBounds());
}

colourPickerButton->setBounds(getLocalBounds().withLeft(getLocalBounds().getWidth() - getHeight()));
}

void ColourDropdownSelector::setColourOptions(const juce::Array<juce::Colour> &colours)
{
clear(juce::NotificationType::dontSendNotification);
colourEditorBox->clear(juce::NotificationType::dontSendNotification);
int id = 1;
for (const juce::Colour& c : colours)
{
addItem(c.toDisplayString(false), id++);
colourEditorBox->addItem(c.toDisplayString(false), id++);
}

findIdealComboBoxNumColumns(this, getNumItems());
findIdealComboBoxNumColumns(colourEditorBox.get(), colourEditorBox->getNumItems());
}

void ColourDropdownSelector::setShowDropdown(bool show)
Expand All @@ -47,6 +66,7 @@ void ColourDropdownSelector::setShowDropdown(bool show)
void ColourDropdownSelector::setShowPicker(bool show)
{
showPicker = show;
colourPickerButton->setVisible(show);
}

void ColourDropdownSelector::setOnValueChangeCallback(std::function<void()> callbackIn)
Expand All @@ -57,9 +77,9 @@ void ColourDropdownSelector::setOnValueChangeCallback(std::function<void()> call
juce::Array<juce::Colour> ColourDropdownSelector::getColourOptions() const
{
juce::Array<juce::Colour> options;
for (int i = 0; i < getNumItems(); i++)
for (int i = 0; i < colourEditorBox->getNumItems(); i++)
{
options.add(juce::Colour::fromString("ff" + getItemText(i)));
options.add(juce::Colour::fromString("ff" + colourEditorBox->getItemText(i)));
}

return options;
Expand All @@ -77,12 +97,12 @@ juce::Colour ColourDropdownSelector::getSelectedColour()

void ColourDropdownSelector::deselectColour()
{
setSelectedId(0, juce::NotificationType::sendNotification);
colourEditorBox->setSelectedId(0, juce::NotificationType::sendNotification);
}

juce::Colour ColourDropdownSelector::parseInput() const
{
juce::String text = getText();
juce::String text = colourEditorBox->getText();

// Skip odd-numbered lengths and those less than 6 for RGB
if (text.length() % 2 == 1 || text.length() != 6)
Expand All @@ -104,15 +124,15 @@ void ColourDropdownSelector::valueChangedCallback()
{
juce::Colour selectedColour = lastSetColour;

if (getSelectedId() == 0)
if (colourEditorBox->getSelectedId() == 0)
{
juce::Colour parsedColour = parseInput();
if (parsedColour.isOpaque())
{
selectedColour = parsedColour;

// force lowercase
setText(parsedColour.toDisplayString(false), juce::NotificationType::dontSendNotification);
colourEditorBox->setText(parsedColour.toDisplayString(false), juce::NotificationType::dontSendNotification);
}
else
{
Expand All @@ -122,7 +142,7 @@ void ColourDropdownSelector::valueChangedCallback()
}
else
{
selectedColour = juce::Colour::fromString("ff" + getText());
selectedColour = juce::Colour::fromString("ff" + colourEditorBox->getText());
}

if (selectedColour != lastSetColour)
Expand All @@ -133,3 +153,8 @@ void ColourDropdownSelector::valueChangedCallback()

callback();
}

void ColourDropdownSelector::togglePickerListenForColour(bool listening)
{
colourPickerButton->setColour(LumatoneEditorColourIDs::OutlineColourId, juce::Colours::white);
}
12 changes: 6 additions & 6 deletions Source/components/ColourDropdownSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "../lumatone_editor_library/palettes/colour_selection_group.h"

class ColourDropdownSelector : public juce::ComboBox
class ColourDropdownSelector : public juce::Component
, public ColourSelectionBroadcaster
, public ColourSelectionListener
{
Expand Down Expand Up @@ -47,19 +47,19 @@ class ColourDropdownSelector : public juce::ComboBox

void valueChangedCallback();

private:

using juce::ComboBox::onChange; // don't allow public mutation
void togglePickerListenForColour(bool listening);

private:

// juce::Array<juce::Colour> colourOptions;
std::unique_ptr<juce::ComboBox> colourEditorBox;
std::unique_ptr<juce::TextButton> colourPickerButton;

juce::Colour lastSetColour;

std::function<void()> callback = [](){};

bool showDropdown = true;
bool showPicker = true;
bool showPicker = false;

};

Expand Down
1 change: 1 addition & 0 deletions Source/mapping_editors/MultiSelectControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ MultiSelectControls::MultiSelectControls(const LumatoneEditorState& stateIn)
performAction(SetKeySelectionAction::NewSetKeySelectionActionByCoords(*this, matchingKeyCoords));
});
addAndMakeVisible(colourDropdown.get());
colourDropdown->setShowPicker(true);

keyTypeCombo = std::make_unique<juce::ComboBox>("keyTypeComboSelect");
keyTypeCombo->setEditableText (false);
Expand Down

0 comments on commit a617f75

Please sign in to comment.