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.

334 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. class ComponentAnimator::AnimationTask
  18. {
  19. public:
  20. AnimationTask (Component* const comp) noexcept : component (comp)
  21. {
  22. }
  23. void reset (const Rectangle<int>& finalBounds,
  24. float finalAlpha,
  25. int millisecondsToSpendMoving,
  26. bool useProxyComponent,
  27. double startSpd, double endSpd)
  28. {
  29. msElapsed = 0;
  30. msTotal = jmax (1, millisecondsToSpendMoving);
  31. lastProgress = 0;
  32. destination = finalBounds;
  33. destAlpha = finalAlpha;
  34. isMoving = (finalBounds != component->getBounds());
  35. isChangingAlpha = (finalAlpha != component->getAlpha());
  36. left = component->getX();
  37. top = component->getY();
  38. right = component->getRight();
  39. bottom = component->getBottom();
  40. alpha = component->getAlpha();
  41. const double invTotalDistance = 4.0 / (startSpd + endSpd + 2.0);
  42. startSpeed = jmax (0.0, startSpd * invTotalDistance);
  43. midSpeed = invTotalDistance;
  44. endSpeed = jmax (0.0, endSpd * invTotalDistance);
  45. if (useProxyComponent)
  46. proxy = new ProxyComponent (*component);
  47. else
  48. proxy = nullptr;
  49. component->setVisible (! useProxyComponent);
  50. }
  51. bool useTimeslice (const int elapsed)
  52. {
  53. if (Component* const c = proxy != nullptr ? static_cast <Component*> (proxy)
  54. : static_cast <Component*> (component))
  55. {
  56. msElapsed += elapsed;
  57. double newProgress = msElapsed / (double) msTotal;
  58. if (newProgress >= 0 && newProgress < 1.0)
  59. {
  60. newProgress = timeToDistance (newProgress);
  61. const double delta = (newProgress - lastProgress) / (1.0 - lastProgress);
  62. jassert (newProgress >= lastProgress);
  63. lastProgress = newProgress;
  64. if (delta < 1.0)
  65. {
  66. bool stillBusy = false;
  67. if (isMoving)
  68. {
  69. left += (destination.getX() - left) * delta;
  70. top += (destination.getY() - top) * delta;
  71. right += (destination.getRight() - right) * delta;
  72. bottom += (destination.getBottom() - bottom) * delta;
  73. const Rectangle<int> newBounds (roundToInt (left),
  74. roundToInt (top),
  75. roundToInt (right - left),
  76. roundToInt (bottom - top));
  77. if (newBounds != destination)
  78. {
  79. c->setBounds (newBounds);
  80. stillBusy = true;
  81. }
  82. }
  83. if (isChangingAlpha)
  84. {
  85. alpha += (destAlpha - alpha) * delta;
  86. c->setAlpha ((float) alpha);
  87. stillBusy = true;
  88. }
  89. if (stillBusy)
  90. return true;
  91. }
  92. }
  93. }
  94. moveToFinalDestination();
  95. return false;
  96. }
  97. void moveToFinalDestination()
  98. {
  99. if (component != nullptr)
  100. {
  101. component->setAlpha ((float) destAlpha);
  102. component->setBounds (destination);
  103. if (proxy != nullptr)
  104. component->setVisible (destAlpha > 0);
  105. }
  106. }
  107. //==============================================================================
  108. class ProxyComponent : public Component
  109. {
  110. public:
  111. ProxyComponent (Component& c)
  112. {
  113. setWantsKeyboardFocus (false);
  114. setBounds (c.getBounds());
  115. setTransform (c.getTransform());
  116. setAlpha (c.getAlpha());
  117. setInterceptsMouseClicks (false, false);
  118. if (Component* const parent = c.getParentComponent())
  119. parent->addAndMakeVisible (this);
  120. else if (c.isOnDesktop() && c.getPeer() != nullptr)
  121. addToDesktop (c.getPeer()->getStyleFlags() | ComponentPeer::windowIgnoresKeyPresses);
  122. else
  123. jassertfalse; // seem to be trying to animate a component that's not visible..
  124. image = c.createComponentSnapshot (c.getLocalBounds(), false, getDesktopScaleFactor());
  125. setVisible (true);
  126. toBehind (&c);
  127. }
  128. void paint (Graphics& g) override
  129. {
  130. g.setOpacity (1.0f);
  131. g.drawImageTransformed (image, AffineTransform::scale (getWidth() / (float) image.getWidth(),
  132. getHeight() / (float) image.getHeight()), false);
  133. }
  134. private:
  135. Image image;
  136. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProxyComponent)
  137. };
  138. WeakReference<Component> component;
  139. ScopedPointer<Component> proxy;
  140. Rectangle<int> destination;
  141. double destAlpha;
  142. int msElapsed, msTotal;
  143. double startSpeed, midSpeed, endSpeed, lastProgress;
  144. double left, top, right, bottom, alpha;
  145. bool isMoving, isChangingAlpha;
  146. private:
  147. double timeToDistance (const double time) const noexcept
  148. {
  149. return (time < 0.5) ? time * (startSpeed + time * (midSpeed - startSpeed))
  150. : 0.5 * (startSpeed + 0.5 * (midSpeed - startSpeed))
  151. + (time - 0.5) * (midSpeed + (time - 0.5) * (endSpeed - midSpeed));
  152. }
  153. };
  154. //==============================================================================
  155. ComponentAnimator::ComponentAnimator()
  156. : lastTime (0)
  157. {
  158. }
  159. ComponentAnimator::~ComponentAnimator()
  160. {
  161. }
  162. //==============================================================================
  163. ComponentAnimator::AnimationTask* ComponentAnimator::findTaskFor (Component* const component) const noexcept
  164. {
  165. for (int i = tasks.size(); --i >= 0;)
  166. if (component == tasks.getUnchecked(i)->component.get())
  167. return tasks.getUnchecked(i);
  168. return nullptr;
  169. }
  170. void ComponentAnimator::animateComponent (Component* const component,
  171. const Rectangle<int>& finalBounds,
  172. const float finalAlpha,
  173. const int millisecondsToSpendMoving,
  174. const bool useProxyComponent,
  175. const double startSpeed,
  176. const double endSpeed)
  177. {
  178. // the speeds must be 0 or greater!
  179. jassert (startSpeed >= 0 && endSpeed >= 0);
  180. if (component != nullptr)
  181. {
  182. AnimationTask* at = findTaskFor (component);
  183. if (at == nullptr)
  184. {
  185. at = new AnimationTask (component);
  186. tasks.add (at);
  187. sendChangeMessage();
  188. }
  189. at->reset (finalBounds, finalAlpha, millisecondsToSpendMoving,
  190. useProxyComponent, startSpeed, endSpeed);
  191. if (! isTimerRunning())
  192. {
  193. lastTime = Time::getMillisecondCounter();
  194. startTimer (1000 / 50);
  195. }
  196. }
  197. }
  198. void ComponentAnimator::fadeOut (Component* component, int millisecondsToTake)
  199. {
  200. if (component != nullptr)
  201. {
  202. if (component->isShowing() && millisecondsToTake > 0)
  203. animateComponent (component, component->getBounds(), 0.0f, millisecondsToTake, true, 1.0, 1.0);
  204. component->setVisible (false);
  205. }
  206. }
  207. void ComponentAnimator::fadeIn (Component* component, int millisecondsToTake)
  208. {
  209. if (component != nullptr && ! (component->isVisible() && component->getAlpha() == 1.0f))
  210. {
  211. component->setAlpha (0.0f);
  212. component->setVisible (true);
  213. animateComponent (component, component->getBounds(), 1.0f, millisecondsToTake, false, 1.0, 1.0);
  214. }
  215. }
  216. void ComponentAnimator::cancelAllAnimations (const bool moveComponentsToTheirFinalPositions)
  217. {
  218. if (tasks.size() > 0)
  219. {
  220. if (moveComponentsToTheirFinalPositions)
  221. for (int i = tasks.size(); --i >= 0;)
  222. tasks.getUnchecked(i)->moveToFinalDestination();
  223. tasks.clear();
  224. sendChangeMessage();
  225. }
  226. }
  227. void ComponentAnimator::cancelAnimation (Component* const component,
  228. const bool moveComponentToItsFinalPosition)
  229. {
  230. if (AnimationTask* const at = findTaskFor (component))
  231. {
  232. if (moveComponentToItsFinalPosition)
  233. at->moveToFinalDestination();
  234. tasks.removeObject (at);
  235. sendChangeMessage();
  236. }
  237. }
  238. Rectangle<int> ComponentAnimator::getComponentDestination (Component* const component)
  239. {
  240. jassert (component != nullptr);
  241. if (AnimationTask* const at = findTaskFor (component))
  242. return at->destination;
  243. return component->getBounds();
  244. }
  245. bool ComponentAnimator::isAnimating (Component* component) const noexcept
  246. {
  247. return findTaskFor (component) != nullptr;
  248. }
  249. bool ComponentAnimator::isAnimating() const noexcept
  250. {
  251. return tasks.size() != 0;
  252. }
  253. void ComponentAnimator::timerCallback()
  254. {
  255. const uint32 timeNow = Time::getMillisecondCounter();
  256. if (lastTime == 0 || lastTime == timeNow)
  257. lastTime = timeNow;
  258. const int elapsed = (int) (timeNow - lastTime);
  259. for (int i = tasks.size(); --i >= 0;)
  260. {
  261. if (! tasks.getUnchecked(i)->useTimeslice (elapsed))
  262. {
  263. tasks.remove (i);
  264. sendChangeMessage();
  265. }
  266. }
  267. lastTime = timeNow;
  268. if (tasks.size() == 0)
  269. stopTimer();
  270. }