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.

156 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__
  19. #define __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__
  20. //==============================================================================
  21. /** A ValueSource that remaps specific values to new values.
  22. */
  23. class ValueRemapperSource : public Value::ValueSource,
  24. public Value::Listener
  25. {
  26. public:
  27. ValueRemapperSource (const Value& sourceValue_)
  28. : sourceValue (sourceValue_)
  29. {
  30. sourceValue.addListener (this);
  31. }
  32. ValueRemapperSource (const Value& sourceValue_, const char** mappings_)
  33. : sourceValue (sourceValue_)
  34. {
  35. addMappings (mappings_);
  36. sourceValue.addListener (this);
  37. }
  38. ~ValueRemapperSource() {}
  39. void addMappings (const char** values)
  40. {
  41. while (values[0] != 0 && values[1] != 0)
  42. {
  43. addMapping (values[0], values[1]);
  44. values += 2;
  45. }
  46. }
  47. void addMapping (const var& sourceValue_, const var& remappedValue)
  48. {
  49. mappings.add (sourceValue_);
  50. mappings.add (remappedValue);
  51. }
  52. const var getValue() const
  53. {
  54. const var sourceVar (sourceValue.getValue());
  55. for (int i = 0; i < mappings.size(); i += 2)
  56. if (sourceVar == mappings.getReference(i))
  57. return mappings.getReference (i + 1);
  58. return sourceVar;
  59. }
  60. void setValue (const var& newValue)
  61. {
  62. var remappedVal (newValue);
  63. for (int i = 1; i < mappings.size(); i += 2)
  64. {
  65. if (newValue == mappings.getReference(i))
  66. {
  67. remappedVal = mappings.getReference (i - 1);
  68. break;
  69. }
  70. }
  71. if (remappedVal != sourceValue)
  72. sourceValue = remappedVal;
  73. }
  74. void valueChanged (Value&)
  75. {
  76. sendChangeMessage (true);
  77. }
  78. //==============================================================================
  79. juce_UseDebuggingNewOperator
  80. protected:
  81. Value sourceValue;
  82. Array<var> mappings;
  83. ValueRemapperSource (const ValueRemapperSource&);
  84. const ValueRemapperSource& operator= (const ValueRemapperSource&);
  85. };
  86. //==============================================================================
  87. /**
  88. */
  89. class IntegerValueSource : public Value::ValueSource,
  90. public Value::Listener
  91. {
  92. public:
  93. IntegerValueSource (const Value& sourceValue_)
  94. : sourceValue (sourceValue_)
  95. {
  96. sourceValue.addListener (this);
  97. }
  98. ~IntegerValueSource() {}
  99. const var getValue() const
  100. {
  101. return (int) sourceValue.getValue();
  102. }
  103. void setValue (const var& newValue)
  104. {
  105. const var newVal ((int) newValue);
  106. if (newVal != sourceValue)
  107. sourceValue = newVal;
  108. }
  109. void valueChanged (Value&)
  110. {
  111. sendChangeMessage (true);
  112. }
  113. //==============================================================================
  114. juce_UseDebuggingNewOperator
  115. protected:
  116. Value sourceValue;
  117. IntegerValueSource (const IntegerValueSource&);
  118. const IntegerValueSource& operator= (const IntegerValueSource&);
  119. };
  120. #endif // __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__