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.

146 lines
4.8KB

  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. Slider* aSlider;
  38. paramSliders.add (aSlider = new Slider (param->name));
  39. aSlider->setRange (param->range.start, param->range.end);
  40. aSlider->setSliderStyle (Slider::LinearHorizontal);
  41. aSlider->setValue (dynamic_cast<const AudioProcessorParameter*>(param)->getValue());
  42. aSlider->addListener (this);
  43. addAndMakeVisible (aSlider);
  44. Label* aLabel;
  45. paramLabels.add (aLabel = new Label (param->name, param->name));
  46. addAndMakeVisible (aLabel);
  47. }
  48. }
  49. noParameterLabel.setJustificationType (Justification::horizontallyCentred | Justification::verticallyCentred);
  50. noParameterLabel.setFont (noParameterLabel.getFont().withStyle (Font::italic));
  51. setSize (kParamSliderWidth + kParamLabelWidth,
  52. jmax (1, kParamSliderHeight * paramSliders.size()));
  53. if (paramSliders.size() == 0)
  54. addAndMakeVisible (noParameterLabel);
  55. else
  56. startTimer (100);
  57. }
  58. ~GenericEditor()
  59. {
  60. }
  61. void resized() override
  62. {
  63. Rectangle<int> r = getLocalBounds();
  64. noParameterLabel.setBounds (r);
  65. for (int i = 0; i < paramSliders.size(); ++i)
  66. {
  67. Rectangle<int> paramBounds = r.removeFromTop (kParamSliderHeight);
  68. Rectangle<int> labelBounds = paramBounds.removeFromLeft (kParamLabelWidth);
  69. paramLabels[i]->setBounds (labelBounds);
  70. paramSliders[i]->setBounds (paramBounds);
  71. }
  72. }
  73. void paint (Graphics& g) override
  74. {
  75. g.fillAll (Colours::white);
  76. }
  77. //==============================================================================
  78. void sliderValueChanged (Slider* slider) override
  79. {
  80. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  81. {
  82. if (slider->isMouseButtonDown())
  83. param->setValueNotifyingHost ((float) slider->getValue());
  84. else
  85. param->setValue ((float) slider->getValue());
  86. }
  87. }
  88. void sliderDragStarted (Slider* slider) override
  89. {
  90. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  91. param->beginChangeGesture();
  92. }
  93. void sliderDragEnded (Slider* slider) override
  94. {
  95. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  96. param->endChangeGesture();
  97. }
  98. private:
  99. void timerCallback() override
  100. {
  101. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  102. for (int i = 0; i < params.size(); ++i)
  103. {
  104. if (const AudioProcessorParameter* param = params[i])
  105. {
  106. if (i < paramSliders.size())
  107. paramSliders[i]->setValue (param->getValue());
  108. }
  109. }
  110. }
  111. AudioProcessorParameter* getParameterForSlider (Slider* slider)
  112. {
  113. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  114. return params[paramSliders.indexOf (slider)];
  115. }
  116. Label noParameterLabel;
  117. OwnedArray<Slider> paramSliders;
  118. OwnedArray<Label> paramLabels;
  119. };