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.

301 lines
10KB

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