From a7b6f55885601c0b1b7d02f85f11d729ff29cad8 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 23 Mar 2018 16:21:15 +0000 Subject: [PATCH] Ensure that the underlying var array controlled by MultiChoicePropertyComponent is sorted and that its toggle buttons are updated if the default changes --- .../juce_MultiChoicePropertyComponent.cpp | 57 ++++++++++++++----- .../juce_MultiChoicePropertyComponent.h | 2 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp index 6c41493ef7..c896d9438f 100644 --- a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp @@ -28,6 +28,21 @@ namespace juce { +//============================================================================== +class StringComparator +{ +public: + static int compareElements (var first, var second) + { + if (first.toString() > second.toString()) + return 1; + else if (first.toString() < second.toString()) + return -1; + + return 0; + } +}; + //============================================================================== class MultiChoicePropertyComponent::MultiChoiceRemapperSource : public Value::ValueSource, private Value::Listener @@ -44,21 +59,26 @@ public: { if (auto* arr = sourceValue.getValue().getArray()) if (arr->contains (varToControl)) - return 1; + return true; - return 0; + return false; } void setValue (const var& newValue) override { if (auto* arr = sourceValue.getValue().getArray()) { - auto newValueInt = static_cast (newValue); + auto temp = *arr; - if (newValueInt == 1) - arr->addIfNotAlreadyThere (varToControl); + if (static_cast (newValue)) + temp.addIfNotAlreadyThere (varToControl); else - arr->remove (arr->indexOf (varToControl)); + temp.remove (arr->indexOf (varToControl)); + + StringComparator c; + temp.sort (c); + + sourceValue = temp; } } @@ -77,7 +97,7 @@ class MultiChoicePropertyComponent::MultiChoiceRemapperSourceWithDefault : pu private Value::Listener { public: - MultiChoiceRemapperSourceWithDefault (const ValueWithDefault& vwd, var v) + MultiChoiceRemapperSourceWithDefault (ValueWithDefault& vwd, var v) : valueWithDefault (vwd), sourceValue (valueWithDefault.getPropertyAsValue()), varToControl (v) @@ -89,26 +109,31 @@ public: { if (auto* arr = valueWithDefault.get().getArray()) if (arr->contains (varToControl)) - return 1; + return true; - return 0; + return false; } void setValue (const var& newValue) override { if (auto* arr = valueWithDefault.get().getArray()) { - auto newValueInt = static_cast (newValue); + auto temp = *arr; - if (newValueInt == 1) - arr->addIfNotAlreadyThere (varToControl); + if (static_cast (newValue)) + temp.addIfNotAlreadyThere (varToControl); else - arr->remove (arr->indexOf (varToControl)); + temp.remove (arr->indexOf (varToControl)); + + StringComparator c; + temp.sort (c); + + valueWithDefault = temp; } } private: - ValueWithDefault valueWithDefault; + ValueWithDefault& valueWithDefault; Value sourceValue; var varToControl; @@ -161,7 +186,7 @@ MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueTo correspondingValues[i]))); } -MultiChoicePropertyComponent::MultiChoicePropertyComponent (const ValueWithDefault& valueToControl, +MultiChoicePropertyComponent::MultiChoicePropertyComponent (ValueWithDefault& valueToControl, const String& propertyName, const StringArray& choices, const Array& correspondingValues) @@ -173,6 +198,8 @@ MultiChoicePropertyComponent::MultiChoicePropertyComponent (const ValueWithDefau for (int i = 0; i < choiceButtons.size(); ++i) choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueToControl, correspondingValues[i]))); + + valueToControl.onDefaultChange = [this] { repaint(); }; } void MultiChoicePropertyComponent::paint (Graphics& g) diff --git a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h index b44805517e..ef19c53967 100644 --- a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h @@ -67,7 +67,7 @@ public: valueToControl value. This array must contain the same number of items as the choices array */ - MultiChoicePropertyComponent (const ValueWithDefault& valueToControl, + MultiChoicePropertyComponent (ValueWithDefault& valueToControl, const String& propertyName, const StringArray& choices, const Array& correspondingValues);