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
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 Button::Listener,
  97. private Timer
  98. {
  99. public:
  100. AnimationDemo()
  101. {
  102. setOpaque (true);
  103. setSize (620, 620);
  104. for (int i = 11; --i >= 0;)
  105. {
  106. Button* b = createButton();
  107. componentsToAnimate.add (b);
  108. addAndMakeVisible (b);
  109. b->addListener (this);
  110. }
  111. addAndMakeVisible (ballGenerator);
  112. ballGenerator.centreWithSize (80, 50);
  113. cycleCount = 2;
  114. for (int i = 0; i < componentsToAnimate.size(); ++i)
  115. componentsToAnimate.getUnchecked (i)->setBounds (getLocalBounds().reduced (250, 250));
  116. for (int i = 0; i < componentsToAnimate.size(); ++i)
  117. {
  118. const int newIndex = (i + 3) % componentsToAnimate.size();
  119. const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
  120. const float radius = getWidth() * 0.35f;
  121. Rectangle<int> r (getWidth() / 2 + (int) (radius * std::sin (angle)) - 50,
  122. getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
  123. 100, 100);
  124. animator.animateComponent (componentsToAnimate.getUnchecked(i),
  125. r.reduced (10),
  126. 1.0f,
  127. 500 + i * 100,
  128. false,
  129. 0.0,
  130. 0.0);
  131. }
  132. startTimerHz (60);
  133. }
  134. void paint (Graphics& g) override
  135. {
  136. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  137. }
  138. private:
  139. OwnedArray<Component> componentsToAnimate;
  140. OwnedArray<BallComponent> balls;
  141. BallGeneratorComponent ballGenerator;
  142. ComponentAnimator animator;
  143. int cycleCount;
  144. Button* createRandomButton()
  145. {
  146. DrawablePath normal, over;
  147. Path star1;
  148. star1.addStar (Point<float>(), 5, 20.0f, 50.0f, 0.2f);
  149. normal.setPath (star1);
  150. normal.setFill (Colours::red);
  151. Path star2;
  152. star2.addStar (Point<float>(), 7, 30.0f, 50.0f, 0.0f);
  153. over.setPath (star2);
  154. over.setFill (Colours::pink);
  155. over.setStrokeFill (Colours::black);
  156. over.setStrokeThickness (5.0f);
  157. Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
  158. BinaryData::juce_icon_pngSize);
  159. DrawableImage down;
  160. down.setImage (juceIcon);
  161. down.setOverlayColour (Colours::black.withAlpha (0.3f));
  162. if (Random::getSystemRandom().nextInt (10) > 2)
  163. {
  164. int type = Random::getSystemRandom().nextInt (3);
  165. DrawableButton* d = new DrawableButton ("Button",
  166. type == 0 ? DrawableButton::ImageOnButtonBackground
  167. : (type == 1 ? DrawableButton::ImageFitted
  168. : DrawableButton::ImageAboveTextLabel));
  169. d->setImages (&normal,
  170. Random::getSystemRandom().nextBool() ? &over : nullptr,
  171. Random::getSystemRandom().nextBool() ? &down : nullptr);
  172. if (Random::getSystemRandom().nextBool())
  173. {
  174. d->setColour (DrawableButton::backgroundColourId, getRandomBrightColour());
  175. d->setColour (DrawableButton::backgroundOnColourId, getRandomBrightColour());
  176. }
  177. d->setClickingTogglesState (Random::getSystemRandom().nextBool());
  178. return d;
  179. }
  180. ImageButton* b = new ImageButton ("ImageButton");
  181. b->setImages (true, true, true,
  182. juceIcon, 0.7f, Colours::transparentBlack,
  183. juceIcon, 1.0f, getRandomDarkColour().withAlpha (0.2f),
  184. juceIcon, 1.0f, getRandomBrightColour().withAlpha (0.8f),
  185. 0.5f);
  186. return b;
  187. }
  188. Button* createButton()
  189. {
  190. Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
  191. BinaryData::juce_icon_pngSize)
  192. .rescaled (128, 128);
  193. ImageButton* b = new ImageButton ("ImageButton");
  194. b->setImages (true, true, true,
  195. juceIcon, 1.0f, Colours::transparentBlack,
  196. juceIcon, 1.0f, Colours::white,
  197. juceIcon, 1.0f, Colours::white,
  198. 0.5f);
  199. return b;
  200. }
  201. void buttonClicked (Button*) override
  202. {
  203. for (int i = 0; i < componentsToAnimate.size(); ++i)
  204. {
  205. const int newIndex = (i + 3 * cycleCount) % componentsToAnimate.size();
  206. const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
  207. const float radius = getWidth() * 0.35f;
  208. Rectangle<int> r (getWidth() / 2 + (int) (radius * std::sin (angle)) - 50,
  209. getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
  210. 100, 100);
  211. animator.animateComponent (componentsToAnimate.getUnchecked(i),
  212. r.reduced (10),
  213. 1.0f,
  214. 900 + (int) (300 * std::sin (angle)),
  215. false,
  216. 0.0,
  217. 0.0);
  218. }
  219. ++cycleCount;
  220. }
  221. void timerCallback() override
  222. {
  223. // Go through each of our balls and update their position
  224. for (int i = balls.size(); --i >= 0;)
  225. if (! balls.getUnchecked(i)->step())
  226. balls.remove (i);
  227. // Randomly generate new balls
  228. if (Random::getSystemRandom().nextInt (100) < 4)
  229. {
  230. BallComponent* ball = new BallComponent (ballGenerator.getBounds().getCentre().toFloat());
  231. addAndMakeVisible (ball);
  232. balls.add (ball);
  233. }
  234. }
  235. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationDemo)
  236. };
  237. // This static object will register this demo type in a global list of demos..
  238. static JuceDemoType<AnimationDemo> demo ("10 Components: Animation");