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
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. //==============================================================================
  20. /** This will be the source of our balls and can be dragged around. */
  21. class BallGeneratorComponent : public Component
  22. {
  23. public:
  24. BallGeneratorComponent()
  25. {
  26. }
  27. void paint (Graphics& g) override
  28. {
  29. Rectangle<float> area (getLocalBounds().toFloat().reduced (2.0f));
  30. g.setColour (Colours::orange);
  31. g.drawRoundedRectangle (area, 10.0f, 2.0f);
  32. AttributedString s;
  33. s.setJustification (Justification::centred);
  34. s.setWordWrap (AttributedString::none);
  35. s.append ("Drag Me!");
  36. s.setColour (Colours::white);
  37. s.draw (g, area);
  38. }
  39. void resized() override
  40. {
  41. // Just set the limits of our constrainer so that we don't drag ourselves off the screen
  42. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  43. }
  44. void mouseDown (const MouseEvent& e) override
  45. {
  46. // Prepares our dragger to drag this Component
  47. dragger.startDraggingComponent (this, e);
  48. }
  49. void mouseDrag (const MouseEvent& e) override
  50. {
  51. // Moves this Component according to the mouse drag event and applies our constraints to it
  52. dragger.dragComponent (this, e, &constrainer);
  53. }
  54. private:
  55. ComponentBoundsConstrainer constrainer;
  56. ComponentDragger dragger;
  57. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallGeneratorComponent)
  58. };
  59. //==============================================================================
  60. struct BallComponent : public Component
  61. {
  62. BallComponent (const Point<float>& pos)
  63. : position (pos),
  64. speed (Random::getSystemRandom().nextFloat() * 4.0f - 2.0f,
  65. Random::getSystemRandom().nextFloat() * -6.0f - 2.0f),
  66. colour (Colours::white)
  67. {
  68. setSize (20, 20);
  69. step();
  70. }
  71. bool step()
  72. {
  73. position += speed;
  74. speed.y += 0.1f;
  75. setCentrePosition ((int) position.x,
  76. (int) position.y);
  77. if (Component* parent = getParentComponent())
  78. return isPositiveAndBelow (position.x, (float) parent->getWidth())
  79. && position.y < (float) parent->getHeight();
  80. return position.y < 400.0f && position.x >= -10.0f;
  81. }
  82. void paint (Graphics& g)
  83. {
  84. g.setColour (colour);
  85. g.fillEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f);
  86. g.setColour (Colours::darkgrey);
  87. g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
  88. }
  89. Point<float> position, speed;
  90. Colour colour;
  91. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallComponent)
  92. };
  93. //==============================================================================
  94. class AnimationDemo : public Component,
  95. private Button::Listener,
  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. Button* b = createButton();
  106. componentsToAnimate.add (b);
  107. addAndMakeVisible (b);
  108. b->addListener (this);
  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 * 2.0f * float_Pi / 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. fillTiledBackground (g);
  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. ImageButton* b = new ImageButton ("ImageButton");
  192. b->setImages (true, true, true,
  193. juceIcon, 1.0f, Colours::transparentBlack,
  194. juceIcon, 1.0f, Colours::white,
  195. juceIcon, 1.0f, Colours::white,
  196. 0.5f);
  197. return b;
  198. }
  199. void buttonClicked (Button*) override
  200. {
  201. for (int i = 0; i < componentsToAnimate.size(); ++i)
  202. {
  203. const int newIndex = (i + 3 * cycleCount) % componentsToAnimate.size();
  204. const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
  205. const float radius = getWidth() * 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 (componentsToAnimate.getUnchecked(i),
  210. r.reduced (10),
  211. 1.0f,
  212. 900 + 300 * std::sin (angle),
  213. false,
  214. 0.0,
  215. 0.0);
  216. }
  217. ++cycleCount;
  218. }
  219. void timerCallback() override
  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. {
  228. BallComponent* ball = new BallComponent (ballGenerator.getBounds().getCentre().toFloat());
  229. addAndMakeVisible (ball);
  230. balls.add (ball);
  231. }
  232. }
  233. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationDemo)
  234. };
  235. // This static object will register this demo type in a global list of demos..
  236. static JuceDemoType<AnimationDemo> demo ("10 Components: Animation");