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_ValueWithDefault.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. //==============================================================================
  21. /**
  22. This class acts as a wrapper around a property inside a ValueTree.
  23. If the property inside the ValueTree is missing the ValueWithDefault will automatically
  24. return a default value, which can be specified when initialising the ValueWithDefault.
  25. @tags{DataStructures}
  26. */
  27. class ValueWithDefault
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an unitialised ValueWithDefault. Initialise it using one of the referTo() methods. */
  32. ValueWithDefault() = default;
  33. /** Creates an ValueWithDefault object. The default value will be an empty var. */
  34. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um)
  35. : targetTree (tree),
  36. targetProperty (propertyID),
  37. undoManager (um),
  38. defaultValue()
  39. {
  40. }
  41. /** Creates an ValueWithDefault object. The default value will be defaultToUse. */
  42. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
  43. const var& defaultToUse)
  44. : targetTree (tree),
  45. targetProperty (propertyID),
  46. undoManager (um),
  47. defaultValue (defaultToUse)
  48. {
  49. }
  50. /** Creates an ValueWithDefault object. The default value will be defaultToUse.
  51. Use this constructor if the underlying var object being controlled is an array and
  52. it will handle the conversion to/from a delimited String that can be written to
  53. XML format.
  54. */
  55. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
  56. const var& defaultToUse, StringRef arrayDelimiter)
  57. : targetTree (tree),
  58. targetProperty (propertyID),
  59. undoManager (um),
  60. defaultValue (defaultToUse),
  61. delimiter (arrayDelimiter)
  62. {
  63. }
  64. /** Creates a ValueWithDefault object from another ValueWithDefault object. */
  65. ValueWithDefault (const ValueWithDefault& other)
  66. : targetTree (other.targetTree),
  67. targetProperty (other.targetProperty),
  68. undoManager (other.undoManager),
  69. defaultValue (other.defaultValue),
  70. delimiter (other.delimiter)
  71. {
  72. }
  73. //==============================================================================
  74. /** Returns the current value of the property. If the property does not exist this
  75. returns the default value.
  76. */
  77. var get() const noexcept
  78. {
  79. if (isUsingDefault())
  80. return defaultValue;
  81. if (delimiter.isNotEmpty())
  82. return delimitedStringToVarArray (targetTree[targetProperty].toString());
  83. return targetTree[targetProperty];
  84. }
  85. /** Returns the current property as a Value object. */
  86. Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
  87. /** Returns the current default value. */
  88. var getDefault() const { return defaultValue; }
  89. /** Sets the default value to a new var. */
  90. void setDefault (const var& newDefault)
  91. {
  92. if (defaultValue != newDefault)
  93. {
  94. defaultValue = newDefault;
  95. if (onDefaultChange != nullptr)
  96. onDefaultChange();
  97. }
  98. }
  99. /** Returns true if the property does not exist in the referenced ValueTree. */
  100. bool isUsingDefault() const
  101. {
  102. return ! targetTree.hasProperty (targetProperty);
  103. }
  104. /** Removes the property from the referenced ValueTree. */
  105. void resetToDefault() noexcept
  106. {
  107. targetTree.removeProperty (targetProperty, nullptr);
  108. }
  109. /** You can assign a lambda to this callback object to have it called when the default value is changed. */
  110. std::function<void()> onDefaultChange;
  111. //==============================================================================
  112. /** Sets the property and returns the new ValueWithDefault. This will modify the property in the referenced ValueTree. */
  113. ValueWithDefault& operator= (const var& newValue)
  114. {
  115. setValue (newValue, undoManager);
  116. return *this;
  117. }
  118. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  119. void setValue (const var& newValue, UndoManager* undoManagerToUse)
  120. {
  121. if (auto* array = newValue.getArray())
  122. targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array), undoManagerToUse);
  123. else
  124. targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
  125. }
  126. //==============================================================================
  127. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree. */
  128. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
  129. {
  130. referToWithDefault (tree, property, um, var(), {});
  131. }
  132. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree,
  133. and specifies a default value to use.
  134. */
  135. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
  136. {
  137. referToWithDefault (tree, property, um, defaultVal, {});
  138. }
  139. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um,
  140. const var& defaultVal, StringRef arrayDelimiter)
  141. {
  142. referToWithDefault (tree, property, um, defaultVal, arrayDelimiter);
  143. }
  144. //==============================================================================
  145. /** Returns a reference to the ValueTree containing the referenced property. */
  146. ValueTree& getValueTree() noexcept { return targetTree; }
  147. /** Returns the property ID of the referenced property. */
  148. Identifier& getPropertyID() noexcept { return targetProperty; }
  149. /** Returns the UndoManager that is being used. */
  150. UndoManager* getUndoManager() noexcept { return undoManager; }
  151. //==============================================================================
  152. ValueWithDefault& operator= (const ValueWithDefault& other)
  153. {
  154. referToWithDefault (other.targetTree, other.targetProperty, other.undoManager,
  155. other.defaultValue, other.delimiter);
  156. return *this;
  157. }
  158. private:
  159. //==============================================================================
  160. ValueTree targetTree;
  161. Identifier targetProperty;
  162. UndoManager* undoManager = nullptr;
  163. var defaultValue;
  164. String delimiter;
  165. //==============================================================================
  166. void referToWithDefault (const ValueTree& v, const Identifier& i, UndoManager* um,
  167. const var& defaultVal, StringRef del)
  168. {
  169. targetTree = v;
  170. targetProperty = i;
  171. undoManager = um;
  172. defaultValue = defaultVal;
  173. delimiter = del;
  174. }
  175. //==============================================================================
  176. String varArrayToDelimitedString (const Array<var>& input) const noexcept
  177. {
  178. // if you are trying to control a var that is an array then you need to
  179. // set a delimiter string that will be used when writing to XML!
  180. jassert (delimiter.isNotEmpty());
  181. StringArray elements;
  182. for (auto& v : input)
  183. elements.add (v.toString());
  184. return elements.joinIntoString (delimiter);
  185. }
  186. Array<var> delimitedStringToVarArray (StringRef input) const noexcept
  187. {
  188. Array<var> arr;
  189. for (auto t : StringArray::fromTokens (input, delimiter, {}))
  190. arr.add (t);
  191. return arr;
  192. }
  193. //==============================================================================
  194. JUCE_DECLARE_WEAK_REFERENCEABLE (ValueWithDefault)
  195. };
  196. } // namespace juce