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.

269 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. //==============================================================================
  16. class ChoiceRemapperValueSource : public Value::ValueSource,
  17. private Value::Listener
  18. {
  19. public:
  20. ChoiceRemapperValueSource (const Value& source, const Array<var>& map)
  21. : sourceValue (source),
  22. mappings (map)
  23. {
  24. sourceValue.addListener (this);
  25. }
  26. var getValue() const override
  27. {
  28. auto targetValue = sourceValue.getValue();
  29. for (auto& map : mappings)
  30. if (map.equalsWithSameType (targetValue))
  31. return mappings.indexOf (map) + 1;
  32. return mappings.indexOf (targetValue) + 1;
  33. }
  34. void setValue (const var& newValue) override
  35. {
  36. auto remappedVal = mappings [static_cast<int> (newValue) - 1];
  37. if (! remappedVal.equalsWithSameType (sourceValue))
  38. sourceValue = remappedVal;
  39. }
  40. protected:
  41. Value sourceValue;
  42. Array<var> mappings;
  43. void valueChanged (Value&) override { sendChangeMessage (true); }
  44. //==============================================================================
  45. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceRemapperValueSource)
  46. };
  47. //==============================================================================
  48. class ChoiceRemapperValueSourceWithDefault : public Value::ValueSource,
  49. private Value::Listener
  50. {
  51. public:
  52. ChoiceRemapperValueSourceWithDefault (const ValueTreePropertyWithDefault& v, const Array<var>& map)
  53. : value (v),
  54. sourceValue (value.getPropertyAsValue()),
  55. mappings (map)
  56. {
  57. sourceValue.addListener (this);
  58. }
  59. var getValue() const override
  60. {
  61. if (! value.isUsingDefault())
  62. {
  63. const auto target = sourceValue.getValue();
  64. const auto equalsWithSameType = [&target] (const var& map) { return map.equalsWithSameType (target); };
  65. auto iter = std::find_if (mappings.begin(), mappings.end(), equalsWithSameType);
  66. if (iter == mappings.end())
  67. iter = std::find (mappings.begin(), mappings.end(), target);
  68. if (iter != mappings.end())
  69. return 1 + (int) std::distance (mappings.begin(), iter);
  70. }
  71. return -1;
  72. }
  73. void setValue (const var& newValue) override
  74. {
  75. auto newValueInt = static_cast<int> (newValue);
  76. if (newValueInt == -1)
  77. {
  78. value.resetToDefault();
  79. }
  80. else
  81. {
  82. auto remappedVal = mappings [newValueInt - 1];
  83. if (! remappedVal.equalsWithSameType (sourceValue))
  84. value = remappedVal;
  85. }
  86. }
  87. private:
  88. void valueChanged (Value&) override { sendChangeMessage (true); }
  89. ValueTreePropertyWithDefault value;
  90. Value sourceValue;
  91. Array<var> mappings;
  92. //==============================================================================
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceRemapperValueSourceWithDefault)
  94. };
  95. //==============================================================================
  96. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  97. : PropertyComponent (name),
  98. isCustomClass (true)
  99. {
  100. }
  101. ChoicePropertyComponent::ChoicePropertyComponent (const String& name,
  102. const StringArray& choiceList,
  103. const Array<var>& correspondingValues)
  104. : PropertyComponent (name),
  105. choices (choiceList)
  106. {
  107. // The array of corresponding values must contain one value for each of the items in
  108. // the choices array!
  109. jassertquiet (correspondingValues.size() == choices.size());
  110. }
  111. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  112. const String& name,
  113. const StringArray& choiceList,
  114. const Array<var>& correspondingValues)
  115. : ChoicePropertyComponent (name, choiceList, correspondingValues)
  116. {
  117. refreshChoices();
  118. initialiseComboBox (Value (new ChoiceRemapperValueSource (valueToControl, correspondingValues)));
  119. }
  120. ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
  121. const String& name,
  122. const StringArray& choiceList,
  123. const Array<var>& correspondingValues)
  124. : ChoicePropertyComponent (name, choiceList, correspondingValues)
  125. {
  126. value = valueToControl;
  127. auto getDefaultString = [this, correspondingValues] { return choices [correspondingValues.indexOf (value.getDefault())]; };
  128. refreshChoices (getDefaultString());
  129. initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, correspondingValues)));
  130. value.onDefaultChange = [this, getDefaultString]
  131. {
  132. auto selectedId = comboBox.getSelectedId();
  133. refreshChoices (getDefaultString());
  134. comboBox.setSelectedId (selectedId);
  135. };
  136. }
  137. ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
  138. const String& name)
  139. : PropertyComponent (name),
  140. choices ({ "Enabled", "Disabled" })
  141. {
  142. value = valueToControl;
  143. auto getDefaultString = [this] { return value.getDefault() ? "Enabled" : "Disabled"; };
  144. refreshChoices (getDefaultString());
  145. initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, { true, false })));
  146. value.onDefaultChange = [this, getDefaultString]
  147. {
  148. auto selectedId = comboBox.getSelectedId();
  149. refreshChoices (getDefaultString());
  150. comboBox.setSelectedId (selectedId);
  151. };
  152. }
  153. //==============================================================================
  154. void ChoicePropertyComponent::initialiseComboBox (const Value& v)
  155. {
  156. if (v != Value())
  157. comboBox.setSelectedId (v.getValue(), dontSendNotification);
  158. comboBox.getSelectedIdAsValue().referTo (v);
  159. comboBox.setEditableText (false);
  160. addAndMakeVisible (comboBox);
  161. }
  162. void ChoicePropertyComponent::refreshChoices()
  163. {
  164. comboBox.clear();
  165. for (int i = 0; i < choices.size(); ++i)
  166. {
  167. const auto& choice = choices[i];
  168. if (choice.isNotEmpty())
  169. comboBox.addItem (choice, i + 1);
  170. else
  171. comboBox.addSeparator();
  172. }
  173. }
  174. void ChoicePropertyComponent::refreshChoices (const String& defaultString)
  175. {
  176. refreshChoices();
  177. comboBox.addItem ("Default" + (defaultString.isNotEmpty() ? " (" + defaultString + ")" : ""), -1);
  178. }
  179. //==============================================================================
  180. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  181. {
  182. jassertfalse; // you need to override this method in your subclass!
  183. }
  184. int ChoicePropertyComponent::getIndex() const
  185. {
  186. jassertfalse; // you need to override this method in your subclass!
  187. return -1;
  188. }
  189. const StringArray& ChoicePropertyComponent::getChoices() const
  190. {
  191. return choices;
  192. }
  193. //==============================================================================
  194. void ChoicePropertyComponent::refresh()
  195. {
  196. if (isCustomClass)
  197. {
  198. if (! comboBox.isVisible())
  199. {
  200. refreshChoices();
  201. initialiseComboBox ({});
  202. comboBox.onChange = [this] { changeIndex(); };
  203. }
  204. comboBox.setSelectedId (getIndex() + 1, dontSendNotification);
  205. }
  206. }
  207. void ChoicePropertyComponent::changeIndex()
  208. {
  209. if (isCustomClass)
  210. {
  211. auto newIndex = comboBox.getSelectedId() - 1;
  212. if (newIndex != getIndex())
  213. setIndex (newIndex);
  214. }
  215. }
  216. } // namespace juce