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.

360 lines
13KB

  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. //==============================================================================
  21. class StringComparator
  22. {
  23. public:
  24. static int compareElements (var first, var second)
  25. {
  26. if (first.toString() > second.toString())
  27. return 1;
  28. else if (first.toString() < second.toString())
  29. return -1;
  30. return 0;
  31. }
  32. };
  33. static void updateButtonTickColour (ToggleButton* button, bool usingDefault)
  34. {
  35. button->setColour (ToggleButton::tickColourId, button->getLookAndFeel().findColour (ToggleButton::tickColourId)
  36. .withAlpha (usingDefault ? 0.4f : 1.0f));
  37. }
  38. //==============================================================================
  39. class MultiChoicePropertyComponent::MultiChoiceRemapperSource : public Value::ValueSource,
  40. private Value::Listener
  41. {
  42. public:
  43. MultiChoiceRemapperSource (const Value& source, var v, int c)
  44. : sourceValue (source),
  45. varToControl (v),
  46. maxChoices (c)
  47. {
  48. sourceValue.addListener (this);
  49. }
  50. var getValue() const override
  51. {
  52. if (auto* arr = sourceValue.getValue().getArray())
  53. if (arr->contains (varToControl))
  54. return true;
  55. return false;
  56. }
  57. void setValue (const var& newValue) override
  58. {
  59. if (auto* arr = sourceValue.getValue().getArray())
  60. {
  61. auto temp = *arr;
  62. if (static_cast<bool> (newValue))
  63. {
  64. if (temp.addIfNotAlreadyThere (varToControl) && (maxChoices != -1) && (temp.size() > maxChoices))
  65. temp.remove (temp.size() - 2);
  66. }
  67. else
  68. {
  69. temp.remove (arr->indexOf (varToControl));
  70. }
  71. StringComparator c;
  72. temp.sort (c);
  73. sourceValue = temp;
  74. }
  75. }
  76. private:
  77. Value sourceValue;
  78. var varToControl;
  79. int maxChoices;
  80. //==============================================================================
  81. void valueChanged (Value&) override { sendChangeMessage (true); }
  82. //==============================================================================
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSource)
  84. };
  85. //==============================================================================
  86. class MultiChoicePropertyComponent::MultiChoiceRemapperSourceWithDefault : public Value::ValueSource,
  87. private Value::Listener
  88. {
  89. public:
  90. MultiChoiceRemapperSourceWithDefault (const ValueTreePropertyWithDefault& val,
  91. var v, int c, ToggleButton* b)
  92. : value (val),
  93. varToControl (v),
  94. sourceValue (value.getPropertyAsValue()),
  95. maxChoices (c),
  96. buttonToControl (b)
  97. {
  98. sourceValue.addListener (this);
  99. }
  100. var getValue() const override
  101. {
  102. auto v = value.get();
  103. if (auto* arr = v.getArray())
  104. {
  105. if (arr->contains (varToControl))
  106. {
  107. updateButtonTickColour (buttonToControl, value.isUsingDefault());
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. void setValue (const var& newValue) override
  114. {
  115. auto v = value.get();
  116. OptionalScopedPointer<Array<var>> arrayToControl;
  117. if (value.isUsingDefault())
  118. arrayToControl.set (new Array<var>(), true); // use an empty array so the default values are overwritten
  119. else
  120. arrayToControl.set (v.getArray(), false);
  121. if (arrayToControl != nullptr)
  122. {
  123. auto temp = *arrayToControl;
  124. bool newState = newValue;
  125. if (value.isUsingDefault())
  126. {
  127. if (auto* defaultArray = v.getArray())
  128. {
  129. if (defaultArray->contains (varToControl))
  130. newState = true; // force the state as the user is setting it explicitly
  131. }
  132. }
  133. if (newState)
  134. {
  135. if (temp.addIfNotAlreadyThere (varToControl) && (maxChoices != -1) && (temp.size() > maxChoices))
  136. temp.remove (temp.size() - 2);
  137. }
  138. else
  139. {
  140. temp.remove (temp.indexOf (varToControl));
  141. }
  142. StringComparator c;
  143. temp.sort (c);
  144. value = temp;
  145. if (temp.size() == 0)
  146. value.resetToDefault();
  147. }
  148. }
  149. private:
  150. //==============================================================================
  151. void valueChanged (Value&) override { sendChangeMessage (true); }
  152. //==============================================================================
  153. ValueTreePropertyWithDefault value;
  154. var varToControl;
  155. Value sourceValue;
  156. int maxChoices;
  157. ToggleButton* buttonToControl;
  158. //==============================================================================
  159. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSourceWithDefault)
  160. };
  161. //==============================================================================
  162. int MultiChoicePropertyComponent::getTotalButtonsHeight (int numButtons)
  163. {
  164. return numButtons * buttonHeight + 1;
  165. }
  166. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const String& propertyName,
  167. const StringArray& choices,
  168. const Array<var>& correspondingValues)
  169. : PropertyComponent (propertyName, jmin (getTotalButtonsHeight (choices.size()), collapsedHeight))
  170. {
  171. // The array of corresponding values must contain one value for each of the items in
  172. // the choices array!
  173. jassertquiet (choices.size() == correspondingValues.size());
  174. for (auto choice : choices)
  175. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  176. if (preferredHeight >= collapsedHeight)
  177. {
  178. expandable = true;
  179. maxHeight = getTotalButtonsHeight (choiceButtons.size()) + expandAreaHeight;
  180. }
  181. if (isExpandable())
  182. {
  183. {
  184. Path expandShape;
  185. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  186. expandButton.setShape (expandShape, true, true, false);
  187. }
  188. expandButton.onClick = [this] { setExpanded (! expanded); };
  189. addAndMakeVisible (expandButton);
  190. lookAndFeelChanged();
  191. }
  192. }
  193. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  194. const String& propertyName,
  195. const StringArray& choices,
  196. const Array<var>& correspondingValues,
  197. int maxChoices)
  198. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  199. {
  200. // The value to control must be an array!
  201. jassert (valueToControl.getValue().isArray());
  202. for (int i = 0; i < choiceButtons.size(); ++i)
  203. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  204. correspondingValues[i],
  205. maxChoices)));
  206. }
  207. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
  208. const String& propertyName,
  209. const StringArray& choices,
  210. const Array<var>& correspondingValues,
  211. int maxChoices)
  212. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  213. {
  214. value = valueToControl;
  215. // The value to control must be an array!
  216. jassert (value.get().isArray());
  217. for (int i = 0; i < choiceButtons.size(); ++i)
  218. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (value,
  219. correspondingValues[i],
  220. maxChoices,
  221. choiceButtons[i])));
  222. value.onDefaultChange = [this] { repaint(); };
  223. }
  224. void MultiChoicePropertyComponent::paint (Graphics& g)
  225. {
  226. g.setColour (findColour (TextEditor::backgroundColourId));
  227. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  228. if (isExpandable() && ! isExpanded())
  229. {
  230. g.setColour (findColour (TextEditor::backgroundColourId).contrasting().withAlpha (0.4f));
  231. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  232. .removeFromBottom (expandAreaHeight).withTrimmedLeft (10),
  233. Justification::centredLeft, 1);
  234. }
  235. PropertyComponent::paint (g);
  236. }
  237. void MultiChoicePropertyComponent::resized()
  238. {
  239. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  240. if (isExpandable())
  241. {
  242. bounds.removeFromBottom (5);
  243. auto buttonSlice = bounds.removeFromBottom (10);
  244. expandButton.setSize (10, 10);
  245. expandButton.setCentrePosition (buttonSlice.getCentre());
  246. }
  247. numHidden = 0;
  248. for (auto* b : choiceButtons)
  249. {
  250. if (bounds.getHeight() >= buttonHeight)
  251. {
  252. b->setVisible (true);
  253. b->setBounds (bounds.removeFromTop (buttonHeight).reduced (5, 2));
  254. }
  255. else
  256. {
  257. b->setVisible (false);
  258. ++numHidden;
  259. }
  260. }
  261. }
  262. void MultiChoicePropertyComponent::setExpanded (bool shouldBeExpanded) noexcept
  263. {
  264. if (! isExpandable() || (isExpanded() == shouldBeExpanded))
  265. return;
  266. expanded = shouldBeExpanded;
  267. preferredHeight = expanded ? maxHeight : collapsedHeight;
  268. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  269. propertyPanel->resized();
  270. if (onHeightChange != nullptr)
  271. onHeightChange();
  272. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  273. (float) expandButton.getBounds().getCentreX(),
  274. (float) expandButton.getBounds().getCentreY()));
  275. resized();
  276. }
  277. //==============================================================================
  278. void MultiChoicePropertyComponent::lookAndFeelChanged()
  279. {
  280. auto iconColour = findColour (TextEditor::backgroundColourId).contrasting();
  281. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  282. const auto usingDefault = value.isUsingDefault();
  283. for (auto* button : choiceButtons)
  284. updateButtonTickColour (button, usingDefault);
  285. }
  286. } // namespace juce