Audio plugin host https://kx.studio/carla
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.

354 lines
12KB

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