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.

juce_ChoicePropertyComponent.cpp 4.5KB

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