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.

341 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. class StringComparator
  23. {
  24. public:
  25. static int compareElements (var first, var second)
  26. {
  27. if (first.toString() > second.toString())
  28. return 1;
  29. else if (first.toString() < second.toString())
  30. return -1;
  31. return 0;
  32. }
  33. };
  34. //==============================================================================
  35. class MultiChoicePropertyComponent::MultiChoiceRemapperSource : public Value::ValueSource,
  36. private Value::Listener
  37. {
  38. public:
  39. MultiChoiceRemapperSource (const Value& source, var v, int c)
  40. : sourceValue (source),
  41. varToControl (v),
  42. maxChoices (c)
  43. {
  44. sourceValue.addListener (this);
  45. }
  46. var getValue() const override
  47. {
  48. if (auto* arr = sourceValue.getValue().getArray())
  49. if (arr->contains (varToControl))
  50. return true;
  51. return false;
  52. }
  53. void setValue (const var& newValue) override
  54. {
  55. if (auto* arr = sourceValue.getValue().getArray())
  56. {
  57. auto temp = *arr;
  58. if (static_cast<bool> (newValue))
  59. {
  60. if (temp.addIfNotAlreadyThere (varToControl) && (maxChoices != -1) && (temp.size() > maxChoices))
  61. temp.remove (temp.size() - 2);
  62. }
  63. else
  64. {
  65. temp.remove (arr->indexOf (varToControl));
  66. }
  67. StringComparator c;
  68. temp.sort (c);
  69. sourceValue = temp;
  70. }
  71. }
  72. private:
  73. Value sourceValue;
  74. var varToControl;
  75. int maxChoices;
  76. //==============================================================================
  77. void valueChanged (Value&) override { sendChangeMessage (true); }
  78. //==============================================================================
  79. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSource)
  80. };
  81. //==============================================================================
  82. class MultiChoicePropertyComponent::MultiChoiceRemapperSourceWithDefault : public Value::ValueSource,
  83. private Value::Listener
  84. {
  85. public:
  86. MultiChoiceRemapperSourceWithDefault (ValueWithDefault& vwd, var v, int c, ToggleButton* b)
  87. : valueWithDefault (vwd),
  88. varToControl (v),
  89. sourceValue (valueWithDefault.getPropertyAsValue()),
  90. maxChoices (c),
  91. buttonToControl (b)
  92. {
  93. sourceValue.addListener (this);
  94. }
  95. var getValue() const override
  96. {
  97. auto v = valueWithDefault.get();
  98. if (auto* arr = v.getArray())
  99. {
  100. if (arr->contains (varToControl))
  101. {
  102. updateButtonTickColour();
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. void setValue (const var& newValue) override
  109. {
  110. auto v = valueWithDefault.get();
  111. OptionalScopedPointer<Array<var>> arrayToControl;
  112. if (valueWithDefault.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 (valueWithDefault.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. valueWithDefault = temp;
  140. if (temp.size() == 0)
  141. valueWithDefault.resetToDefault();
  142. }
  143. }
  144. private:
  145. ValueWithDefault& valueWithDefault;
  146. var varToControl;
  147. Value sourceValue;
  148. int maxChoices;
  149. ToggleButton* buttonToControl;
  150. //==============================================================================
  151. void valueChanged (Value&) override { sendChangeMessage (true); }
  152. void updateButtonTickColour() const noexcept
  153. {
  154. auto alpha = valueWithDefault.isUsingDefault() ? 0.4f : 1.0f;
  155. auto baseColour = buttonToControl->findColour (ToggleButton::tickColourId);
  156. buttonToControl->setColour (ToggleButton::tickColourId, baseColour.withAlpha (alpha));
  157. }
  158. //==============================================================================
  159. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSourceWithDefault)
  160. };
  161. //==============================================================================
  162. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const String& propertyName,
  163. const StringArray& choices,
  164. const Array<var>& correspondingValues)
  165. : PropertyComponent (propertyName, 70)
  166. {
  167. // The array of corresponding values must contain one value for each of the items in
  168. // the choices array!
  169. jassert (choices.size() == correspondingValues.size());
  170. ignoreUnused (correspondingValues);
  171. for (auto choice : choices)
  172. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  173. maxHeight = (choiceButtons.size() * 25) + 20;
  174. {
  175. Path expandShape;
  176. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  177. expandButton.setShape (expandShape, true, true, false);
  178. }
  179. expandButton.onClick = [this] { setExpanded (! expanded); };
  180. addAndMakeVisible (expandButton);
  181. lookAndFeelChanged();
  182. }
  183. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  184. const String& propertyName,
  185. const StringArray& choices,
  186. const Array<var>& correspondingValues,
  187. int maxChoices)
  188. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  189. {
  190. // The value to control must be an array!
  191. jassert (valueToControl.getValue().isArray());
  192. for (int i = 0; i < choiceButtons.size(); ++i)
  193. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  194. correspondingValues[i],
  195. maxChoices)));
  196. }
  197. MultiChoicePropertyComponent::MultiChoicePropertyComponent (ValueWithDefault& 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.get().isArray());
  206. for (int i = 0; i < choiceButtons.size(); ++i)
  207. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueToControl,
  208. correspondingValues[i],
  209. maxChoices,
  210. choiceButtons[i])));
  211. valueToControl.onDefaultChange = [this] { repaint(); };
  212. }
  213. void MultiChoicePropertyComponent::paint (Graphics& g)
  214. {
  215. g.setColour (findColour (TextEditor::backgroundColourId));
  216. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  217. if (! expanded)
  218. {
  219. g.setColour (findColour (TextEditor::backgroundColourId).contrasting().withAlpha (0.4f));
  220. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  221. .removeFromBottom (20).withTrimmedLeft (10),
  222. Justification::centredLeft, 1);
  223. }
  224. PropertyComponent::paint (g);
  225. }
  226. void MultiChoicePropertyComponent::resized()
  227. {
  228. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  229. bounds.removeFromBottom (5);
  230. auto buttonSlice = bounds.removeFromBottom (10);
  231. expandButton.setSize (10, 10);
  232. expandButton.setCentrePosition (buttonSlice.getCentre());
  233. numHidden = 0;
  234. for (auto* b : choiceButtons)
  235. {
  236. if (bounds.getHeight() >= 25)
  237. {
  238. b->setVisible (true);
  239. b->setBounds (bounds.removeFromTop (25).reduced (5, 2));
  240. }
  241. else
  242. {
  243. b->setVisible (false);
  244. ++numHidden;
  245. }
  246. }
  247. }
  248. void MultiChoicePropertyComponent::setExpanded (bool isExpanded) noexcept
  249. {
  250. if (expanded == isExpanded)
  251. return;
  252. expanded = isExpanded;
  253. preferredHeight = expanded ? maxHeight : 70;
  254. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  255. propertyPanel->resized();
  256. if (onHeightChange != nullptr)
  257. onHeightChange();
  258. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  259. (float) expandButton.getBounds().getCentreX(),
  260. (float) expandButton.getBounds().getCentreY()));
  261. resized();
  262. }
  263. //==============================================================================
  264. void MultiChoicePropertyComponent::lookAndFeelChanged()
  265. {
  266. auto iconColour = findColour (TextEditor::backgroundColourId).contrasting();
  267. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  268. }
  269. } // namespace juce