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.

262 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: TimersAndEventsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Application using timers and events.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: TimersAndEventsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. /** Simple message that holds a Colour. */
  37. struct ColourMessage final : public Message
  38. {
  39. ColourMessage (Colour col) : colour (col) {}
  40. /** Returns the colour of a ColourMessage of white if the message is not a ColourMessage. */
  41. static Colour getColour (const Message& message)
  42. {
  43. if (auto* cm = dynamic_cast<const ColourMessage*> (&message))
  44. return cm->colour;
  45. return Colours::white;
  46. }
  47. Colour colour;
  48. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourMessage)
  49. };
  50. //==============================================================================
  51. /** Simple component that can be triggered to flash.
  52. The flash will then fade using a Timer to repaint itself and will send a change
  53. message once it is finished.
  54. */
  55. class FlashingComponent final : public Component,
  56. public MessageListener,
  57. public ChangeBroadcaster,
  58. private Timer
  59. {
  60. public:
  61. FlashingComponent() {}
  62. void startFlashing()
  63. {
  64. flashAlpha = 1.0f;
  65. startTimerHz (25);
  66. }
  67. /** Stops this component flashing without sending a change message. */
  68. void stopFlashing()
  69. {
  70. flashAlpha = 0.0f;
  71. stopTimer();
  72. repaint();
  73. }
  74. /** Sets the colour of the component. */
  75. void setFlashColour (const Colour newColour)
  76. {
  77. colour = newColour;
  78. repaint();
  79. }
  80. /** Draws our component. */
  81. void paint (Graphics& g) override
  82. {
  83. g.setColour (colour.overlaidWith (Colours::white.withAlpha (flashAlpha)));
  84. g.fillEllipse (getLocalBounds().toFloat());
  85. }
  86. /** Custom mouse handler to trigger a flash. */
  87. void mouseDown (const MouseEvent&) override
  88. {
  89. startFlashing();
  90. }
  91. /** Message listener callback used to change our colour */
  92. void handleMessage (const Message& message) override
  93. {
  94. setFlashColour (ColourMessage::getColour (message));
  95. }
  96. private:
  97. float flashAlpha = 0.0f;
  98. Colour colour { Colours::red };
  99. void timerCallback() override
  100. {
  101. // Reduce the alpha level of the flash slightly so it fades out
  102. flashAlpha -= 0.075f;
  103. if (flashAlpha < 0.05f)
  104. {
  105. stopFlashing();
  106. sendChangeMessage();
  107. // Once we've finished flashing send a change message to trigger the next component to flash
  108. }
  109. repaint();
  110. }
  111. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FlashingComponent)
  112. };
  113. //==============================================================================
  114. class TimersAndEventsDemo final : public Component,
  115. private ChangeListener
  116. {
  117. public:
  118. TimersAndEventsDemo()
  119. {
  120. setOpaque (true);
  121. // Create and add our FlashingComponents with some random colours and sizes
  122. for (int i = 0; i < numFlashingComponents; ++i)
  123. {
  124. auto* newFlasher = new FlashingComponent();
  125. flashingComponents.add (newFlasher);
  126. newFlasher->setFlashColour (getRandomBrightColour());
  127. newFlasher->addChangeListener (this);
  128. auto diameter = 25 + random.nextInt (75);
  129. newFlasher->setSize (diameter, diameter);
  130. addAndMakeVisible (newFlasher);
  131. }
  132. addAndMakeVisible (stopButton);
  133. stopButton.onClick = [this] { stopButtonClicked(); };
  134. addAndMakeVisible (randomColourButton);
  135. randomColourButton.onClick = [this] { randomColourButtonClicked(); };
  136. // lay out our components in a pseudo random grid
  137. Rectangle<int> area (0, 100, 150, 150);
  138. for (auto* comp : flashingComponents)
  139. {
  140. auto buttonArea = area.withSize (comp->getWidth(), comp->getHeight());
  141. buttonArea.translate (random.nextInt (area.getWidth() - comp->getWidth()),
  142. random.nextInt (area.getHeight() - comp->getHeight()));
  143. comp->setBounds (buttonArea);
  144. area.translate (area.getWidth(), 0);
  145. // if we go off the right start a new row
  146. if (area.getRight() > (800 - area.getWidth()))
  147. {
  148. area.translate (0, area.getWidth());
  149. area.setX (0);
  150. }
  151. }
  152. setSize (600, 600);
  153. }
  154. ~TimersAndEventsDemo() override
  155. {
  156. for (auto* fc : flashingComponents)
  157. fc->removeChangeListener (this);
  158. }
  159. void paint (Graphics& g) override
  160. {
  161. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  162. Colours::darkgrey));
  163. }
  164. void paintOverChildren (Graphics& g) override
  165. {
  166. auto explanationArea = getLocalBounds().removeFromTop (100);
  167. AttributedString s;
  168. 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");
  169. s.append (newLine);
  170. s.append ("Click the \"Set Random Colour\" button to change the colour of one of the circles.");
  171. s.append (newLine);
  172. s.setFont (16.0f);
  173. s.setColour (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText, Colours::lightgrey));
  174. s.draw (g, explanationArea.reduced (10).toFloat());
  175. }
  176. void resized() override
  177. {
  178. auto area = getLocalBounds().removeFromBottom (40);
  179. randomColourButton.setBounds (area.removeFromLeft (166) .reduced (8));
  180. stopButton .setBounds (area.removeFromRight (166).reduced (8));
  181. }
  182. private:
  183. enum { numFlashingComponents = 9 };
  184. OwnedArray<FlashingComponent> flashingComponents;
  185. TextButton randomColourButton { "Set Random Colour" },
  186. stopButton { "Stop" };
  187. Random random;
  188. void changeListenerCallback (ChangeBroadcaster* source) override
  189. {
  190. for (int i = 0; i < flashingComponents.size(); ++i)
  191. if (source == flashingComponents.getUnchecked (i))
  192. flashingComponents.getUnchecked ((i + 1) % flashingComponents.size())->startFlashing();
  193. }
  194. void randomColourButtonClicked()
  195. {
  196. // Here we post a new ColourMessage with a random colour to a random flashing component.
  197. // This will send a message to the component asynchronously and trigger its handleMessage callback
  198. flashingComponents.getUnchecked (random.nextInt (flashingComponents.size()))->postMessage (new ColourMessage (getRandomBrightColour()));
  199. }
  200. void stopButtonClicked()
  201. {
  202. for (auto* fc : flashingComponents)
  203. fc->stopFlashing();
  204. }
  205. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimersAndEventsDemo)
  206. };