1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Update property component constructors to allow temporary ValueTreePropertyWithDefault objects

This commit is contained in:
ed 2021-12-10 15:00:32 +00:00
parent dea0f1638b
commit 1de36db60a
6 changed files with 87 additions and 126 deletions

View file

@ -27,11 +27,11 @@ namespace juce
{
//==============================================================================
class ChoicePropertyComponent::RemapperValueSource : public Value::ValueSource,
private Value::Listener
class ChoiceRemapperValueSource : public Value::ValueSource,
private Value::Listener
{
public:
RemapperValueSource (const Value& source, const Array<var>& map)
ChoiceRemapperValueSource (const Value& source, const Array<var>& map)
: sourceValue (source),
mappings (map)
{
@ -64,17 +64,17 @@ protected:
void valueChanged (Value&) override { sendChangeMessage (true); }
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceRemapperValueSource)
};
//==============================================================================
class ChoicePropertyComponent::RemapperValueSourceWithDefault : public Value::ValueSource,
private Value::Listener
class ChoiceRemapperValueSourceWithDefault : public Value::ValueSource,
private Value::Listener
{
public:
RemapperValueSourceWithDefault (ValueWithDefault* vwd, const Array<var>& map)
: valueWithDefault (vwd),
sourceValue (valueWithDefault->getPropertyAsValue()),
ChoiceRemapperValueSourceWithDefault (const ValueTreePropertyWithDefault& v, const Array<var>& map)
: value (v),
sourceValue (value.getPropertyAsValue()),
mappings (map)
{
sourceValue.addListener (this);
@ -82,7 +82,7 @@ public:
var getValue() const override
{
if (valueWithDefault != nullptr && ! valueWithDefault->isUsingDefault())
if (! value.isUsingDefault())
{
const auto target = sourceValue.getValue();
const auto equalsWithSameType = [&target] (const var& map) { return map.equalsWithSameType (target); };
@ -101,33 +101,30 @@ public:
void setValue (const var& newValue) override
{
if (valueWithDefault == nullptr)
return;
auto newValueInt = static_cast<int> (newValue);
if (newValueInt == -1)
{
valueWithDefault->resetToDefault();
value.resetToDefault();
}
else
{
auto remappedVal = mappings [newValueInt - 1];
if (! remappedVal.equalsWithSameType (sourceValue))
*valueWithDefault = remappedVal;
value = remappedVal;
}
}
private:
void valueChanged (Value&) override { sendChangeMessage (true); }
WeakReference<ValueWithDefault> valueWithDefault;
ValueTreePropertyWithDefault value;
Value sourceValue;
Array<var> mappings;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSourceWithDefault)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceRemapperValueSourceWithDefault)
};
//==============================================================================
@ -155,23 +152,23 @@ ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
: ChoicePropertyComponent (name, choiceList, correspondingValues)
{
refreshChoices();
initialiseComboBox (Value (new RemapperValueSource (valueToControl, correspondingValues)));
initialiseComboBox (Value (new ChoiceRemapperValueSource (valueToControl, correspondingValues)));
}
ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToControl,
ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
const String& name,
const StringArray& choiceList,
const Array<var>& correspondingValues)
: ChoicePropertyComponent (name, choiceList, correspondingValues)
{
valueWithDefault = &valueToControl;
value = valueToControl;
auto getDefaultString = [this, correspondingValues] { return choices [correspondingValues.indexOf (valueWithDefault->getDefault())]; };
auto getDefaultString = [this, correspondingValues] { return choices [correspondingValues.indexOf (value.getDefault())]; };
refreshChoices (getDefaultString());
initialiseComboBox (Value (new RemapperValueSourceWithDefault (valueWithDefault, correspondingValues)));
initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, correspondingValues)));
valueWithDefault->onDefaultChange = [this, getDefaultString]
value.onDefaultChange = [this, getDefaultString]
{
auto selectedId = comboBox.getSelectedId();
refreshChoices (getDefaultString());
@ -179,19 +176,19 @@ ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToContr
};
}
ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToControl,
ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
const String& name)
: PropertyComponent (name),
choices ({ "Enabled", "Disabled" })
{
valueWithDefault = &valueToControl;
value = valueToControl;
auto getDefaultString = [this] { return valueWithDefault->getDefault() ? "Enabled" : "Disabled"; };
auto getDefaultString = [this] { return value.getDefault() ? "Enabled" : "Disabled"; };
refreshChoices (getDefaultString());
initialiseComboBox (Value (new RemapperValueSourceWithDefault (valueWithDefault, { true, false })));
initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, { true, false })));
valueWithDefault->onDefaultChange = [this, getDefaultString]
value.onDefaultChange = [this, getDefaultString]
{
auto selectedId = comboBox.getSelectedId();
refreshChoices (getDefaultString());
@ -199,12 +196,6 @@ ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToContr
};
}
ChoicePropertyComponent::~ChoicePropertyComponent()
{
if (valueWithDefault != nullptr)
valueWithDefault->onDefaultChange = nullptr;
}
//==============================================================================
void ChoicePropertyComponent::initialiseComboBox (const Value& v)
{