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.

104 lines
4.2KB

  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. Provides a class of AudioProcessorParameter that can be used to select
  17. an indexed, named choice from a list.
  18. @see AudioParameterFloat, AudioParameterInt, AudioParameterBool
  19. @tags{Audio}
  20. */
  21. class JUCE_API AudioParameterChoice : public RangedAudioParameter
  22. {
  23. public:
  24. /** Creates a AudioParameterChoice with the specified parameters.
  25. @param parameterID The parameter ID to use
  26. @param parameterName The parameter name to use
  27. @param choices The set of choices to use
  28. @param defaultItemIndex The index of the default choice
  29. @param parameterLabel An optional label for the parameter's value
  30. @param stringFromIndex An optional lambda function that converts a choice
  31. index to a string with a maximum length. This may
  32. be used by hosts to display the parameter's value.
  33. @param indexFromString An optional lambda function that parses a string and
  34. converts it into a choice index. Some hosts use this
  35. to allow users to type in parameter values.
  36. */
  37. AudioParameterChoice (const String& parameterID, const String& parameterName,
  38. const StringArray& choices,
  39. int defaultItemIndex,
  40. const String& parameterLabel = String(),
  41. std::function<String(int index, int maximumStringLength)> stringFromIndex = nullptr,
  42. std::function<int(const String& text)> indexFromString = nullptr);
  43. /** Destructor. */
  44. ~AudioParameterChoice() override;
  45. /** Returns the current index of the selected item. */
  46. int getIndex() const noexcept { return roundToInt (value.load()); }
  47. /** Returns the current index of the selected item. */
  48. operator int() const noexcept { return getIndex(); }
  49. /** Returns the name of the currently selected item. */
  50. String getCurrentChoiceName() const noexcept { return choices[getIndex()]; }
  51. /** Returns the name of the currently selected item. */
  52. operator String() const noexcept { return getCurrentChoiceName(); }
  53. /** Changes the selected item to a new index. */
  54. AudioParameterChoice& operator= (int newValue);
  55. /** Returns the range of values that the parameter can take. */
  56. const NormalisableRange<float>& getNormalisableRange() const override { return range; }
  57. /** Provides access to the list of choices that this parameter is working with. */
  58. const StringArray choices;
  59. protected:
  60. /** Override this method if you are interested in receiving callbacks
  61. when the parameter value changes.
  62. */
  63. virtual void valueChanged (int newValue);
  64. private:
  65. //==============================================================================
  66. float getValue() const override;
  67. void setValue (float newValue) override;
  68. float getDefaultValue() const override;
  69. int getNumSteps() const override;
  70. bool isDiscrete() const override;
  71. String getText (float, int) const override;
  72. float getValueForText (const String&) const override;
  73. const NormalisableRange<float> range;
  74. std::atomic<float> value;
  75. const float defaultValue;
  76. std::function<String(int, int)> stringFromIndexFunction;
  77. std::function<int(const String&)> indexFromStringFunction;
  78. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioParameterChoice)
  79. };
  80. } // namespace juce