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.

254 lines
8.3KB

  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. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. /** Simple message that holds a Colour. */
  20. struct ColourMessage : public Message
  21. {
  22. ColourMessage (Colour col) : colour (col)
  23. {
  24. }
  25. /** Returns the colour of a ColourMessage of white if the message is not a ColourMessage. */
  26. static Colour getColour (const Message& message)
  27. {
  28. if (const ColourMessage* cm = dynamic_cast<const ColourMessage*> (&message))
  29. return cm->colour;
  30. return Colours::white;
  31. }
  32. private:
  33. Colour colour;
  34. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourMessage)
  35. };
  36. //==============================================================================
  37. /** Simple component that can be triggered to flash.
  38. The flash will then fade using a Timer to repaint itself and will send a change
  39. message once it is finished.
  40. */
  41. class FlashingComponent : public Component,
  42. public MessageListener,
  43. public ChangeBroadcaster,
  44. private Timer
  45. {
  46. public:
  47. FlashingComponent()
  48. : flashAlpha (0.0f),
  49. colour (Colours::red)
  50. {
  51. }
  52. void startFlashing()
  53. {
  54. flashAlpha = 1.0f;
  55. startTimerHz (25);
  56. }
  57. /** Stops this component flashing without sending a change message. */
  58. void stopFlashing()
  59. {
  60. flashAlpha = 0.0f;
  61. stopTimer();
  62. repaint();
  63. }
  64. /** Sets the colour of the component. */
  65. void setFlashColour (const Colour newColour)
  66. {
  67. colour = newColour;
  68. repaint();
  69. }
  70. /** Draws our component. */
  71. void paint (Graphics& g) override
  72. {
  73. g.setColour (colour.overlaidWith (Colours::white.withAlpha (flashAlpha)));
  74. g.fillEllipse (getLocalBounds().toFloat());
  75. }
  76. /** Custom mouse handler to trigger a flash. */
  77. void mouseDown (const MouseEvent&) override
  78. {
  79. startFlashing();
  80. }
  81. /** Message listener callback used to change our colour */
  82. void handleMessage (const Message& message) override
  83. {
  84. setFlashColour (ColourMessage::getColour (message));
  85. }
  86. private:
  87. float flashAlpha;
  88. Colour colour;
  89. void timerCallback() override
  90. {
  91. // Reduce the alpha level of the flash slightly so it fades out
  92. flashAlpha -= 0.075f;
  93. if (flashAlpha < 0.05f)
  94. {
  95. stopFlashing();
  96. sendChangeMessage();
  97. // Once we've finsihed flashing send a change message to trigger the next component to flash
  98. }
  99. repaint();
  100. }
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FlashingComponent)
  102. };
  103. //==============================================================================
  104. class TimersAndEventsDemo : public Component,
  105. private ChangeListener,
  106. private Button::Listener
  107. {
  108. public:
  109. TimersAndEventsDemo()
  110. {
  111. setOpaque (true);
  112. // Create and add our FlashingComponents with some random colours and sizes
  113. for (int i = 0; i < numFlashingComponents; ++i)
  114. {
  115. FlashingComponent* newFlasher = new FlashingComponent();
  116. flashingComponents.add (newFlasher);
  117. newFlasher->setFlashColour (getRandomBrightColour());
  118. newFlasher->addChangeListener (this);
  119. const int diameter = 25 + random.nextInt (75);
  120. newFlasher->setSize (diameter, diameter);
  121. addAndMakeVisible (newFlasher);
  122. }
  123. addAndMakeVisible (stopButton);
  124. stopButton.addListener (this);
  125. stopButton.setButtonText ("Stop");
  126. addAndMakeVisible (randomColourButton);
  127. randomColourButton.addListener (this);
  128. randomColourButton.setButtonText ("Set Random Colour");
  129. // lay out our components in a psudo random grid
  130. Rectangle<int> area (0, 100, 150, 150);
  131. for (int i = 0; i < flashingComponents.size(); ++i)
  132. {
  133. FlashingComponent* comp = flashingComponents.getUnchecked (i);
  134. Rectangle<int> buttonArea (area.withSize (comp->getWidth(), comp->getHeight()));
  135. buttonArea.translate (random.nextInt (area.getWidth() - comp->getWidth()),
  136. random.nextInt (area.getHeight() - comp->getHeight()));
  137. comp->setBounds (buttonArea);
  138. area.translate (area.getWidth(), 0);
  139. // if we go off the right start a new row
  140. if (area.getRight() > (800 - area.getWidth()))
  141. {
  142. area.translate (0, area.getWidth());
  143. area.setX (0);
  144. }
  145. }
  146. }
  147. ~TimersAndEventsDemo()
  148. {
  149. stopButton.removeListener (this);
  150. randomColourButton.removeListener (this);
  151. for (int i = flashingComponents.size(); --i >= 0;)
  152. flashingComponents.getUnchecked (i)->removeChangeListener (this);
  153. }
  154. void paint (Graphics& g) override
  155. {
  156. g.fillAll (Colours::darkgrey);
  157. }
  158. void paintOverChildren (Graphics& g) override
  159. {
  160. const Rectangle<int> explanationArea (getLocalBounds().removeFromTop (100));
  161. AttributedString s;
  162. s.append ("Click on a circle to make it flash. When it has finished flashing it will send a message which causes the next circle to flash");
  163. s.append (newLine);
  164. s.append ("Click the \"Set Random Colour\" button to change the colour of one of the circles.");
  165. s.append (newLine);
  166. s.setFont (Font (16.0f));
  167. s.setColour (Colours::lightgrey);
  168. s.draw (g, explanationArea.reduced (10).toFloat());
  169. }
  170. void resized() override
  171. {
  172. Rectangle<int> area (getLocalBounds().removeFromBottom (40));
  173. randomColourButton.setBounds (area.removeFromLeft (166).reduced (8));
  174. stopButton.setBounds (area.removeFromRight (166).reduced (8));
  175. }
  176. private:
  177. enum { numFlashingComponents = 9 };
  178. OwnedArray<FlashingComponent> flashingComponents;
  179. TextButton randomColourButton, stopButton;
  180. Random random;
  181. void changeListenerCallback (ChangeBroadcaster* source) override
  182. {
  183. for (int i = 0; i < flashingComponents.size(); ++i)
  184. if (source == flashingComponents.getUnchecked (i))
  185. flashingComponents.getUnchecked ((i + 1) % flashingComponents.size())->startFlashing();
  186. }
  187. void buttonClicked (Button* button) override
  188. {
  189. if (button == &randomColourButton)
  190. {
  191. // Here we post a new ColourMessage with a random colour to a random flashing component.
  192. // This will send a message to the component asynchronously and trigger its handleMessage callback
  193. flashingComponents.getUnchecked (random.nextInt (flashingComponents.size()))->postMessage (new ColourMessage (getRandomBrightColour()));
  194. }
  195. else if (button == &stopButton)
  196. {
  197. for (int i = 0; i < flashingComponents.size(); ++i)
  198. flashingComponents.getUnchecked (i)->stopFlashing();
  199. }
  200. }
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimersAndEventsDemo)
  202. };
  203. // This static object will register this demo type in a global list of demos..
  204. static JuceDemoType<TimersAndEventsDemo> demo ("40 Timers & Events");