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.

353 lines
12KB

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