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.

362 lines
13KB

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