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.

179 lines
6.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 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. private:
  72. String getLayoutName() const
  73. {
  74. if (AudioProcessor* processor = getAudioProcessor())
  75. return processor->busArrangement.outputBuses.getReference (0).channels.getDescription();
  76. return "Unknown";
  77. }
  78. void timerCallback() override
  79. {
  80. if (getAudioProcessor()->isSuspended() != lastSuspended)
  81. {
  82. lastSuspended = getAudioProcessor()->isSuspended();
  83. const AudioChannelSet& channelSet = getAudioProcessor()->busArrangement.outputBuses.getReference (0).channels;
  84. if (channelSet != currentChannelLayout)
  85. {
  86. currentChannelLayout = channelSet;
  87. layoutTitle.setText (currentChannelLayout.getDescription(), NotificationType::dontSendNotification);
  88. channelButtons.clear();
  89. activeChannels.resize (currentChannelLayout.size());
  90. if (currentChannelLayout == AudioChannelSet::disabled())
  91. {
  92. noChannelsLabel.setVisible (true);
  93. }
  94. else
  95. {
  96. const int numChannels = currentChannelLayout.size();
  97. for (int i = 0; i < numChannels; ++i)
  98. {
  99. const String channelName =
  100. AudioChannelSet::getAbbreviatedChannelTypeName (currentChannelLayout.getTypeOfChannel (i));
  101. TextButton* newButton;
  102. channelButtons.add (newButton = new TextButton (channelName, channelName));
  103. newButton->addListener (this);
  104. addAndMakeVisible (newButton);
  105. }
  106. noChannelsLabel.setVisible (false);
  107. resized();
  108. }
  109. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  110. {
  111. for (int i = 0; i < activeChannels.size(); ++i)
  112. {
  113. bool isActive = listener->isChannelActive (i);
  114. activeChannels.getReference (i) = isActive;
  115. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? Colours::lightsalmon : Colours::lightgrey);
  116. channelButtons [i]->repaint();
  117. }
  118. }
  119. }
  120. }
  121. if (! lastSuspended)
  122. {
  123. if (ChannelClickListener* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  124. {
  125. for (int i = 0; i < activeChannels.size(); ++i)
  126. {
  127. bool isActive = listener->isChannelActive (i);
  128. if (activeChannels.getReference (i) != isActive)
  129. {
  130. activeChannels.getReference (i) = isActive;
  131. channelButtons [i]->setColour (TextButton::buttonColourId, isActive ? Colours::lightsalmon : Colours::lightgrey);
  132. channelButtons [i]->repaint();
  133. }
  134. }
  135. }
  136. }
  137. }
  138. AudioChannelSet currentChannelLayout;
  139. Label noChannelsLabel, layoutTitle;
  140. OwnedArray<TextButton> channelButtons;
  141. Array<bool> activeChannels;
  142. bool lastSuspended;
  143. };