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.

236 lines
7.1KB

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