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.

375 lines
13KB

  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. 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. 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 (ValueWithDefault* vwd, var v, int c, ToggleButton* b)
  91. : valueWithDefault (vwd),
  92. varToControl (v),
  93. sourceValue (valueWithDefault->getPropertyAsValue()),
  94. maxChoices (c),
  95. buttonToControl (b)
  96. {
  97. sourceValue.addListener (this);
  98. }
  99. var getValue() const override
  100. {
  101. if (valueWithDefault == nullptr)
  102. return {};
  103. auto v = valueWithDefault->get();
  104. if (auto* arr = v.getArray())
  105. {
  106. if (arr->contains (varToControl))
  107. {
  108. updateButtonTickColour (buttonToControl, valueWithDefault->isUsingDefault());
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. void setValue (const var& newValue) override
  115. {
  116. if (valueWithDefault == nullptr)
  117. return;
  118. auto v = valueWithDefault->get();
  119. OptionalScopedPointer<Array<var>> arrayToControl;
  120. if (valueWithDefault->isUsingDefault())
  121. arrayToControl.set (new Array<var>(), true); // use an empty array so the default values are overwritten
  122. else
  123. arrayToControl.set (v.getArray(), false);
  124. if (arrayToControl != nullptr)
  125. {
  126. auto temp = *arrayToControl;
  127. bool newState = newValue;
  128. if (valueWithDefault->isUsingDefault())
  129. {
  130. if (auto* defaultArray = v.getArray())
  131. {
  132. if (defaultArray->contains (varToControl))
  133. newState = true; // force the state as the user is setting it explicitly
  134. }
  135. }
  136. if (newState)
  137. {
  138. if (temp.addIfNotAlreadyThere (varToControl) && (maxChoices != -1) && (temp.size() > maxChoices))
  139. temp.remove (temp.size() - 2);
  140. }
  141. else
  142. {
  143. temp.remove (temp.indexOf (varToControl));
  144. }
  145. StringComparator c;
  146. temp.sort (c);
  147. *valueWithDefault = temp;
  148. if (temp.size() == 0)
  149. valueWithDefault->resetToDefault();
  150. }
  151. }
  152. private:
  153. //==============================================================================
  154. void valueChanged (Value&) override { sendChangeMessage (true); }
  155. //==============================================================================
  156. WeakReference<ValueWithDefault> valueWithDefault;
  157. var varToControl;
  158. Value sourceValue;
  159. int maxChoices;
  160. ToggleButton* buttonToControl;
  161. //==============================================================================
  162. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSourceWithDefault)
  163. };
  164. //==============================================================================
  165. int MultiChoicePropertyComponent::getTotalButtonsHeight (int numButtons)
  166. {
  167. return numButtons * buttonHeight + 1;
  168. }
  169. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const String& propertyName,
  170. const StringArray& choices,
  171. const Array<var>& correspondingValues)
  172. : PropertyComponent (propertyName, jmin (getTotalButtonsHeight (choices.size()), collapsedHeight))
  173. {
  174. // The array of corresponding values must contain one value for each of the items in
  175. // the choices array!
  176. jassert (choices.size() == correspondingValues.size());
  177. ignoreUnused (correspondingValues);
  178. for (auto choice : choices)
  179. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  180. if (preferredHeight >= collapsedHeight)
  181. {
  182. expandable = true;
  183. maxHeight = getTotalButtonsHeight (choiceButtons.size()) + expandAreaHeight;
  184. }
  185. if (isExpandable())
  186. {
  187. {
  188. Path expandShape;
  189. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  190. expandButton.setShape (expandShape, true, true, false);
  191. }
  192. expandButton.onClick = [this] { setExpanded (! expanded); };
  193. addAndMakeVisible (expandButton);
  194. lookAndFeelChanged();
  195. }
  196. }
  197. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  198. const String& propertyName,
  199. const StringArray& choices,
  200. const Array<var>& correspondingValues,
  201. int maxChoices)
  202. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  203. {
  204. // The value to control must be an array!
  205. jassert (valueToControl.getValue().isArray());
  206. for (int i = 0; i < choiceButtons.size(); ++i)
  207. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  208. correspondingValues[i],
  209. maxChoices)));
  210. }
  211. MultiChoicePropertyComponent::MultiChoicePropertyComponent (ValueWithDefault& valueToControl,
  212. const String& propertyName,
  213. const StringArray& choices,
  214. const Array<var>& correspondingValues,
  215. int maxChoices)
  216. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  217. {
  218. valueWithDefault = &valueToControl;
  219. // The value to control must be an array!
  220. jassert (valueWithDefault->get().isArray());
  221. for (int i = 0; i < choiceButtons.size(); ++i)
  222. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueWithDefault,
  223. correspondingValues[i],
  224. maxChoices,
  225. choiceButtons[i])));
  226. valueWithDefault->onDefaultChange = [this] { repaint(); };
  227. }
  228. MultiChoicePropertyComponent::~MultiChoicePropertyComponent()
  229. {
  230. if (valueWithDefault != nullptr)
  231. valueWithDefault->onDefaultChange = nullptr;
  232. }
  233. void MultiChoicePropertyComponent::paint (Graphics& g)
  234. {
  235. g.setColour (findColour (TextEditor::backgroundColourId));
  236. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  237. if (isExpandable() && ! isExpanded())
  238. {
  239. g.setColour (findColour (TextEditor::backgroundColourId).contrasting().withAlpha (0.4f));
  240. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  241. .removeFromBottom (expandAreaHeight).withTrimmedLeft (10),
  242. Justification::centredLeft, 1);
  243. }
  244. PropertyComponent::paint (g);
  245. }
  246. void MultiChoicePropertyComponent::resized()
  247. {
  248. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  249. if (isExpandable())
  250. {
  251. bounds.removeFromBottom (5);
  252. auto buttonSlice = bounds.removeFromBottom (10);
  253. expandButton.setSize (10, 10);
  254. expandButton.setCentrePosition (buttonSlice.getCentre());
  255. }
  256. numHidden = 0;
  257. for (auto* b : choiceButtons)
  258. {
  259. if (bounds.getHeight() >= buttonHeight)
  260. {
  261. b->setVisible (true);
  262. b->setBounds (bounds.removeFromTop (buttonHeight).reduced (5, 2));
  263. }
  264. else
  265. {
  266. b->setVisible (false);
  267. ++numHidden;
  268. }
  269. }
  270. }
  271. void MultiChoicePropertyComponent::setExpanded (bool shouldBeExpanded) noexcept
  272. {
  273. if (! isExpandable() || (isExpanded() == shouldBeExpanded))
  274. return;
  275. expanded = shouldBeExpanded;
  276. preferredHeight = expanded ? maxHeight : collapsedHeight;
  277. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  278. propertyPanel->resized();
  279. if (onHeightChange != nullptr)
  280. onHeightChange();
  281. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  282. (float) expandButton.getBounds().getCentreX(),
  283. (float) expandButton.getBounds().getCentreY()));
  284. resized();
  285. }
  286. //==============================================================================
  287. void MultiChoicePropertyComponent::lookAndFeelChanged()
  288. {
  289. auto iconColour = findColour (TextEditor::backgroundColourId).contrasting();
  290. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  291. if (valueWithDefault != nullptr)
  292. {
  293. auto usingDefault = valueWithDefault->isUsingDefault();
  294. for (auto* button : choiceButtons)
  295. updateButtonTickColour (button, usingDefault);
  296. }
  297. }
  298. } // namespace juce