30namespace ValueTreeSynchroniserHelpers
42 static void getValueTreePath (ValueTree v,
const ValueTree& topLevelTree, Array<int>& path)
44 while (v != topLevelTree)
46 ValueTree parent (v.getParent());
48 if (! parent.isValid())
51 path.add (parent.indexOf (v));
56 static void writeHeader (MemoryOutputStream& stream, ChangeType type)
58 stream.writeByte ((
char) type);
61 static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
62 ChangeType type, ValueTree v)
64 writeHeader (stream, type);
67 getValueTreePath (v, target.getRoot(), path);
69 stream.writeCompressedInt (path.size());
71 for (
int i = path.size(); --i >= 0;)
72 stream.writeCompressedInt (path.getUnchecked(i));
75 static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
77 const int numLevels = input.readCompressedInt();
79 if (! isPositiveAndBelow (numLevels, 65536))
82 for (
int i = numLevels; --i >= 0;)
84 const int index = input.readCompressedInt();
86 if (! isPositiveAndBelow (index, v.getNumChildren()))
109 writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
114void ValueTreeSynchroniser::valueTreePropertyChanged (
ValueTree& vt,
const Identifier& property)
120 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
122 value->writeToStream (m);
126 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::propertyRemoved, vt);
133void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
135 const int index = parentTree.indexOf (childTree);
136 jassert (index >= 0);
138 MemoryOutputStream m;
139 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
140 m.writeCompressedInt (index);
141 childTree.writeToStream (m);
145void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&,
int oldIndex)
147 MemoryOutputStream m;
148 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
149 m.writeCompressedInt (oldIndex);
153void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent,
int oldIndex,
int newIndex)
155 MemoryOutputStream m;
156 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
157 m.writeCompressedInt (oldIndex);
158 m.writeCompressedInt (newIndex);
162void ValueTreeSynchroniser::valueTreeParentChanged (ValueTree&) {}
168 const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.
readByte();
170 if (type == ValueTreeSynchroniserHelpers::fullSync)
176 ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
183 case ValueTreeSynchroniserHelpers::propertyChanged:
190 case ValueTreeSynchroniserHelpers::propertyRemoved:
197 case ValueTreeSynchroniserHelpers::childAdded:
204 case ValueTreeSynchroniserHelpers::childRemoved:
218 case ValueTreeSynchroniserHelpers::childMoved:
226 v.
moveChild (oldIndex, newIndex, undoManager);
Represents a string identifier, designed for accessing properties by name.
const String & toString() const noexcept
Returns this identifier as a string.
Writes data to an internal memory buffer, which grows as required.
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
Manages a list of undo/redo commands.
~ValueTreeSynchroniser() override
Destructor.
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Applies an encoded change to the given destination tree.
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
This callback happens when the ValueTree changes and the given state-change message needs to be appli...
ValueTreeSynchroniser(const ValueTree &tree)
Creates a ValueTreeSynchroniser that watches the given tree.
void sendFullSyncCallback()
Forces the sending of a full state message, which may be large, as it encodes the entire ValueTree.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
static ValueTree readFromStream(InputStream &input)
Reloads a tree from a stream that was written with writeToStream().
void removeChild(const ValueTree &child, UndoManager *undoManager)
Removes the specified child from this tree's child-list.
ValueTree getChild(int index) const
Returns one of this tree's sub-trees.
int getNumChildren() const noexcept
Returns the number of child trees inside this one.
bool isValid() const noexcept
Returns true if this tree refers to some valid data.
const var * getPropertyPointer(const Identifier &name) const noexcept
Returns a pointer to the value of a named property, or nullptr if the property doesn't exist.
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Changes a named property of the tree.
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Adds a child to this tree.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
void writeToStream(OutputStream &output) const
Stores this tree (and all its children) in a binary format.
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Moves one of the sub-trees to a different index.
void removeProperty(const Identifier &name, UndoManager *undoManager)
Removes a property from the tree.
static var readFromStream(InputStream &input)
Reads back a stored binary representation of a value.