Connect juce::Range bounds to juce::Value


#1

Is it possible to make the start and end properties of a juce::Range a juce::Value somehow?

I’m using juce::Values all over my GUI to persist the GUI state, but can’t find a way to easily persist the juce::Ranges I use internally.


#2

I’ve come up with this proxy class to wrap a juce::Range with a juce::Value each for the start and end property:

template<typename T>
class ValueRange : private juce::Value::Listener {

public:
	juce::Value startValue, endValue;

	ValueRange(const T start, const T end) :
			range(start, end),
			startValue(start), endValue(end) {

		startValue.addListener(this);
		endValue.addListener(this);
	}

	ValueRange(const ValueRange&) = delete;

	operator juce::Range<T>() {
		return range;
	}

	operator const juce::Range<T> &() const {
		return range;
	}

	const juce::Range<T> &getRange() const {
		return range;
	}

	ValueRange &operator=(const juce::Range<T> &newRange) {
		range = newRange;
		startValue = range.getStart();
		endValue = range.getEnd();

		return *this;
	}

private:
	juce::Range<T> range;

	void valueChanged(juce::Value &value) override {
		if (value.refersToSameSourceAs(startValue)) {
			range.setStart((T) startValue.getValue());

		} else if (value.refersToSameSourceAs(endValue)) {
			range.setEnd((T) endValue.getValue());
		}
	}
};