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.

189 lines
6.4KB

  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 (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  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. const auto activeColour = getLookAndFeel().findColour (Slider::thumbColourId);
  102. const auto inactiveColour = getLookAndFeel().findColour (Slider::trackColourId);
  103. for (int i = 0; i < activeChannels.size(); ++i)
  104. {
  105. bool isActive = listener->isChannelActive (i);
  106. activeChannels.getReference (i) = isActive;
  107. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? activeColour : inactiveColour);
  108. channelButtons [i]->repaint();
  109. }
  110. }
  111. }
  112. }
  113. private:
  114. String getLayoutName() const
  115. {
  116. if (AudioProcessor* p = getAudioProcessor())
  117. return p->getChannelLayoutOfBus (false, 0).getDescription();
  118. return "Unknown";
  119. }
  120. void timerCallback() override
  121. {
  122. if (getAudioProcessor()->isSuspended() != lastSuspended)
  123. {
  124. lastSuspended = getAudioProcessor()->isSuspended();
  125. updateGUI();
  126. }
  127. if (! lastSuspended)
  128. {
  129. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  130. {
  131. const auto activeColour = getLookAndFeel().findColour (Slider::thumbColourId);
  132. const auto inactiveColour = getLookAndFeel().findColour (Slider::trackColourId);
  133. for (int i = 0; i < activeChannels.size(); ++i)
  134. {
  135. bool isActive = listener->isChannelActive (i);
  136. if (activeChannels.getReference (i) != isActive)
  137. {
  138. activeChannels.getReference (i) = isActive;
  139. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? activeColour : inactiveColour);
  140. channelButtons [i]->repaint();
  141. }
  142. }
  143. }
  144. }
  145. }
  146. AudioChannelSet currentChannelLayout;
  147. Label noChannelsLabel, layoutTitle;
  148. OwnedArray<TextButton> channelButtons;
  149. Array<bool> activeChannels;
  150. bool lastSuspended;
  151. };