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

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