/*============================================================================== Copyright 2018 by Roland Rabien For more information visit www.rabiensoftware.com ==============================================================================*/ #pragma once //============================================================================== /* Mirrors a ValueTree in Objects */ class ValueTreeObject : private ValueTree::Listener { public: ValueTreeObject (const ValueTree& state); ValueTree& getState() { return state; } static std::function factory; public: const OwnedArray& getChildren() const { return children; } template TargetClass* findParentOfType() const { auto* p = parent; while (p != nullptr) { if (auto* res = dynamic_cast (parent)) return res; p = p->parent; } return nullptr; } template Array findChildrenOfClass() const { Array res; for (auto* c : children) if (auto* t = dynamic_cast (c)) res.add (t); return res; } template int countChildrenOfClass() const { int count = 0; for (auto* c : children) if (auto* t = dynamic_cast (c)) count++; return count; } template TargetClass* findChildOfClass (int idx) const { int count = 0; for (auto* c : children) { if (auto* t = dynamic_cast (c)) { if (count == idx) return t; count++; } } return nullptr; } private: void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, const Identifier& property) override; void valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded) override; void valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved) override; void valueTreeChildOrderChanged (ValueTree& parentTreeWhoseChildrenHaveMoved, int oldIndex, int newIndex) override; void valueTreeParentChanged (ValueTree& treeWhoseParentHasChanged) override; void valueTreeRedirected (ValueTree& treeWhichHasBeenChanged) override; private: ValueTree state; ValueTreeObject* parent = nullptr; OwnedArray children; JUCE_LEAK_DETECTOR (ValueTreeObject) };