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.

152 lines
5.1KB

  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. class GenericEditor : public AudioProcessorEditor,
  20. public SliderListener,
  21. private Timer
  22. {
  23. public:
  24. enum
  25. {
  26. kParamSliderHeight = 40,
  27. kParamLabelWidth = 80,
  28. kParamSliderWidth = 300
  29. };
  30. GenericEditor (AudioProcessor& parent)
  31. : AudioProcessorEditor (parent),
  32. noParameterLabel ("noparam", "No parameters available")
  33. {
  34. const OwnedArray<AudioProcessorParameter>& params = parent.getParameters();
  35. for (int i = 0; i < params.size(); ++i)
  36. {
  37. if (const AudioParameterFloat* param = dynamic_cast<AudioParameterFloat*>(params[i]))
  38. {
  39. const bool isLevelMeter = (((param->category & 0xffff0000) >> 16) == 2);
  40. if (isLevelMeter)
  41. continue;
  42. Slider* aSlider;
  43. paramSliders.add (aSlider = new Slider (param->name));
  44. aSlider->setRange (param->range.start, param->range.end);
  45. aSlider->setSliderStyle (Slider::LinearHorizontal);
  46. aSlider->setValue (dynamic_cast<const AudioProcessorParameter*>(param)->getValue());
  47. aSlider->addListener (this);
  48. addAndMakeVisible (aSlider);
  49. Label* aLabel;
  50. paramLabels.add (aLabel = new Label (param->name, param->name));
  51. addAndMakeVisible (aLabel);
  52. }
  53. }
  54. noParameterLabel.setJustificationType (Justification::horizontallyCentred | Justification::verticallyCentred);
  55. noParameterLabel.setFont (noParameterLabel.getFont().withStyle (Font::italic));
  56. setSize (kParamSliderWidth + kParamLabelWidth,
  57. jmax (1, kParamSliderHeight * paramSliders.size()));
  58. if (paramSliders.size() == 0)
  59. addAndMakeVisible (noParameterLabel);
  60. else
  61. startTimer (100);
  62. }
  63. ~GenericEditor()
  64. {
  65. }
  66. void resized() override
  67. {
  68. Rectangle<int> r = getLocalBounds();
  69. noParameterLabel.setBounds (r);
  70. for (int i = 0; i < paramSliders.size(); ++i)
  71. {
  72. Rectangle<int> paramBounds = r.removeFromTop (kParamSliderHeight);
  73. Rectangle<int> labelBounds = paramBounds.removeFromLeft (kParamLabelWidth);
  74. paramLabels[i]->setBounds (labelBounds);
  75. paramSliders[i]->setBounds (paramBounds);
  76. }
  77. }
  78. void paint (Graphics& g) override
  79. {
  80. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  81. }
  82. //==============================================================================
  83. void sliderValueChanged (Slider* slider) override
  84. {
  85. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  86. {
  87. if (slider->isMouseButtonDown())
  88. param->setValueNotifyingHost ((float) slider->getValue());
  89. else
  90. param->setValue ((float) slider->getValue());
  91. }
  92. }
  93. void sliderDragStarted (Slider* slider) override
  94. {
  95. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  96. param->beginChangeGesture();
  97. }
  98. void sliderDragEnded (Slider* slider) override
  99. {
  100. if (AudioProcessorParameter* param = getParameterForSlider (slider))
  101. param->endChangeGesture();
  102. }
  103. private:
  104. void timerCallback() override
  105. {
  106. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  107. for (int i = 0; i < params.size(); ++i)
  108. {
  109. if (const AudioProcessorParameter* param = params[i])
  110. {
  111. if (i < paramSliders.size())
  112. paramSliders[i]->setValue (param->getValue());
  113. }
  114. }
  115. }
  116. AudioProcessorParameter* getParameterForSlider (Slider* slider)
  117. {
  118. const OwnedArray<AudioProcessorParameter>& params = getAudioProcessor()->getParameters();
  119. return params[paramSliders.indexOf (slider)];
  120. }
  121. Label noParameterLabel;
  122. OwnedArray<Slider> paramSliders;
  123. OwnedArray<Label> paramLabels;
  124. };