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.

245 lines
8.7KB

  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 MultiChoicePropertyComponent::MultiChoiceRemapperSource : public Value::ValueSource,
  23. private Value::Listener
  24. {
  25. public:
  26. MultiChoiceRemapperSource (const Value& source, var v)
  27. : sourceValue (source),
  28. varToControl (v)
  29. {
  30. sourceValue.addListener (this);
  31. }
  32. var getValue() const override
  33. {
  34. if (auto* arr = sourceValue.getValue().getArray())
  35. if (arr->contains (varToControl))
  36. return 1;
  37. return 0;
  38. }
  39. void setValue (const var& newValue) override
  40. {
  41. if (auto* arr = sourceValue.getValue().getArray())
  42. {
  43. auto newValueInt = static_cast<int> (newValue);
  44. if (newValueInt == 1)
  45. arr->addIfNotAlreadyThere (varToControl);
  46. else
  47. arr->remove (arr->indexOf (varToControl));
  48. }
  49. }
  50. private:
  51. Value sourceValue;
  52. var varToControl;
  53. void valueChanged (Value&) override { sendChangeMessage (true); }
  54. //==============================================================================
  55. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSource)
  56. };
  57. //==============================================================================
  58. class MultiChoicePropertyComponent::MultiChoiceRemapperSourceWithDefault : public Value::ValueSource,
  59. private Value::Listener
  60. {
  61. public:
  62. MultiChoiceRemapperSourceWithDefault (const ValueWithDefault& vwd, var v)
  63. : valueWithDefault (vwd),
  64. sourceValue (valueWithDefault.getPropertyAsValue()),
  65. varToControl (v)
  66. {
  67. sourceValue.addListener (this);
  68. }
  69. var getValue() const override
  70. {
  71. if (auto* arr = valueWithDefault.get().getArray())
  72. if (arr->contains (varToControl))
  73. return 1;
  74. return 0;
  75. }
  76. void setValue (const var& newValue) override
  77. {
  78. if (auto* arr = valueWithDefault.get().getArray())
  79. {
  80. auto newValueInt = static_cast<int> (newValue);
  81. if (newValueInt == 1)
  82. arr->addIfNotAlreadyThere (varToControl);
  83. else
  84. arr->remove (arr->indexOf (varToControl));
  85. }
  86. }
  87. private:
  88. ValueWithDefault valueWithDefault;
  89. Value sourceValue;
  90. var varToControl;
  91. void valueChanged (Value&) override { sendChangeMessage (true); }
  92. //==============================================================================
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoiceRemapperSourceWithDefault)
  94. };
  95. //==============================================================================
  96. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const String& propertyName,
  97. const StringArray& choices,
  98. const Array<var>& correspondingValues)
  99. : PropertyComponent (propertyName, 70)
  100. {
  101. // The array of corresponding values must contain one value for each of the items in
  102. // the choices array!
  103. jassert (choices.size() == correspondingValues.size());
  104. ignoreUnused (correspondingValues);
  105. for (auto choice : choices)
  106. addAndMakeVisible (choiceButtons.add (new ToggleButton (choice)));
  107. maxHeight = (choiceButtons.size() * 25) + 20;
  108. {
  109. Path expandShape;
  110. expandShape.addTriangle ({ 0, 0 }, { 5, 10 }, { 10, 0});
  111. expandButton.setShape (expandShape, true, true, false);
  112. }
  113. expandButton.onClick = [this] { setExpanded (! expanded); };
  114. addAndMakeVisible (expandButton);
  115. lookAndFeelChanged();
  116. }
  117. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const Value& valueToControl,
  118. const String& propertyName,
  119. const StringArray& choices,
  120. const Array<var>& correspondingValues)
  121. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  122. {
  123. // The value to control must be an array!
  124. jassert (valueToControl.getValue().isArray());
  125. for (int i = 0; i < choiceButtons.size(); ++i)
  126. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSource (valueToControl,
  127. correspondingValues[i])));
  128. }
  129. MultiChoicePropertyComponent::MultiChoicePropertyComponent (const ValueWithDefault& valueToControl,
  130. const String& propertyName,
  131. const StringArray& choices,
  132. const Array<var>& correspondingValues)
  133. : MultiChoicePropertyComponent (propertyName, choices, correspondingValues)
  134. {
  135. // The value to control must be an array!
  136. jassert (valueToControl.get().isArray());
  137. for (int i = 0; i < choiceButtons.size(); ++i)
  138. choiceButtons[i]->getToggleStateValue().referTo (Value (new MultiChoiceRemapperSourceWithDefault (valueToControl,
  139. correspondingValues[i])));
  140. }
  141. void MultiChoicePropertyComponent::paint (Graphics& g)
  142. {
  143. g.setColour (findColour (TextEditor::backgroundColourId));
  144. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (*this));
  145. if (! expanded)
  146. {
  147. g.setColour (findColour (PropertyComponent::labelTextColourId).withAlpha (0.4f));
  148. g.drawFittedText ("+ " + String (numHidden) + " more", getLookAndFeel().getPropertyComponentContentPosition (*this)
  149. .removeFromBottom (20).withTrimmedLeft (10),
  150. Justification::centredLeft, 1);
  151. }
  152. PropertyComponent::paint (g);
  153. }
  154. void MultiChoicePropertyComponent::resized()
  155. {
  156. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  157. bounds.removeFromBottom (5);
  158. expandButton.setBounds (bounds.removeFromBottom (10));
  159. numHidden = 0;
  160. for (auto* b : choiceButtons)
  161. {
  162. if (bounds.getHeight() >= 25)
  163. {
  164. b->setVisible (true);
  165. b->setBounds (bounds.removeFromTop (25).reduced (5, 2));
  166. }
  167. else
  168. {
  169. b->setVisible (false);
  170. ++numHidden;
  171. }
  172. }
  173. }
  174. void MultiChoicePropertyComponent::setExpanded (bool isExpanded) noexcept
  175. {
  176. if (expanded == isExpanded)
  177. return;
  178. expanded = isExpanded;
  179. preferredHeight = expanded ? maxHeight : 70;
  180. if (auto* propertyPanel = findParentComponentOfClass<PropertyPanel>())
  181. propertyPanel->resized();
  182. if (onHeightChange != nullptr)
  183. onHeightChange();
  184. expandButton.setTransform (AffineTransform::rotation (expanded ? MathConstants<float>::pi : MathConstants<float>::twoPi,
  185. (float) expandButton.getBounds().getCentreX(),
  186. (float) expandButton.getBounds().getCentreY()));
  187. }
  188. //==============================================================================
  189. void MultiChoicePropertyComponent::lookAndFeelChanged()
  190. {
  191. auto iconColour = findColour (PropertyComponent::labelTextColourId);
  192. expandButton.setColours (iconColour, iconColour.darker(), iconColour.darker());
  193. }
  194. } // namespace juce