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.

350 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. //==============================================================================
  146. void valueChanged (Value&) override { sendChangeMessage (true); }
  147. void updateButtonTickColour() const noexcept
  148. {
  149. auto alpha = valueWithDefault->isUsingDefault() ? 0.4f : 1.0f;
  150. auto baseColour = buttonToControl->findColour (ToggleButton::tickColourId);
  151. buttonToControl->setColour (ToggleButton::tickColourId, baseColour.withAlpha (alpha));
  152. }
  153. //==============================================================================
  154. ValueWithDefault* valueWithDefault;
  155. var varToControl;
  156. Value sourceValue;
  157. int maxChoices;
  158. ToggleButton* buttonToControl;
  159. //==============================================================================
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSourceWithDefault)
  161. };
  162. //==============================================================================
  163. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const String& propertyName,
  164. const StringArray& choices,
  165. const Array<var>& correspondingValues)
  166. : PropertyComponent (propertyName, 70)
  167. {
  168. // The array of corresponding values must contain one value for each of the items in
  169. // the choices array!
  170. jassert (choices.size() == correspondingValues.size());
  171. ignoreUnused (correspondingValues);
  172. for (auto choice : choices)
  173. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  174. maxHeight = (choiceButtons.size() * 25) + 20;
  175. {
  176. Path expandShape;
  177. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  178. expandButton.setShape (expandShape, true, true, false);
  179. }
  180. expandButton.onClick = [this] { setExpanded (! expanded); };
  181. addAndMakeVisible (expandButton);
  182. lookAndFeelChanged();
  183. }
  184. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  185. const String& propertyName,
  186. const StringArray& choices,
  187. const Array<var>& correspondingValues,
  188. int maxChoices)
  189. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  190. {
  191. // The value to control must be an array!
  192. jassert (valueToControl.getValue().isArray());
  193. for (int i = 0; i < choiceButtons.size(); ++i)
  194. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  195. correspondingValues[i],
  196. maxChoices)));
  197. }
  198. MultiChoicePropertyComponent::MultiChoicePropertyComponent (ValueWithDefault& valueToControl,
  199. const String& propertyName,
  200. const StringArray& choices,
  201. const Array<var>& correspondingValues,
  202. int maxChoices)
  203. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  204. {
  205. valueWithDefault = &valueToControl;
  206. // The value to control must be an array!
  207. jassert (valueWithDefault->get().isArray());
  208. for (int i = 0; i < choiceButtons.size(); ++i)
  209. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueWithDefault,
  210. correspondingValues[i],
  211. maxChoices,
  212. choiceButtons[i])));
  213. valueWithDefault->onDefaultChange = [this] { repaint(); };
  214. }
  215. MultiChoicePropertyComponent::~MultiChoicePropertyComponent()
  216. {
  217. if (valueWithDefault != nullptr)
  218. valueWithDefault->onDefaultChange = nullptr;
  219. }
  220. void MultiChoicePropertyComponent::paint (Graphics& g)
  221. {
  222. g.setColour (findColour (TextEditor::backgroundColourId));
  223. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  224. if (! expanded)
  225. {
  226. g.setColour (findColour (TextEditor::backgroundColourId).contrasting().withAlpha (0.4f));
  227. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  228. .removeFromBottom (20).withTrimmedLeft (10),
  229. Justification::centredLeft, 1);
  230. }
  231. PropertyComponent::paint (g);
  232. }
  233. void MultiChoicePropertyComponent::resized()
  234. {
  235. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  236. bounds.removeFromBottom (5);
  237. auto buttonSlice = bounds.removeFromBottom (10);
  238. expandButton.setSize (10, 10);
  239. expandButton.setCentrePosition (buttonSlice.getCentre());
  240. numHidden = 0;
  241. for (auto* b : choiceButtons)
  242. {
  243. if (bounds.getHeight() >= 25)
  244. {
  245. b->setVisible (true);
  246. b->setBounds (bounds.removeFromTop (25).reduced (5, 2));
  247. }
  248. else
  249. {
  250. b->setVisible (false);
  251. ++numHidden;
  252. }
  253. }
  254. }
  255. void MultiChoicePropertyComponent::setExpanded (bool isExpanded) noexcept
  256. {
  257. if (expanded == isExpanded)
  258. return;
  259. expanded = isExpanded;
  260. preferredHeight = expanded ? maxHeight : 70;
  261. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  262. propertyPanel->resized();
  263. if (onHeightChange != nullptr)
  264. onHeightChange();
  265. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  266. (float) expandButton.getBounds().getCentreX(),
  267. (float) expandButton.getBounds().getCentreY()));
  268. resized();
  269. }
  270. //==============================================================================
  271. void MultiChoicePropertyComponent::lookAndFeelChanged()
  272. {
  273. auto iconColour = findColour (TextEditor::backgroundColourId).contrasting();
  274. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  275. }
  276. } // namespace juce