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.

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