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.

300 lines
9.6KB

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