Browse Source

Ensure that the underlying var array controlled by MultiChoicePropertyComponent is sorted and that its toggle buttons are updated if the default changes

tags/2021-05-28
ed 7 years ago
parent
commit
a7b6f55885
2 changed files with 43 additions and 16 deletions
  1. +42
    -15
      modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp
  2. +1
    -1
      modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h

+ 42
- 15
modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp View File

@@ -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<int> (newValue);
auto temp = *arr;
if (newValueInt == 1)
arr->addIfNotAlreadyThere (varToControl);
if (static_cast<bool> (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<int> (newValue);
auto temp = *arr;
if (newValueInt == 1)
arr->addIfNotAlreadyThere (varToControl);
if (static_cast<bool> (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<var>& 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)


+ 1
- 1
modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h View File

@@ -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<var>& correspondingValues);


Loading…
Cancel
Save