From a617f753e2b4f8cdc81bba38a9f8ce6714046e40 Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Sun, 21 Jul 2024 17:07:11 -0400 Subject: [PATCH] refactor ColourDropdownSelector to inherit from Component, implement picker button drawing --- Source/components/ColourDropdownSelector.cpp | 63 +++++++++++++------ Source/components/ColourDropdownSelector.h | 12 ++-- .../mapping_editors/MultiSelectControls.cpp | 1 + 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/Source/components/ColourDropdownSelector.cpp b/Source/components/ColourDropdownSelector.cpp index 949a619..45dac58 100644 --- a/Source/components/ColourDropdownSelector.cpp +++ b/Source/components/ColourDropdownSelector.cpp @@ -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("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(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 &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) @@ -47,6 +66,7 @@ void ColourDropdownSelector::setShowDropdown(bool show) void ColourDropdownSelector::setShowPicker(bool show) { showPicker = show; + colourPickerButton->setVisible(show); } void ColourDropdownSelector::setOnValueChangeCallback(std::function callbackIn) @@ -57,9 +77,9 @@ void ColourDropdownSelector::setOnValueChangeCallback(std::function call juce::Array ColourDropdownSelector::getColourOptions() const { juce::Array 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; @@ -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) @@ -104,7 +124,7 @@ void ColourDropdownSelector::valueChangedCallback() { juce::Colour selectedColour = lastSetColour; - if (getSelectedId() == 0) + if (colourEditorBox->getSelectedId() == 0) { juce::Colour parsedColour = parseInput(); if (parsedColour.isOpaque()) @@ -112,7 +132,7 @@ void ColourDropdownSelector::valueChangedCallback() selectedColour = parsedColour; // force lowercase - setText(parsedColour.toDisplayString(false), juce::NotificationType::dontSendNotification); + colourEditorBox->setText(parsedColour.toDisplayString(false), juce::NotificationType::dontSendNotification); } else { @@ -122,7 +142,7 @@ void ColourDropdownSelector::valueChangedCallback() } else { - selectedColour = juce::Colour::fromString("ff" + getText()); + selectedColour = juce::Colour::fromString("ff" + colourEditorBox->getText()); } if (selectedColour != lastSetColour) @@ -133,3 +153,8 @@ void ColourDropdownSelector::valueChangedCallback() callback(); } + +void ColourDropdownSelector::togglePickerListenForColour(bool listening) +{ + colourPickerButton->setColour(LumatoneEditorColourIDs::OutlineColourId, juce::Colours::white); +} diff --git a/Source/components/ColourDropdownSelector.h b/Source/components/ColourDropdownSelector.h index f44bbc1..e39ce43 100644 --- a/Source/components/ColourDropdownSelector.h +++ b/Source/components/ColourDropdownSelector.h @@ -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 { @@ -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 colourOptions; + std::unique_ptr colourEditorBox; + std::unique_ptr colourPickerButton; juce::Colour lastSetColour; std::function callback = [](){}; bool showDropdown = true; - bool showPicker = true; + bool showPicker = false; }; diff --git a/Source/mapping_editors/MultiSelectControls.cpp b/Source/mapping_editors/MultiSelectControls.cpp index 6baa51a..78f3348 100644 --- a/Source/mapping_editors/MultiSelectControls.cpp +++ b/Source/mapping_editors/MultiSelectControls.cpp @@ -46,6 +46,7 @@ MultiSelectControls::MultiSelectControls(const LumatoneEditorState& stateIn) performAction(SetKeySelectionAction::NewSetKeySelectionActionByCoords(*this, matchingKeyCoords)); }); addAndMakeVisible(colourDropdown.get()); + colourDropdown->setShowPicker(true); keyTypeCombo = std::make_unique("keyTypeComboSelect"); keyTypeCombo->setEditableText (false);