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.

302 lines
9.8KB

  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: AnimationDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays an animated draggable ball.
  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: AnimationDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. /** This will be the source of our balls and can be dragged around. */
  37. class BallGeneratorComponent final : public Component
  38. {
  39. public:
  40. BallGeneratorComponent() {}
  41. void paint (Graphics& g) override
  42. {
  43. auto area = getLocalBounds().reduced (2);
  44. g.setColour (Colours::orange);
  45. g.drawRoundedRectangle (area.toFloat(), 10.0f, 2.0f);
  46. g.setColour (findColour (TextButton::textColourOffId));
  47. g.drawFittedText ("Drag Me!", area, Justification::centred, 1);
  48. }
  49. void resized() override
  50. {
  51. // Just set the limits of our constrainer so that we don't drag ourselves off the screen
  52. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(),
  53. getHeight(), getWidth());
  54. }
  55. void mouseDown (const MouseEvent& e) override
  56. {
  57. // Prepares our dragger to drag this Component
  58. dragger.startDraggingComponent (this, e);
  59. }
  60. void mouseDrag (const MouseEvent& e) override
  61. {
  62. // Moves this Component according to the mouse drag event and applies our constraints to it
  63. dragger.dragComponent (this, e, &constrainer);
  64. }
  65. private:
  66. ComponentBoundsConstrainer constrainer;
  67. ComponentDragger dragger;
  68. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallGeneratorComponent)
  69. };
  70. //==============================================================================
  71. struct BallComponent final : public Component
  72. {
  73. BallComponent (Point<float> pos)
  74. : position (pos),
  75. speed (Random::getSystemRandom().nextFloat() * 4.0f - 2.0f,
  76. Random::getSystemRandom().nextFloat() * -6.0f - 2.0f),
  77. colour (Colours::white)
  78. {
  79. setSize (20, 20);
  80. step();
  81. }
  82. bool step()
  83. {
  84. position += speed;
  85. speed.y += 0.1f;
  86. setCentrePosition ((int) position.x,
  87. (int) position.y);
  88. if (auto* parent = getParentComponent())
  89. return isPositiveAndBelow (position.x, (float) parent->getWidth())
  90. && position.y < (float) parent->getHeight();
  91. return position.y < 400.0f && position.x >= -10.0f;
  92. }
  93. void paint (Graphics& g) override
  94. {
  95. g.setColour (colour);
  96. g.fillEllipse (2.0f, 2.0f, (float) getWidth() - 4.0f, (float) getHeight() - 4.0f);
  97. g.setColour (Colours::darkgrey);
  98. g.drawEllipse (2.0f, 2.0f, (float) getWidth() - 4.0f, (float) getHeight() - 4.0f, 1.0f);
  99. }
  100. Point<float> position, speed;
  101. Colour colour;
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallComponent)
  103. };
  104. //==============================================================================
  105. class AnimationDemo final : public Component,
  106. private Timer
  107. {
  108. public:
  109. AnimationDemo()
  110. {
  111. setOpaque (true);
  112. for (auto i = 0; i < 11; ++i)
  113. {
  114. auto* b = createButton();
  115. componentsToAnimate.add (b);
  116. addAndMakeVisible (b);
  117. b->onClick = [this] { triggerAnimation(); };
  118. }
  119. addAndMakeVisible (ballGenerator);
  120. cycleCount = 2;
  121. startTimerHz (60);
  122. setSize (620, 620);
  123. }
  124. void paint (Graphics& g) override
  125. {
  126. g.fillAll (findColour (ResizableWindow::backgroundColourId));
  127. }
  128. void resized() override
  129. {
  130. ballGenerator.centreWithSize (80, 50);
  131. triggerAnimation();
  132. }
  133. private:
  134. OwnedArray<Component> componentsToAnimate;
  135. OwnedArray<BallComponent> balls;
  136. BallGeneratorComponent ballGenerator;
  137. ComponentAnimator animator;
  138. int cycleCount;
  139. bool firstCallback = true;
  140. Button* createRandomButton()
  141. {
  142. DrawablePath normal, over;
  143. Path star1;
  144. star1.addStar ({}, 5, 20.0f, 50.0f, 0.2f);
  145. normal.setPath (star1);
  146. normal.setFill (Colours::red);
  147. Path star2;
  148. star2.addStar ({}, 7, 30.0f, 50.0f, 0.0f);
  149. over.setPath (star2);
  150. over.setFill (Colours::pink);
  151. over.setStrokeFill (Colours::black);
  152. over.setStrokeThickness (5.0f);
  153. auto juceIcon = getImageFromAssets ("juce_icon.png");
  154. DrawableImage down;
  155. down.setImage (juceIcon);
  156. down.setOverlayColour (Colours::black.withAlpha (0.3f));
  157. if (Random::getSystemRandom().nextInt (10) > 2)
  158. {
  159. auto type = Random::getSystemRandom().nextInt (3);
  160. auto* d = new DrawableButton ("Button",
  161. type == 0 ? DrawableButton::ImageOnButtonBackground
  162. : (type == 1 ? DrawableButton::ImageFitted
  163. : DrawableButton::ImageAboveTextLabel));
  164. d->setImages (&normal,
  165. Random::getSystemRandom().nextBool() ? &over : nullptr,
  166. Random::getSystemRandom().nextBool() ? &down : nullptr);
  167. if (Random::getSystemRandom().nextBool())
  168. {
  169. d->setColour (DrawableButton::backgroundColourId, getRandomBrightColour());
  170. d->setColour (DrawableButton::backgroundOnColourId, getRandomBrightColour());
  171. }
  172. d->setClickingTogglesState (Random::getSystemRandom().nextBool());
  173. return d;
  174. }
  175. auto* b = new ImageButton ("ImageButton");
  176. b->setImages (true, true, true,
  177. juceIcon, 0.7f, Colours::transparentBlack,
  178. juceIcon, 1.0f, getRandomDarkColour() .withAlpha (0.2f),
  179. juceIcon, 1.0f, getRandomBrightColour().withAlpha (0.8f),
  180. 0.5f);
  181. return b;
  182. }
  183. Button* createButton()
  184. {
  185. auto juceIcon = getImageFromAssets ("juce_icon.png").rescaled (128, 128);
  186. auto* b = new ImageButton ("ImageButton");
  187. b->setImages (true, true, true,
  188. juceIcon, 1.0f, Colours::transparentBlack,
  189. juceIcon, 1.0f, Colours::white,
  190. juceIcon, 1.0f, Colours::white,
  191. 0.5f);
  192. return b;
  193. }
  194. void triggerAnimation()
  195. {
  196. auto width = getWidth();
  197. auto height = getHeight();
  198. bool useWidth = (height > width);
  199. for (auto* component : componentsToAnimate)
  200. {
  201. auto newIndex = (componentsToAnimate.indexOf (component) + 3 * cycleCount)
  202. % componentsToAnimate.size();
  203. auto angle = (float) newIndex * MathConstants<float>::twoPi / (float) componentsToAnimate.size();
  204. auto radius = useWidth ? (float) width * 0.35f
  205. : (float) height * 0.35f;
  206. Rectangle<int> r (getWidth() / 2 + (int) (radius * std::sin (angle)) - 50,
  207. getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
  208. 100, 100);
  209. animator.animateComponent (component, r.reduced (10), 1.0f,
  210. 900 + (int) (300 * std::sin (angle)),
  211. false, 0.0, 0.0);
  212. }
  213. ++cycleCount;
  214. }
  215. void timerCallback() override
  216. {
  217. if (firstCallback)
  218. {
  219. triggerAnimation();
  220. firstCallback = false;
  221. }
  222. // Go through each of our balls and update their position
  223. for (int i = balls.size(); --i >= 0;)
  224. if (! balls.getUnchecked (i)->step())
  225. balls.remove (i);
  226. // Randomly generate new balls
  227. if (Random::getSystemRandom().nextInt (100) < 4)
  228. addAndMakeVisible (balls.add (new BallComponent (ballGenerator.getBounds().getCentre().toFloat())));
  229. }
  230. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationDemo)
  231. };