Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ValueTreeSynchroniser.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace ValueTreeSynchroniserHelpers
  21. {
  22. enum ChangeType
  23. {
  24. propertyChanged = 1,
  25. fullSync = 2,
  26. childAdded = 3,
  27. childRemoved = 4,
  28. childMoved = 5,
  29. propertyRemoved = 6
  30. };
  31. static void getValueTreePath (ValueTree v, const ValueTree& topLevelTree, Array<int>& path)
  32. {
  33. while (v != topLevelTree)
  34. {
  35. ValueTree parent (v.getParent());
  36. if (! parent.isValid())
  37. break;
  38. path.add (parent.indexOf (v));
  39. v = parent;
  40. }
  41. }
  42. static void writeHeader (MemoryOutputStream& stream, ChangeType type)
  43. {
  44. stream.writeByte ((char) type);
  45. }
  46. static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
  47. ChangeType type, ValueTree v)
  48. {
  49. writeHeader (stream, type);
  50. Array<int> path;
  51. getValueTreePath (v, target.getRoot(), path);
  52. stream.writeCompressedInt (path.size());
  53. for (int i = path.size(); --i >= 0;)
  54. stream.writeCompressedInt (path.getUnchecked(i));
  55. }
  56. static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
  57. {
  58. const int numLevels = input.readCompressedInt();
  59. if (! isPositiveAndBelow (numLevels, 65536)) // sanity-check
  60. return {};
  61. for (int i = numLevels; --i >= 0;)
  62. {
  63. const int index = input.readCompressedInt();
  64. if (! isPositiveAndBelow (index, v.getNumChildren()))
  65. return {};
  66. v = v.getChild (index);
  67. }
  68. return v;
  69. }
  70. }
  71. ValueTreeSynchroniser::ValueTreeSynchroniser (const ValueTree& tree) : valueTree (tree)
  72. {
  73. valueTree.addListener (this);
  74. }
  75. ValueTreeSynchroniser::~ValueTreeSynchroniser()
  76. {
  77. valueTree.removeListener (this);
  78. }
  79. void ValueTreeSynchroniser::sendFullSyncCallback()
  80. {
  81. MemoryOutputStream m;
  82. writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
  83. valueTree.writeToStream (m);
  84. stateChanged (m.getData(), m.getDataSize());
  85. }
  86. void ValueTreeSynchroniser::valueTreePropertyChanged (ValueTree& vt, const Identifier& property)
  87. {
  88. MemoryOutputStream m;
  89. if (auto* value = vt.getPropertyPointer (property))
  90. {
  91. ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
  92. m.writeString (property.toString());
  93. value->writeToStream (m);
  94. }
  95. else
  96. {
  97. ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyRemoved, vt);
  98. m.writeString (property.toString());
  99. }
  100. stateChanged (m.getData(), m.getDataSize());
  101. }
  102. void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
  103. {
  104. const int index = parentTree.indexOf (childTree);
  105. jassert (index >= 0);
  106. MemoryOutputStream m;
  107. ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
  108. m.writeCompressedInt (index);
  109. childTree.writeToStream (m);
  110. stateChanged (m.getData(), m.getDataSize());
  111. }
  112. void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int oldIndex)
  113. {
  114. MemoryOutputStream m;
  115. ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
  116. m.writeCompressedInt (oldIndex);
  117. stateChanged (m.getData(), m.getDataSize());
  118. }
  119. void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent, int oldIndex, int newIndex)
  120. {
  121. MemoryOutputStream m;
  122. ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
  123. m.writeCompressedInt (oldIndex);
  124. m.writeCompressedInt (newIndex);
  125. stateChanged (m.getData(), m.getDataSize());
  126. }
  127. bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
  128. {
  129. MemoryInputStream input (data, dataSize, false);
  130. const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
  131. if (type == ValueTreeSynchroniserHelpers::fullSync)
  132. {
  133. root = ValueTree::readFromStream (input);
  134. return true;
  135. }
  136. ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
  137. if (! v.isValid())
  138. return false;
  139. switch (type)
  140. {
  141. case ValueTreeSynchroniserHelpers::propertyChanged:
  142. {
  143. Identifier property (input.readString());
  144. v.setProperty (property, var::readFromStream (input), undoManager);
  145. return true;
  146. }
  147. case ValueTreeSynchroniserHelpers::propertyRemoved:
  148. {
  149. Identifier property (input.readString());
  150. v.removeProperty (property, undoManager);
  151. return true;
  152. }
  153. case ValueTreeSynchroniserHelpers::childAdded:
  154. {
  155. const int index = input.readCompressedInt();
  156. v.addChild (ValueTree::readFromStream (input), index, undoManager);
  157. return true;
  158. }
  159. case ValueTreeSynchroniserHelpers::childRemoved:
  160. {
  161. const int index = input.readCompressedInt();
  162. if (isPositiveAndBelow (index, v.getNumChildren()))
  163. {
  164. v.removeChild (index, undoManager);
  165. return true;
  166. }
  167. jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
  168. break;
  169. }
  170. case ValueTreeSynchroniserHelpers::childMoved:
  171. {
  172. const int oldIndex = input.readCompressedInt();
  173. const int newIndex = input.readCompressedInt();
  174. if (isPositiveAndBelow (oldIndex, v.getNumChildren())
  175. && isPositiveAndBelow (newIndex, v.getNumChildren()))
  176. {
  177. v.moveChild (oldIndex, newIndex, undoManager);
  178. return true;
  179. }
  180. jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
  181. break;
  182. }
  183. case ValueTreeSynchroniserHelpers::fullSync:
  184. break;
  185. default:
  186. jassertfalse; // Seem to have received some corrupt data?
  187. break;
  188. }
  189. return false;
  190. }
  191. } // namespace juce