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.

345 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. 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.reset (new ProxyComponent (*component));
  49. else
  50. proxy.reset();
  51. component->setVisible (! useProxyComponent);
  52. }
  53. bool useTimeslice (const int elapsed)
  54. {
  55. if (auto* c = proxy != nullptr ? proxy.get()
  56. : component.get())
  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().findDisplayForRect (getScreenBounds()).scale;
  133. image = c.createComponentSnapshot (c.getLocalBounds(), false, scale);
  134. setVisible (true);
  135. toBehind (&c);
  136. }
  137. void paint (Graphics& g) override
  138. {
  139. g.setOpacity (1.0f);
  140. g.drawImageTransformed (image, AffineTransform::scale (getWidth() / (float) image.getWidth(),
  141. getHeight() / (float) image.getHeight()), false);
  142. }
  143. private:
  144. Image image;
  145. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProxyComponent)
  146. };
  147. WeakReference<Component> component;
  148. std::unique_ptr<Component> proxy;
  149. Rectangle<int> destination;
  150. double destAlpha;
  151. int msElapsed, msTotal;
  152. double startSpeed, midSpeed, endSpeed, lastProgress;
  153. double left, top, right, bottom, alpha;
  154. bool isMoving, isChangingAlpha;
  155. private:
  156. double timeToDistance (const double time) const noexcept
  157. {
  158. return (time < 0.5) ? time * (startSpeed + time * (midSpeed - startSpeed))
  159. : 0.5 * (startSpeed + 0.5 * (midSpeed - startSpeed))
  160. + (time - 0.5) * (midSpeed + (time - 0.5) * (endSpeed - midSpeed));
  161. }
  162. JUCE_DECLARE_WEAK_REFERENCEABLE (AnimationTask)
  163. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationTask)
  164. };
  165. //==============================================================================
  166. ComponentAnimator::ComponentAnimator() : lastTime (0) {}
  167. ComponentAnimator::~ComponentAnimator() {}
  168. //==============================================================================
  169. ComponentAnimator::AnimationTask* ComponentAnimator::findTaskFor (Component* const component) const noexcept
  170. {
  171. for (int i = tasks.size(); --i >= 0;)
  172. if (component == tasks.getUnchecked(i)->component.get())
  173. return tasks.getUnchecked(i);
  174. return nullptr;
  175. }
  176. void ComponentAnimator::animateComponent (Component* const component,
  177. const Rectangle<int>& finalBounds,
  178. const float finalAlpha,
  179. const int millisecondsToSpendMoving,
  180. const bool useProxyComponent,
  181. const double startSpeed,
  182. const double endSpeed)
  183. {
  184. // the speeds must be 0 or greater!
  185. jassert (startSpeed >= 0 && endSpeed >= 0);
  186. if (component != nullptr)
  187. {
  188. auto* at = findTaskFor (component);
  189. if (at == nullptr)
  190. {
  191. at = new AnimationTask (component);
  192. tasks.add (at);
  193. sendChangeMessage();
  194. }
  195. at->reset (finalBounds, finalAlpha, millisecondsToSpendMoving,
  196. useProxyComponent, startSpeed, endSpeed);
  197. if (! isTimerRunning())
  198. {
  199. lastTime = Time::getMillisecondCounter();
  200. startTimerHz (50);
  201. }
  202. }
  203. }
  204. void ComponentAnimator::fadeOut (Component* component, int millisecondsToTake)
  205. {
  206. if (component != nullptr)
  207. {
  208. if (component->isShowing() && millisecondsToTake > 0)
  209. animateComponent (component, component->getBounds(), 0.0f, millisecondsToTake, true, 1.0, 1.0);
  210. component->setVisible (false);
  211. }
  212. }
  213. void ComponentAnimator::fadeIn (Component* component, int millisecondsToTake)
  214. {
  215. if (component != nullptr && ! (component->isVisible() && component->getAlpha() == 1.0f))
  216. {
  217. component->setAlpha (0.0f);
  218. component->setVisible (true);
  219. animateComponent (component, component->getBounds(), 1.0f, millisecondsToTake, false, 1.0, 1.0);
  220. }
  221. }
  222. void ComponentAnimator::cancelAllAnimations (const bool moveComponentsToTheirFinalPositions)
  223. {
  224. if (tasks.size() > 0)
  225. {
  226. if (moveComponentsToTheirFinalPositions)
  227. for (int i = tasks.size(); --i >= 0;)
  228. tasks.getUnchecked(i)->moveToFinalDestination();
  229. tasks.clear();
  230. sendChangeMessage();
  231. }
  232. }
  233. void ComponentAnimator::cancelAnimation (Component* const component,
  234. const bool moveComponentToItsFinalPosition)
  235. {
  236. if (auto* at = findTaskFor (component))
  237. {
  238. if (moveComponentToItsFinalPosition)
  239. at->moveToFinalDestination();
  240. tasks.removeObject (at);
  241. sendChangeMessage();
  242. }
  243. }
  244. Rectangle<int> ComponentAnimator::getComponentDestination (Component* const component)
  245. {
  246. jassert (component != nullptr);
  247. if (auto* at = findTaskFor (component))
  248. return at->destination;
  249. return component->getBounds();
  250. }
  251. bool ComponentAnimator::isAnimating (Component* component) const noexcept
  252. {
  253. return findTaskFor (component) != nullptr;
  254. }
  255. bool ComponentAnimator::isAnimating() const noexcept
  256. {
  257. return tasks.size() != 0;
  258. }
  259. void ComponentAnimator::timerCallback()
  260. {
  261. auto timeNow = Time::getMillisecondCounter();
  262. if (lastTime == 0)
  263. lastTime = timeNow;
  264. auto elapsed = (int) (timeNow - lastTime);
  265. for (auto* task : Array<AnimationTask*> (tasks.begin(), tasks.size()))
  266. {
  267. if (tasks.contains (task) && ! task->useTimeslice (elapsed))
  268. {
  269. tasks.removeObject (task);
  270. sendChangeMessage();
  271. }
  272. }
  273. lastTime = timeNow;
  274. if (tasks.size() == 0)
  275. stopTimer();
  276. }
  277. } // namespace juce