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.

348 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. class ComponentAnimator::AnimationTask
  20. {
  21. public:
  22. AnimationTask (Component* c) noexcept : component (c) {}
  23. ~AnimationTask()
  24. {
  25. masterReference.clear();
  26. }
  27. void reset (const Rectangle<int>& finalBounds,
  28. float finalAlpha,
  29. int millisecondsToSpendMoving,
  30. bool useProxyComponent,
  31. double startSpd, double endSpd)
  32. {
  33. msElapsed = 0;
  34. msTotal = jmax (1, millisecondsToSpendMoving);
  35. lastProgress = 0;
  36. destination = finalBounds;
  37. destAlpha = finalAlpha;
  38. isMoving = (finalBounds != component->getBounds());
  39. isChangingAlpha = (finalAlpha != component->getAlpha());
  40. left = component->getX();
  41. top = component->getY();
  42. right = component->getRight();
  43. bottom = component->getBottom();
  44. alpha = component->getAlpha();
  45. const double invTotalDistance = 4.0 / (startSpd + endSpd + 2.0);
  46. startSpeed = jmax (0.0, startSpd * invTotalDistance);
  47. midSpeed = invTotalDistance;
  48. endSpeed = jmax (0.0, endSpd * invTotalDistance);
  49. if (useProxyComponent)
  50. proxy = new ProxyComponent (*component);
  51. else
  52. proxy = nullptr;
  53. component->setVisible (! useProxyComponent);
  54. }
  55. bool useTimeslice (const int elapsed)
  56. {
  57. if (auto* c = proxy != nullptr ? static_cast<Component*> (proxy)
  58. : static_cast<Component*> (component))
  59. {
  60. msElapsed += elapsed;
  61. double newProgress = msElapsed / (double) msTotal;
  62. if (newProgress >= 0 && newProgress < 1.0)
  63. {
  64. const WeakReference<AnimationTask> weakRef (this);
  65. newProgress = timeToDistance (newProgress);
  66. const double delta = (newProgress - lastProgress) / (1.0 - lastProgress);
  67. jassert (newProgress >= lastProgress);
  68. lastProgress = newProgress;
  69. if (delta < 1.0)
  70. {
  71. bool stillBusy = false;
  72. if (isMoving)
  73. {
  74. left += (destination.getX() - left) * delta;
  75. top += (destination.getY() - top) * delta;
  76. right += (destination.getRight() - right) * delta;
  77. bottom += (destination.getBottom() - bottom) * delta;
  78. const Rectangle<int> newBounds (roundToInt (left),
  79. roundToInt (top),
  80. roundToInt (right - left),
  81. roundToInt (bottom - top));
  82. if (newBounds != destination)
  83. {
  84. c->setBounds (newBounds);
  85. stillBusy = true;
  86. }
  87. }
  88. // Check whether the animation was cancelled/deleted during
  89. // a callback during the setBounds method
  90. if (weakRef.wasObjectDeleted())
  91. return false;
  92. if (isChangingAlpha)
  93. {
  94. alpha += (destAlpha - alpha) * delta;
  95. c->setAlpha ((float) alpha);
  96. stillBusy = true;
  97. }
  98. if (stillBusy)
  99. return true;
  100. }
  101. }
  102. }
  103. moveToFinalDestination();
  104. return false;
  105. }
  106. void moveToFinalDestination()
  107. {
  108. if (component != nullptr)
  109. {
  110. const WeakReference<AnimationTask> weakRef (this);
  111. component->setAlpha ((float) destAlpha);
  112. component->setBounds (destination);
  113. if (! weakRef.wasObjectDeleted())
  114. if (proxy != nullptr)
  115. component->setVisible (destAlpha > 0);
  116. }
  117. }
  118. //==============================================================================
  119. struct ProxyComponent : public Component
  120. {
  121. ProxyComponent (Component& c)
  122. {
  123. setWantsKeyboardFocus (false);
  124. setBounds (c.getBounds());
  125. setTransform (c.getTransform());
  126. setAlpha (c.getAlpha());
  127. setInterceptsMouseClicks (false, false);
  128. if (auto* parent = c.getParentComponent())
  129. parent->addAndMakeVisible (this);
  130. else if (c.isOnDesktop() && c.getPeer() != nullptr)
  131. addToDesktop (c.getPeer()->getStyleFlags() | ComponentPeer::windowIgnoresKeyPresses);
  132. else
  133. jassertfalse; // seem to be trying to animate a component that's not visible..
  134. auto scale = (float) Desktop::getInstance().getDisplays()
  135. .getDisplayContaining (getScreenBounds().getCentre()).scale;
  136. image = c.createComponentSnapshot (c.getLocalBounds(), false, scale);
  137. setVisible (true);
  138. toBehind (&c);
  139. }
  140. void paint (Graphics& g) override
  141. {
  142. g.setOpacity (1.0f);
  143. g.drawImageTransformed (image, AffineTransform::scale (getWidth() / (float) image.getWidth(),
  144. getHeight() / (float) image.getHeight()), false);
  145. }
  146. private:
  147. Image image;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProxyComponent)
  149. };
  150. WeakReference<AnimationTask>::Master masterReference;
  151. friend class WeakReference<AnimationTask>;
  152. WeakReference<Component> component;
  153. ScopedPointer<Component> proxy;
  154. Rectangle<int> destination;
  155. double destAlpha;
  156. int msElapsed, msTotal;
  157. double startSpeed, midSpeed, endSpeed, lastProgress;
  158. double left, top, right, bottom, alpha;
  159. bool isMoving, isChangingAlpha;
  160. private:
  161. double timeToDistance (const double time) const noexcept
  162. {
  163. return (time < 0.5) ? time * (startSpeed + time * (midSpeed - startSpeed))
  164. : 0.5 * (startSpeed + 0.5 * (midSpeed - startSpeed))
  165. + (time - 0.5) * (midSpeed + (time - 0.5) * (endSpeed - midSpeed));
  166. }
  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. }