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.

125 lines
4.2KB

  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. void sliderValueChanged (Slider* slider) override
  78. {
  79. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  80. int paramIndex = paramSliders.indexOf (slider);
  81. if (paramIndex >= 0 && paramIndex < params.size())
  82. params[paramIndex]->setValueNotifyingHost ((float) slider->getValue());
  83. }
  84. private:
  85. void timerCallback() override
  86. {
  87. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  88. for (int i = 0; i < params.size(); ++i)
  89. {
  90. if (const AudioProcessorParameter* param = params[i])
  91. {
  92. if (i < paramSliders.size())
  93. paramSliders[i]->setValue (param->getValue());
  94. }
  95. }
  96. }
  97. Label noParameterLabel;
  98. OwnedArray<Slider> paramSliders;
  99. OwnedArray<Label> paramLabels;
  100. };