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.

255 lines
8.5KB

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