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.

juce_ComponentAnimator.cpp 11KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. }