Audio plugin host https://kx.studio/carla
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.

juce_ComponentAnimator.h 8.1KB

9 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_COMPONENTANIMATOR_H_INCLUDED
  18. #define JUCE_COMPONENTANIMATOR_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Animates a set of components, moving them to a new position and/or fading their
  22. alpha levels.
  23. To animate a component, create a ComponentAnimator instance or (preferably) use the
  24. global animator object provided by Desktop::getAnimator(), and call its animateComponent()
  25. method to commence the movement.
  26. If you're using your own ComponentAnimator instance, you'll need to make sure it isn't
  27. deleted before it finishes moving the components, or they'll be abandoned before reaching their
  28. destinations.
  29. It's ok to delete components while they're being animated - the animator will detect this
  30. and safely stop using them.
  31. The class is a ChangeBroadcaster and sends a notification when any components
  32. start or finish being animated.
  33. @see Desktop::getAnimator
  34. */
  35. class JUCE_API ComponentAnimator : public ChangeBroadcaster,
  36. private Timer
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates a ComponentAnimator. */
  41. ComponentAnimator();
  42. /** Destructor. */
  43. ~ComponentAnimator();
  44. //==============================================================================
  45. /** Starts a component moving from its current position to a specified position.
  46. If the component is already in the middle of an animation, that will be abandoned,
  47. and a new animation will begin, moving the component from its current location.
  48. The start and end speed parameters let you apply some acceleration to the component's
  49. movement.
  50. @param component the component to move
  51. @param finalBounds the destination bounds to which the component should move. To leave the
  52. component in the same place, just pass component->getBounds() for this value
  53. @param finalAlpha the alpha value that the component should have at the end of the animation
  54. @param animationDurationMilliseconds how long the animation should last, in milliseconds
  55. @param useProxyComponent if true, this means the component should be replaced by an internally
  56. managed temporary component which is a snapshot of the original component.
  57. This avoids the component having to paint itself as it moves, so may
  58. be more efficient. This option also allows you to delete the original
  59. component immediately after starting the animation, because the animation
  60. can proceed without it. If you use a proxy, the original component will be
  61. made invisible by this call, and then will become visible again at the end
  62. of the animation. It'll also mean that the proxy component will be temporarily
  63. added to the component's parent, so avoid it if this might confuse the parent
  64. component, or if there's a chance the parent might decide to delete its children.
  65. @param startSpeed a value to indicate the relative start speed of the animation. If this is 0,
  66. the component will start by accelerating from rest; higher values mean that it
  67. will have an initial speed greater than zero. If the value if greater than 1, it
  68. will decelerate towards the middle of its journey. To move the component at a
  69. constant rate for its entire animation, set both the start and end speeds to 1.0
  70. @param endSpeed a relative speed at which the component should be moving when the animation finishes.
  71. If this is 0, the component will decelerate to a standstill at its final position;
  72. higher values mean the component will still be moving when it stops. To move the component
  73. at a constant rate for its entire animation, set both the start and end speeds to 1.0
  74. */
  75. void animateComponent (Component* component,
  76. const Rectangle<int>& finalBounds,
  77. float finalAlpha,
  78. int animationDurationMilliseconds,
  79. bool useProxyComponent,
  80. double startSpeed,
  81. double endSpeed);
  82. /** Begins a fade-out of this components alpha level.
  83. This is a quick way of invoking animateComponent() with a target alpha value of 0.0f, using
  84. a proxy. You're safe to delete the component after calling this method, and this won't
  85. interfere with the animation's progress.
  86. */
  87. void fadeOut (Component* component, int millisecondsToTake);
  88. /** Begins a fade-in of a component.
  89. This is a quick way of invoking animateComponent() with a target alpha value of 1.0f.
  90. */
  91. void fadeIn (Component* component, int millisecondsToTake);
  92. /** Stops a component if it's currently being animated.
  93. If moveComponentToItsFinalPosition is true, then the component will
  94. be immediately moved to its destination position and size. If false, it will be
  95. left in whatever location it currently occupies.
  96. */
  97. void cancelAnimation (Component* component,
  98. bool moveComponentToItsFinalPosition);
  99. /** Clears all of the active animations.
  100. If moveComponentsToTheirFinalPositions is true, all the components will
  101. be immediately set to their final positions. If false, they will be
  102. left in whatever locations they currently occupy.
  103. */
  104. void cancelAllAnimations (bool moveComponentsToTheirFinalPositions);
  105. /** Returns the destination position for a component.
  106. If the component is being animated, this will return the target position that
  107. was specified when animateComponent() was called.
  108. If the specified component isn't currently being animated, this method will just
  109. return its current position.
  110. */
  111. Rectangle<int> getComponentDestination (Component* component);
  112. /** Returns true if the specified component is currently being animated. */
  113. bool isAnimating (Component* component) const noexcept;
  114. /** Returns true if any components are currently being animated. */
  115. bool isAnimating() const noexcept;
  116. private:
  117. //==============================================================================
  118. class AnimationTask;
  119. OwnedArray<AnimationTask> tasks;
  120. uint32 lastTime;
  121. AnimationTask* findTaskFor (Component*) const noexcept;
  122. void timerCallback() override;
  123. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentAnimator)
  124. };
  125. #endif // JUCE_COMPONENTANIMATOR_H_INCLUDED