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.

160 lines
4.5KB

  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. class ChoicePropertyComponent::RemapperValueSource : public Value::ValueSource,
  22. private Value::Listener
  23. {
  24. public:
  25. RemapperValueSource (const Value& source, const Array<var>& map)
  26. : sourceValue (source), mappings (map)
  27. {
  28. sourceValue.addListener (this);
  29. }
  30. var getValue() const
  31. {
  32. const var targetValue (sourceValue.getValue());
  33. for (int i = 0; i < mappings.size(); ++i)
  34. if (mappings.getReference(i).equalsWithSameType (targetValue))
  35. return i + 1;
  36. return mappings.indexOf (targetValue) + 1;
  37. }
  38. void setValue (const var& newValue)
  39. {
  40. const var remappedVal (mappings [static_cast<int> (newValue) - 1]);
  41. if (! remappedVal.equalsWithSameType (sourceValue))
  42. sourceValue = remappedVal;
  43. }
  44. protected:
  45. Value sourceValue;
  46. Array<var> mappings;
  47. void valueChanged (Value&)
  48. {
  49. sendChangeMessage (true);
  50. }
  51. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource)
  52. };
  53. //==============================================================================
  54. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  55. : PropertyComponent (name),
  56. isCustomClass (true)
  57. {
  58. }
  59. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  60. const String& name,
  61. const StringArray& choiceList,
  62. const Array<var>& correspondingValues)
  63. : PropertyComponent (name),
  64. choices (choiceList),
  65. isCustomClass (false)
  66. {
  67. // The array of corresponding values must contain one value for each of the items in
  68. // the choices array!
  69. jassert (correspondingValues.size() == choices.size());
  70. createComboBox();
  71. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSource (valueToControl,
  72. correspondingValues)));
  73. }
  74. ChoicePropertyComponent::~ChoicePropertyComponent()
  75. {
  76. }
  77. //==============================================================================
  78. void ChoicePropertyComponent::createComboBox()
  79. {
  80. addAndMakeVisible (comboBox);
  81. for (int i = 0; i < choices.size(); ++i)
  82. {
  83. if (choices[i].isNotEmpty())
  84. comboBox.addItem (choices[i], i + 1);
  85. else
  86. comboBox.addSeparator();
  87. }
  88. comboBox.setEditableText (false);
  89. }
  90. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  91. {
  92. jassertfalse; // you need to override this method in your subclass!
  93. }
  94. int ChoicePropertyComponent::getIndex() const
  95. {
  96. jassertfalse; // you need to override this method in your subclass!
  97. return -1;
  98. }
  99. const StringArray& ChoicePropertyComponent::getChoices() const
  100. {
  101. return choices;
  102. }
  103. //==============================================================================
  104. void ChoicePropertyComponent::refresh()
  105. {
  106. if (isCustomClass)
  107. {
  108. if (! comboBox.isVisible())
  109. {
  110. createComboBox();
  111. comboBox.addListener (this);
  112. }
  113. comboBox.setSelectedId (getIndex() + 1, dontSendNotification);
  114. }
  115. }
  116. void ChoicePropertyComponent::comboBoxChanged (ComboBox*)
  117. {
  118. if (isCustomClass)
  119. {
  120. const int newIndex = comboBox.getSelectedId() - 1;
  121. if (newIndex != getIndex())
  122. setIndex (newIndex);
  123. }
  124. }
  125. } // namespace juce