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.

351 lines
11KB

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