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.

285 lines
9.0KB

  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 ChoicePropertyComponent::RemapperValueSource : public Value::ValueSource,
  17. private Value::Listener
  18. {
  19. public:
  20. RemapperValueSource (const Value& source, const Array<var>& map)
  21. : sourceValue (source),
  22. mappings (map)
  23. {
  24. sourceValue.addListener (this);
  25. }
  26. var getValue() const override
  27. {
  28. auto targetValue = sourceValue.getValue();
  29. for (auto& map : mappings)
  30. if (map.equalsWithSameType (targetValue))
  31. return mappings.indexOf (map) + 1;
  32. return mappings.indexOf (targetValue) + 1;
  33. }
  34. void setValue (const var& newValue) override
  35. {
  36. auto remappedVal = mappings [static_cast<int> (newValue) - 1];
  37. if (! remappedVal.equalsWithSameType (sourceValue))
  38. sourceValue = remappedVal;
  39. }
  40. protected:
  41. Value sourceValue;
  42. Array<var> mappings;
  43. void valueChanged (Value&) override { sendChangeMessage (true); }
  44. //==============================================================================
  45. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource)
  46. };
  47. //==============================================================================
  48. class ChoicePropertyComponent::RemapperValueSourceWithDefault : public Value::ValueSource,
  49. private Value::Listener
  50. {
  51. public:
  52. RemapperValueSourceWithDefault (ValueWithDefault* vwd, const Array<var>& map)
  53. : valueWithDefault (vwd),
  54. sourceValue (valueWithDefault->getPropertyAsValue()),
  55. mappings (map)
  56. {
  57. sourceValue.addListener (this);
  58. }
  59. var getValue() const override
  60. {
  61. if (valueWithDefault == nullptr)
  62. return {};
  63. if (valueWithDefault->isUsingDefault())
  64. return -1;
  65. auto targetValue = sourceValue.getValue();
  66. for (auto map : mappings)
  67. if (map.equalsWithSameType (targetValue))
  68. return mappings.indexOf (map) + 1;
  69. return mappings.indexOf (targetValue) + 1;
  70. }
  71. void setValue (const var& newValue) override
  72. {
  73. if (valueWithDefault == nullptr)
  74. return;
  75. auto newValueInt = static_cast<int> (newValue);
  76. if (newValueInt == -1)
  77. {
  78. valueWithDefault->resetToDefault();
  79. }
  80. else
  81. {
  82. auto remappedVal = mappings [newValueInt - 1];
  83. if (! remappedVal.equalsWithSameType (sourceValue))
  84. *valueWithDefault = remappedVal;
  85. }
  86. }
  87. private:
  88. void valueChanged (Value&) override { sendChangeMessage (true); }
  89. WeakReference<ValueWithDefault> valueWithDefault;
  90. Value sourceValue;
  91. Array<var> mappings;
  92. //==============================================================================
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSourceWithDefault)
  94. };
  95. //==============================================================================
  96. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  97. : PropertyComponent (name),
  98. isCustomClass (true)
  99. {
  100. }
  101. ChoicePropertyComponent::ChoicePropertyComponent (const String& name,
  102. const StringArray& choiceList,
  103. const Array<var>& correspondingValues)
  104. : PropertyComponent (name),
  105. choices (choiceList)
  106. {
  107. // The array of corresponding values must contain one value for each of the items in
  108. // the choices array!
  109. jassert (correspondingValues.size() == choices.size());
  110. ignoreUnused (correspondingValues);
  111. }
  112. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  113. const String& name,
  114. const StringArray& choiceList,
  115. const Array<var>& correspondingValues)
  116. : ChoicePropertyComponent (name, choiceList, correspondingValues)
  117. {
  118. createComboBox();
  119. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSource (valueToControl,
  120. correspondingValues)));
  121. }
  122. ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToControl,
  123. const String& name,
  124. const StringArray& choiceList,
  125. const Array<var>& correspondingValues)
  126. : ChoicePropertyComponent (name, choiceList, correspondingValues)
  127. {
  128. valueWithDefault = &valueToControl;
  129. createComboBoxWithDefault (choiceList [correspondingValues.indexOf (valueWithDefault->getDefault())]);
  130. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSourceWithDefault (valueWithDefault,
  131. correspondingValues)));
  132. valueWithDefault->onDefaultChange = [this, choiceList, correspondingValues]
  133. {
  134. auto selectedId = comboBox.getSelectedId();
  135. comboBox.clear();
  136. createComboBoxWithDefault (choiceList [correspondingValues.indexOf (valueWithDefault->getDefault())]);
  137. comboBox.setSelectedId (selectedId);
  138. };
  139. }
  140. ChoicePropertyComponent::ChoicePropertyComponent (ValueWithDefault& valueToControl,
  141. const String& name)
  142. : PropertyComponent (name),
  143. choices ({ "Enabled", "Disabled" })
  144. {
  145. valueWithDefault = &valueToControl;
  146. createComboBoxWithDefault (valueWithDefault->getDefault() ? "Enabled" : "Disabled");
  147. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSourceWithDefault (valueWithDefault,
  148. { true, false })));
  149. valueWithDefault->onDefaultChange = [this]
  150. {
  151. auto selectedId = comboBox.getSelectedId();
  152. comboBox.clear();
  153. createComboBoxWithDefault (valueWithDefault->getDefault() ? "Enabled" : "Disabled");
  154. comboBox.setSelectedId (selectedId);
  155. };
  156. }
  157. ChoicePropertyComponent::~ChoicePropertyComponent()
  158. {
  159. if (valueWithDefault != nullptr)
  160. valueWithDefault->onDefaultChange = nullptr;
  161. }
  162. //==============================================================================
  163. void ChoicePropertyComponent::createComboBox()
  164. {
  165. addAndMakeVisible (comboBox);
  166. for (auto choice : choices)
  167. {
  168. if (choice.isNotEmpty())
  169. comboBox.addItem (choice, choices.indexOf (choice) + 1);
  170. else
  171. comboBox.addSeparator();
  172. }
  173. comboBox.setEditableText (false);
  174. }
  175. void ChoicePropertyComponent::createComboBoxWithDefault (const String& defaultString)
  176. {
  177. addAndMakeVisible (comboBox);
  178. comboBox.addItem ("Default" + (defaultString.isNotEmpty() ? " (" + defaultString + ")" : ""), -1);
  179. for (auto choice : choices)
  180. {
  181. if (choice.isNotEmpty())
  182. comboBox.addItem (choice, choices.indexOf (choice) + 1);
  183. else
  184. comboBox.addSeparator();
  185. }
  186. comboBox.setEditableText (false);
  187. }
  188. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  189. {
  190. jassertfalse; // you need to override this method in your subclass!
  191. }
  192. int ChoicePropertyComponent::getIndex() const
  193. {
  194. jassertfalse; // you need to override this method in your subclass!
  195. return -1;
  196. }
  197. const StringArray& ChoicePropertyComponent::getChoices() const
  198. {
  199. return choices;
  200. }
  201. //==============================================================================
  202. void ChoicePropertyComponent::refresh()
  203. {
  204. if (isCustomClass)
  205. {
  206. if (! comboBox.isVisible())
  207. {
  208. createComboBox();
  209. comboBox.onChange = [this] { changeIndex(); };
  210. }
  211. comboBox.setSelectedId (getIndex() + 1, dontSendNotification);
  212. }
  213. }
  214. void ChoicePropertyComponent::changeIndex()
  215. {
  216. if (isCustomClass)
  217. {
  218. auto newIndex = comboBox.getSelectedId() - 1;
  219. if (newIndex != getIndex())
  220. setIndex (newIndex);
  221. }
  222. }
  223. } // namespace juce