The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

374 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. 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 (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. jassertquiet (choices.size() == correspondingValues.size());
  177. for (auto choice : choices)
  178. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  179. if (preferredHeight >= collapsedHeight)
  180. {
  181. expandable = true;
  182. maxHeight = getTotalButtonsHeight (choiceButtons.size()) + expandAreaHeight;
  183. }
  184. if (isExpandable())
  185. {
  186. {
  187. Path expandShape;
  188. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  189. expandButton.setShape (expandShape, true, true, false);
  190. }
  191. expandButton.onClick = [this] { setExpanded (! expanded); };
  192. addAndMakeVisible (expandButton);
  193. lookAndFeelChanged();
  194. }
  195. }
  196. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  197. const String& propertyName,
  198. const StringArray& choices,
  199. const Array<var>& correspondingValues,
  200. int maxChoices)
  201. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  202. {
  203. // The value to control must be an array!
  204. jassert (valueToControl.getValue().isArray());
  205. for (int i = 0; i < choiceButtons.size(); ++i)
  206. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  207. correspondingValues[i],
  208. maxChoices)));
  209. }
  210. MultiChoicePropertyComponent::MultiChoicePropertyComponent (ValueWithDefault& valueToControl,
  211. const String& propertyName,
  212. const StringArray& choices,
  213. const Array<var>& correspondingValues,
  214. int maxChoices)
  215. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  216. {
  217. valueWithDefault = &valueToControl;
  218. // The value to control must be an array!
  219. jassert (valueWithDefault->get().isArray());
  220. for (int i = 0; i < choiceButtons.size(); ++i)
  221. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueWithDefault,
  222. correspondingValues[i],
  223. maxChoices,
  224. choiceButtons[i])));
  225. valueWithDefault->onDefaultChange = [this] { repaint(); };
  226. }
  227. MultiChoicePropertyComponent::~MultiChoicePropertyComponent()
  228. {
  229. if (valueWithDefault != nullptr)
  230. valueWithDefault->onDefaultChange = nullptr;
  231. }
  232. void MultiChoicePropertyComponent::paint (Graphics& g)
  233. {
  234. g.setColour (findColour (TextEditor::backgroundColourId));
  235. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  236. if (isExpandable() && ! isExpanded())
  237. {
  238. g.setColour (findColour (TextEditor::backgroundColourId).contrasting().withAlpha (0.4f));
  239. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  240. .removeFromBottom (expandAreaHeight).withTrimmedLeft (10),
  241. Justification::centredLeft, 1);
  242. }
  243. PropertyComponent::paint (g);
  244. }
  245. void MultiChoicePropertyComponent::resized()
  246. {
  247. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  248. if (isExpandable())
  249. {
  250. bounds.removeFromBottom (5);
  251. auto buttonSlice = bounds.removeFromBottom (10);
  252. expandButton.setSize (10, 10);
  253. expandButton.setCentrePosition (buttonSlice.getCentre());
  254. }
  255. numHidden = 0;
  256. for (auto* b : choiceButtons)
  257. {
  258. if (bounds.getHeight() >= buttonHeight)
  259. {
  260. b->setVisible (true);
  261. b->setBounds (bounds.removeFromTop (buttonHeight).reduced (5, 2));
  262. }
  263. else
  264. {
  265. b->setVisible (false);
  266. ++numHidden;
  267. }
  268. }
  269. }
  270. void MultiChoicePropertyComponent::setExpanded (bool shouldBeExpanded) noexcept
  271. {
  272. if (! isExpandable() || (isExpanded() == shouldBeExpanded))
  273. return;
  274. expanded = shouldBeExpanded;
  275. preferredHeight = expanded ? maxHeight : collapsedHeight;
  276. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  277. propertyPanel->resized();
  278. if (onHeightChange != nullptr)
  279. onHeightChange();
  280. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  281. (float) expandButton.getBounds().getCentreX(),
  282. (float) expandButton.getBounds().getCentreY()));
  283. resized();
  284. }
  285. //==============================================================================
  286. void MultiChoicePropertyComponent::lookAndFeelChanged()
  287. {
  288. auto iconColour = findColour (TextEditor::backgroundColourId).contrasting();
  289. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  290. if (valueWithDefault != nullptr)
  291. {
  292. auto usingDefault = valueWithDefault->isUsingDefault();
  293. for (auto* button : choiceButtons)
  294. updateButtonTickColour (button, usingDefault);
  295. }
  296. }
  297. } // namespace juce