The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

94 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. This class can be used to watch for all changes to the state of a ValueTree,
  21. and to convert them to a transmittable binary encoding.
  22. The purpose of this class is to allow two or more ValueTrees to be remotely
  23. synchronised by transmitting encoded changes over some kind of transport
  24. mechanism.
  25. To use it, you'll need to implement a subclass of ValueTreeSynchroniser
  26. and implement the stateChanged() method to transmit the encoded change (maybe
  27. via a network or other means) to a remote destination, where it can be
  28. applied to a target tree.
  29. */
  30. class JUCE_API ValueTreeSynchroniser : private ValueTree::Listener
  31. {
  32. public:
  33. /** Creates a ValueTreeSynchroniser that watches the given tree.
  34. After creating an instance of this class and somehow attaching it to
  35. a target tree, you probably want to call sendFullSyncCallback() to
  36. get them into a common starting state.
  37. */
  38. ValueTreeSynchroniser (const ValueTree& tree);
  39. /** Destructor. */
  40. virtual ~ValueTreeSynchroniser();
  41. /** This callback happens when the ValueTree changes and the given state-change message
  42. needs to be applied to any other trees that need to stay in sync with it.
  43. The data is an opaque blob of binary that you should transmit to wherever your
  44. target tree lives, and use applyChange() to apply this to the target tree.
  45. */
  46. virtual void stateChanged (const void* encodedChange, size_t encodedChangeSize) = 0;
  47. /** Forces the sending of a full state message, which may be large, as it
  48. encodes the entire ValueTree.
  49. This will internally invoke stateChanged() with the encoded version of the state.
  50. */
  51. void sendFullSyncCallback();
  52. /** Applies an encoded change to the given destination tree.
  53. When you implement a receiver for changes that were sent by the stateChanged()
  54. message, this is the function that you'll need to call to apply them to the
  55. target tree that you want to be synced.
  56. */
  57. static bool applyChange (ValueTree& target,
  58. const void* encodedChangeData, size_t encodedChangeDataSize,
  59. UndoManager* undoManager);
  60. /** Returns the root ValueTree that is being observed. */
  61. const ValueTree& getRoot() noexcept { return valueTree; }
  62. private:
  63. ValueTree valueTree;
  64. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  65. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  66. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  67. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  68. void valueTreeParentChanged (ValueTree&) override;
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreeSynchroniser)
  70. };