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.

183 lines
6.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 ChannelClickListener
  18. {
  19. public:
  20. virtual ~ChannelClickListener() {}
  21. virtual void channelButtonClicked (int channelIndex) = 0;
  22. virtual bool isChannelActive (int channelIndex) = 0;
  23. };
  24. class SurroundEditor : public AudioProcessorEditor,
  25. public ButtonListener,
  26. private Timer
  27. {
  28. public:
  29. SurroundEditor (AudioProcessor& parent)
  30. : AudioProcessorEditor (parent),
  31. currentChannelLayout (AudioChannelSet::disabled()),
  32. noChannelsLabel ("noChannelsLabel", "Input disabled"),
  33. layoutTitle ("LayoutTitleLabel", getLayoutName())
  34. {
  35. layoutTitle.setJustificationType (Justification::centred);
  36. addAndMakeVisible (layoutTitle);
  37. addAndMakeVisible (noChannelsLabel);
  38. setSize (640, 64);
  39. lastSuspended = ! getAudioProcessor()->isSuspended();
  40. timerCallback();
  41. startTimer (500);
  42. }
  43. ~SurroundEditor()
  44. {
  45. }
  46. void resized() override
  47. {
  48. Rectangle<int> r = getLocalBounds();
  49. layoutTitle.setBounds (r.removeFromBottom (16));
  50. noChannelsLabel.setBounds (r);
  51. if (channelButtons.size() > 0)
  52. {
  53. const int buttonWidth = r.getWidth() / channelButtons.size();
  54. for (int i = 0; i < channelButtons.size(); ++i)
  55. channelButtons[i]->setBounds (r.removeFromLeft (buttonWidth));
  56. }
  57. }
  58. void paint (Graphics& g) override
  59. {
  60. g.fillAll (Colours::white);
  61. }
  62. void buttonClicked (Button* btn) override
  63. {
  64. if (TextButton* textButton = dynamic_cast<TextButton*> (btn))
  65. {
  66. const int channelIndex = channelButtons.indexOf (textButton);
  67. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  68. listener->channelButtonClicked (channelIndex);
  69. }
  70. }
  71. void updateGUI()
  72. {
  73. const AudioChannelSet& channelSet = getAudioProcessor()->getChannelLayoutOfBus (false, 0);
  74. if (channelSet != currentChannelLayout)
  75. {
  76. currentChannelLayout = channelSet;
  77. layoutTitle.setText (currentChannelLayout.getDescription(), NotificationType::dontSendNotification);
  78. channelButtons.clear();
  79. activeChannels.resize (currentChannelLayout.size());
  80. if (currentChannelLayout == AudioChannelSet::disabled())
  81. {
  82. noChannelsLabel.setVisible (true);
  83. }
  84. else
  85. {
  86. const int numChannels = currentChannelLayout.size();
  87. for (int i = 0; i < numChannels; ++i)
  88. {
  89. const String channelName =
  90. AudioChannelSet::getAbbreviatedChannelTypeName (currentChannelLayout.getTypeOfChannel (i));
  91. TextButton* newButton;
  92. channelButtons.add (newButton = new TextButton (channelName, channelName));
  93. newButton->addListener (this);
  94. addAndMakeVisible (newButton);
  95. }
  96. noChannelsLabel.setVisible (false);
  97. resized();
  98. }
  99. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  100. {
  101. for (int i = 0; i < activeChannels.size(); ++i)
  102. {
  103. bool isActive = listener->isChannelActive (i);
  104. activeChannels.getReference (i) = isActive;
  105. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? Colours::lightsalmon : Colours::lightgrey);
  106. channelButtons [i]->repaint();
  107. }
  108. }
  109. }
  110. }
  111. private:
  112. String getLayoutName() const
  113. {
  114. if (AudioProcessor* p = getAudioProcessor())
  115. return p->getChannelLayoutOfBus (false, 0).getDescription();
  116. return "Unknown";
  117. }
  118. void timerCallback() override
  119. {
  120. if (getAudioProcessor()->isSuspended() != lastSuspended)
  121. {
  122. lastSuspended = getAudioProcessor()->isSuspended();
  123. updateGUI();
  124. }
  125. if (! lastSuspended)
  126. {
  127. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  128. {
  129. for (int i = 0; i < activeChannels.size(); ++i)
  130. {
  131. bool isActive = listener->isChannelActive (i);
  132. if (activeChannels.getReference (i) != isActive)
  133. {
  134. activeChannels.getReference (i) = isActive;
  135. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? Colours::lightsalmon : Colours::lightgrey);
  136. channelButtons [i]->repaint();
  137. }
  138. }
  139. }
  140. }
  141. }
  142. AudioChannelSet currentChannelLayout;
  143. Label noChannelsLabel, layoutTitle;
  144. OwnedArray<TextButton> channelButtons;
  145. Array<bool> activeChannels;
  146. bool lastSuspended;
  147. };