PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/MetroFramework/Animation/AnimationBase.cs

https://github.com/engy/Mist
C# | 189 lines | 131 code | 25 blank | 33 comment | 17 complexity | fb29dfcbe27061fcbb0860d16a5c2f70 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * MetroFramework - Modern UI for WinForms
  3. *
  4. * The MIT License (MIT)
  5. * Copyright (c) 2011 Sven Walter, http://github.com/viperneo
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in the
  9. * Software without restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so, subject to the
  12. * following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  18. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  19. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  21. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  22. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. using System;
  25. using System.Windows.Forms;
  26. namespace MetroFramework.Animation
  27. {
  28. public delegate void AnimationAction();
  29. public delegate bool AnimationFinishedEvaluator();
  30. public abstract class AnimationBase
  31. {
  32. public event EventHandler AnimationCompleted;
  33. private void OnAnimationCompleted()
  34. {
  35. if (AnimationCompleted != null)
  36. AnimationCompleted(this, EventArgs.Empty);
  37. }
  38. private DelayedCall timer;
  39. private Control targetControl;
  40. private AnimationAction actionHandler;
  41. private AnimationFinishedEvaluator evaluatorHandler;
  42. protected TransitionType transitionType;
  43. protected int counter;
  44. protected int startTime;
  45. protected int targetTime;
  46. public bool IsCompleted
  47. {
  48. get
  49. {
  50. if (timer != null)
  51. return !timer.IsWaiting;
  52. return true;
  53. }
  54. }
  55. public bool IsRunning
  56. {
  57. get
  58. {
  59. if (timer != null)
  60. return timer.IsWaiting;
  61. return false;
  62. }
  63. }
  64. public void Cancel()
  65. {
  66. if (IsRunning)
  67. timer.Cancel();
  68. }
  69. protected void Start(Control control, TransitionType transitionType, int duration, AnimationAction actionHandler)
  70. {
  71. Start(control, transitionType, duration, actionHandler, null);
  72. }
  73. protected void Start(Control control, TransitionType transitionType, int duration, AnimationAction actionHandler, AnimationFinishedEvaluator evaluatorHandler)
  74. {
  75. this.targetControl = control;
  76. this.transitionType = transitionType;
  77. this.actionHandler = actionHandler;
  78. this.evaluatorHandler = evaluatorHandler;
  79. this.counter = 0;
  80. this.startTime = 0;
  81. this.targetTime = duration;
  82. timer = DelayedCall.Start(DoAnimation, duration);
  83. }
  84. private void DoAnimation()
  85. {
  86. if (evaluatorHandler == null || evaluatorHandler.Invoke())
  87. {
  88. OnAnimationCompleted();
  89. }
  90. else
  91. {
  92. actionHandler.Invoke();
  93. counter++;
  94. timer.Start();
  95. }
  96. }
  97. protected int MakeTransition(float t, float b, float d, float c)
  98. {
  99. switch (transitionType)
  100. {
  101. case TransitionType.Linear:
  102. // simple linear tweening - no easing
  103. return (int)(c * t / d + b);
  104. case TransitionType.EaseInQuad:
  105. // quadratic (t^2) easing in - accelerating from zero velocity
  106. return (int)(c * (t /= d) * t + b);
  107. case TransitionType.EaseOutQuad:
  108. // quadratic (t^2) easing out - decelerating to zero velocity
  109. return (int)(-c * (t = t / d) * (t - 2) + b);
  110. case TransitionType.EaseInOutQuad:
  111. // quadratic easing in/out - acceleration until halfway, then deceleration
  112. if ((t /= d / 2) < 1)
  113. {
  114. return (int)(c / 2 * t * t + b);
  115. }
  116. else
  117. {
  118. return (int)(-c / 2 * ((--t) * (t - 2) - 1) + b);
  119. }
  120. case TransitionType.EaseInCubic:
  121. // cubic easing in - accelerating from zero velocity
  122. return (int)(c * (t /= d) * t * t + b);
  123. case TransitionType.EaseOutCubic:
  124. // cubic easing in - accelerating from zero velocity
  125. return (int)(c * ((t = t / d - 1) * t * t + 1) + b);
  126. case TransitionType.EaseInOutCubic:
  127. // cubic easing in - accelerating from zero velocity
  128. if ((t /= d / 2) < 1)
  129. {
  130. return (int)(c / 2 * t * t * t + b);
  131. }
  132. else
  133. {
  134. return (int)(c / 2 * ((t -= 2) * t * t + 2) + b);
  135. }
  136. case TransitionType.EaseInQuart:
  137. // quartic easing in - accelerating from zero velocity
  138. return (int)(c * (t /= d) * t * t * t + b);
  139. case TransitionType.EaseInExpo:
  140. // exponential (2^t) easing in - accelerating from zero velocity
  141. if (t == 0)
  142. {
  143. return (int)b;
  144. }
  145. else
  146. {
  147. return (int)(c * Math.Pow(2, (10 * (t / d - 1))) + b);
  148. }
  149. case TransitionType.EaseOutExpo:
  150. // exponential (2^t) easing out - decelerating to zero velocity
  151. if (t == d)
  152. {
  153. return (int)(b + c);
  154. }
  155. else
  156. {
  157. return (int)(c * (-Math.Pow(2, -10 * t / d) + 1) + b);
  158. }
  159. default:
  160. return 0;
  161. }
  162. }
  163. }
  164. }