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.0KB

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