1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
namespace ValueTreeSynchroniserHelpers
{
enum ChangeType
{
propertyChanged = 1,
fullSync = 2,
childAdded = 3,
childRemoved = 4,
childMoved = 5
};
static void getValueTreePath (ValueTree v, const ValueTree& topLevelTree, Array<int>& path)
{
while (v != topLevelTree)
{
ValueTree parent (v.getParent());
if (! parent.isValid())
break;
path.add (parent.indexOf (v));
v = parent;
}
}
static void writeHeader (MemoryOutputStream& stream, ChangeType type)
{
stream.writeByte ((char) type);
}
static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
ChangeType type, ValueTree v)
{
writeHeader (stream, type);
Array<int> path;
getValueTreePath (v, target.getRoot(), path);
stream.writeCompressedInt (path.size());
for (int i = path.size(); --i >= 0;)
stream.writeCompressedInt (path.getUnchecked(i));
}
static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
{
const int numLevels = input.readCompressedInt();
if (! isPositiveAndBelow (numLevels, 65536)) // sanity-check
return ValueTree();
for (int i = numLevels; --i >= 0;)
{
const int index = input.readCompressedInt();
if (! isPositiveAndBelow (index, v.getNumChildren()))
return ValueTree();
v = v.getChild (index);
}
return v;
}
}
ValueTreeSynchroniser::ValueTreeSynchroniser (const ValueTree& tree) : valueTree (tree)
{
valueTree.addListener (this);
}
ValueTreeSynchroniser::~ValueTreeSynchroniser()
{
valueTree.removeListener (this);
}
void ValueTreeSynchroniser::sendFullSyncCallback()
{
MemoryOutputStream m;
writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
valueTree.writeToStream (m);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreePropertyChanged (ValueTree& vt, const Identifier& property)
{
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
m.writeString (property.toString());
vt.getProperty (property).writeToStream (m);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
{
const int index = parentTree.indexOf (childTree);
jassert (index >= 0);
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
m.writeCompressedInt (index);
childTree.writeToStream (m);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int oldIndex)
{
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
m.writeCompressedInt (oldIndex);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent, int oldIndex, int newIndex)
{
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
m.writeCompressedInt (oldIndex);
m.writeCompressedInt (newIndex);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeParentChanged (ValueTree&) {} // (No action needed here)
bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
{
MemoryInputStream input (data, dataSize, false);
const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
if (type == ValueTreeSynchroniserHelpers::fullSync)
{
root = ValueTree::readFromStream (input);
return true;
}
ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
if (! v.isValid())
return false;
switch (type)
{
case ValueTreeSynchroniserHelpers::propertyChanged:
{
Identifier property (input.readString());
v.setProperty (property, var::readFromStream (input), undoManager);
return true;
}
case ValueTreeSynchroniserHelpers::childAdded:
{
const int index = input.readCompressedInt();
v.addChild (ValueTree::readFromStream (input), index, undoManager);
return true;
}
case ValueTreeSynchroniserHelpers::childRemoved:
{
const int index = input.readCompressedInt();
if (isPositiveAndBelow (index, v.getNumChildren()))
{
v.removeChild (index, undoManager);
return true;
}
jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
break;
}
case ValueTreeSynchroniserHelpers::childMoved:
{
const int oldIndex = input.readCompressedInt();
const int newIndex = input.readCompressedInt();
if (isPositiveAndBelow (oldIndex, v.getNumChildren())
&& isPositiveAndBelow (newIndex, v.getNumChildren()))
{
v.moveChild (oldIndex, newIndex, undoManager);
return true;
}
jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
break;
}
default:
jassertfalse; // Seem to have received some corrupt data?
break;
}
return false;
}
|