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.

258 lines
8.5KB

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