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.

163 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Animates a set of components, moving them to a new position and/or fading their
  23. alpha levels.
  24. To animate a component, create a ComponentAnimator instance or (preferably) use the
  25. global animator object provided by Desktop::getAnimator(), and call its animateComponent()
  26. method to commence the movement.
  27. If you're using your own ComponentAnimator instance, you'll need to make sure it isn't
  28. deleted before it finishes moving the components, or they'll be abandoned before reaching their
  29. destinations.
  30. It's ok to delete components while they're being animated - the animator will detect this
  31. and safely stop using them.
  32. The class is a ChangeBroadcaster and sends a notification when any components
  33. start or finish being animated.
  34. @see Desktop::getAnimator
  35. @tags{GUI}
  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() override;
  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 is 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