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.

346 lines
11KB

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