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.

150 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class GenericEditor : public AudioProcessorEditor,
  18. public SliderListener,
  19. private Timer
  20. {
  21. public:
  22. enum
  23. {
  24. kParamSliderHeight = 40,
  25. kParamLabelWidth = 80,
  26. kParamSliderWidth = 300
  27. };
  28. GenericEditor (AudioProcessor& parent)
  29. : AudioProcessorEditor (parent),
  30. noParameterLabel ("noparam", "No parameters available")
  31. {
  32. const OwnedArray<AudioProcessorParameter>& params = parent.getParameters();
  33. for (int i = 0; i < params.size(); ++i)
  34. {
  35. if (const AudioParameterFloat* param = dynamic_cast<AudioParameterFloat*>(params[i]))
  36. {
  37. const bool isLevelMeter = (((param->category & 0xffff0000) >> 16) == 2);
  38. if (isLevelMeter)
  39. continue;
  40. Slider* aSlider;
  41. paramSliders.add (aSlider = new Slider (param->name));
  42. aSlider->setRange (param->range.start, param->range.end);
  43. aSlider->setSliderStyle (Slider::LinearHorizontal);
  44. aSlider->setValue (dynamic_cast<const AudioProcessorParameter*>(param)->getValue());
  45. aSlider->addListener (this);
  46. addAndMakeVisible (aSlider);
  47. Label* aLabel;
  48. paramLabels.add (aLabel = new Label (param->name, param->name));
  49. addAndMakeVisible (aLabel);
  50. }
  51. }
  52. noParameterLabel.setJustificationType (Justification::horizontallyCentred | Justification::verticallyCentred);
  53. noParameterLabel.setFont (noParameterLabel.getFont().withStyle (Font::italic));
  54. setSize (kParamSliderWidth + kParamLabelWidth,
  55. jmax (1, kParamSliderHeight * paramSliders.size()));
  56. if (paramSliders.size() == 0)
  57. addAndMakeVisible (noParameterLabel);
  58. else
  59. startTimer (100);
  60. }
  61. ~GenericEditor()
  62. {
  63. }
  64. void resized() override
  65. {
  66. Rectangle<int> r = getLocalBounds();
  67. noParameterLabel.setBounds (r);
  68. for (int i = 0; i < paramSliders.size(); ++i)
  69. {
  70. Rectangle<int> paramBounds = r.removeFromTop (kParamSliderHeight);
  71. Rectangle<int> labelBounds = paramBounds.removeFromLeft (kParamLabelWidth);
  72. paramLabels[i]->setBounds (labelBounds);
  73. paramSliders[i]->setBounds (paramBounds);
  74. }
  75. }
  76. void paint (Graphics& g) override
  77. {
  78. g.fillAll (Colours::white);
  79. }
  80. //==============================================================================
  81. void sliderValueChanged (Slider* slider) override
  82. {
  83. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  84. {
  85. if (slider->isMouseButtonDown())
  86. param->setValueNotifyingHost ((float) slider->getValue());
  87. else
  88. param->setValue ((float) slider->getValue());
  89. }
  90. }
  91. void sliderDragStarted (Slider* slider) override
  92. {
  93. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  94. param->beginChangeGesture();
  95. }
  96. void sliderDragEnded (Slider* slider) override
  97. {
  98. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  99. param->endChangeGesture();
  100. }
  101. private:
  102. void timerCallback() override
  103. {
  104. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  105. for (int i = 0; i < params.size(); ++i)
  106. {
  107. if (const AudioProcessorParameter* param = params[i])
  108. {
  109. if (i < paramSliders.size())
  110. paramSliders[i]->setValue (param->getValue());
  111. }
  112. }
  113. }
  114. AudioProcessorParameter* getParameterForSlider (Slider* slider)
  115. {
  116. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  117. return params[paramSliders.indexOf (slider)];
  118. }
  119. Label noParameterLabel;
  120. OwnedArray<Slider> paramSliders;
  121. OwnedArray<Label> paramLabels;
  122. };