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.

156 lines
7.7KB

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