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.

101 lines
3.6KB

  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. AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nameToUse,
  22. bool def, const String& labelToUse,
  23. std::function<String (bool, int)> stringFromBool,
  24. std::function<bool (const String&)> boolFromString)
  25. : RangedAudioParameter (idToUse, nameToUse, labelToUse),
  26. value (def ? 1.0f : 0.0f),
  27. defaultValue (value),
  28. stringFromBoolFunction (stringFromBool),
  29. boolFromStringFunction (boolFromString)
  30. {
  31. if (stringFromBoolFunction == nullptr)
  32. stringFromBoolFunction = [] (bool v, int) { return v ? TRANS("On") : TRANS("Off"); };
  33. if (boolFromStringFunction == nullptr)
  34. {
  35. StringArray onStrings;
  36. onStrings.add (TRANS("on"));
  37. onStrings.add (TRANS("yes"));
  38. onStrings.add (TRANS("true"));
  39. StringArray offStrings;
  40. offStrings.add (TRANS("off"));
  41. offStrings.add (TRANS("no"));
  42. offStrings.add (TRANS("false"));
  43. boolFromStringFunction = [onStrings, offStrings] (const String& text)
  44. {
  45. String lowercaseText (text.toLowerCase());
  46. for (auto& testText : onStrings)
  47. if (lowercaseText == testText)
  48. return true;
  49. for (auto& testText : offStrings)
  50. if (lowercaseText == testText)
  51. return false;
  52. return text.getIntValue() != 0;
  53. };
  54. }
  55. }
  56. AudioParameterBool::~AudioParameterBool() {}
  57. float AudioParameterBool::getValue() const { return value; }
  58. void AudioParameterBool::setValue (float newValue) { value = newValue; valueChanged (get()); }
  59. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  60. int AudioParameterBool::getNumSteps() const { return 2; }
  61. bool AudioParameterBool::isDiscrete() const { return true; }
  62. bool AudioParameterBool::isBoolean() const { return true; }
  63. void AudioParameterBool::valueChanged (bool) {}
  64. float AudioParameterBool::getValueForText (const String& text) const
  65. {
  66. return boolFromStringFunction (text) ? 1.0f : 0.0f;
  67. }
  68. String AudioParameterBool::getText (float v, int maximumLength) const
  69. {
  70. return stringFromBoolFunction (v >= 0.5f, maximumLength);
  71. }
  72. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  73. {
  74. if (get() != newValue)
  75. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  76. return *this;
  77. }
  78. } // namespace juce