/NBF/agTweener/Tweener.cs

# · C# · 197 lines · 90 code · 17 blank · 90 comment · 12 complexity · 582b07d958f38d967f7b6936709a4c75 MD5 · raw file

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Animation;
  6. using System.Collections.Generic;
  7. namespace agTweener
  8. {
  9. /// <summary>
  10. /// Tweener
  11. /// Controller for the Tweener class
  12. ///
  13. /// author Michael Cameron
  14. /// version 1.0.1
  15. /// Based on Flash Tweener Actionscript library by Zeh Fernando, Nate Chatellier
  16. /// Updated for Silverlight 2.0 Beta 1
  17. /// </summary>
  18. public delegate void TweenEvent(params object[] args);
  19. public class Tweener
  20. {
  21. private static List<TweenListItem> _tweenlist = new List<TweenListItem>();
  22. private static TimeSpan tickInterval;
  23. private static Storyboard _timer;
  24. public Tweener()
  25. {
  26. }
  27. public static int TweenCount
  28. {
  29. get { return _tweenlist.Count; }
  30. }
  31. public static Storyboard timer
  32. {
  33. get { return _timer; }
  34. set { _timer = value; }
  35. }
  36. /// <summary>
  37. /// Tween method for a FrameworkElement, simplest method with no event callbacks
  38. /// </summary>
  39. /// <param name="target"></param>
  40. /// <param name="p"></param>
  41. /// <returns></returns>
  42. public static bool addTween(FrameworkElement target, TweenParameter p)
  43. {
  44. return addTween(target, p, null, null, null, null, null, null);
  45. }
  46. /// <summary>
  47. /// Tween method for a FrameworkElement - override with onComplete event with params
  48. /// </summary>
  49. /// <param name="target"></param>
  50. /// <param name="p"></param>
  51. /// <param name="onComplete"></param>
  52. /// <param name="onCompleteParams"></param>
  53. /// <returns></returns>
  54. public static bool addTween(FrameworkElement target, TweenParameter p, TweenEvent onComplete, object[] onCompleteParams)
  55. {
  56. return addTween(target, p, null, null, null, null, onComplete, onCompleteParams);
  57. }
  58. /// <summary>
  59. /// Tween method for a FrameworkElement - override with onUpdate and onComplete events with params
  60. /// </summary>
  61. /// <param name="target"></param>
  62. /// <param name="p"></param>
  63. /// <param name="onUpdate"></param>
  64. /// <param name="onUpdateParams"></param>
  65. /// <param name="onComplete"></param>
  66. /// <param name="onCompleteParams"></param>
  67. /// <returns></returns>
  68. public static bool addTween(FrameworkElement target, TweenParameter p, TweenEvent onUpdate, object[] onUpdateParams, TweenEvent onComplete, object[] onCompleteParams)
  69. {
  70. return addTween(target, p, null, null, onUpdate, onUpdateParams, onComplete, onCompleteParams);
  71. }
  72. /// <summary>
  73. /// Tween method for a FrameworkElement - override with onStart, onUpdate and onComplete events with params
  74. /// </summary>
  75. /// <param name="target"></param>
  76. /// <param name="p"></param>
  77. /// <param name="onStart"></param>
  78. /// <param name="onStartParams"></param>
  79. /// <param name="onUpdate"></param>
  80. /// <param name="onUpdateParams"></param>
  81. /// <param name="onComplete"></param>
  82. /// <param name="onCompleteParams"></param>
  83. /// <returns></returns>
  84. public static bool addTween(FrameworkElement target, TweenParameter p, TweenEvent onStart, object[] onStartParams, TweenEvent onUpdate, object[] onUpdateParams, TweenEvent onComplete, object[] onCompleteParams)
  85. {
  86. TweenListItem t = new TweenListItem(target, p, onStart, onStartParams, onUpdate, onUpdateParams, onComplete, onCompleteParams);
  87. _tweenlist.Add(t);
  88. t.Completed += RemoveTween;
  89. if (_timer == null) InitTimer(target);
  90. return true;
  91. }
  92. /// <summary>
  93. /// Tween method for an array of doubles not associated with a FrameworkElement
  94. /// Updates to the values throughout the tween should be retrieved through the OnUpdate event
  95. /// Target values should be set in the TweenParameter.DoubleArray property
  96. /// </summary>
  97. /// <param name="doubleArray"></param>
  98. /// <param name="p"></param>
  99. /// <param name="onStart"></param>
  100. /// <param name="onStartParams"></param>
  101. /// <param name="onUpdate"></param>
  102. /// <param name="onUpdateParams"></param>
  103. /// <param name="onComplete"></param>
  104. /// <param name="onCompleteParams"></param>
  105. /// <returns></returns>
  106. public static bool addTween(double[] doubleArray, TweenParameter p, TweenEvent onStart, object[] onStartParams, TweenEvent onUpdate, object[] onUpdateParams, TweenEvent onComplete, object[] onCompleteParams)
  107. {
  108. TweenListItem t = new TweenListItem(doubleArray, p, onStart, onStartParams, onUpdate, onUpdateParams, onComplete, onCompleteParams);
  109. _tweenlist.Add(t);
  110. t.Completed += RemoveTween;
  111. if (p.TimerParent != null)
  112. if (_timer == null) InitTimer(p.TimerParent);
  113. return true;
  114. }
  115. /// <summary>
  116. /// Tween method for a point not associated with a FrameworkElement
  117. /// Updates to the values throughout the tween should be retrieved through the OnUpdate event
  118. /// Target values should be set in the TweenParameter.DoubleArray property
  119. /// </summary>
  120. /// <param name="point"></param>
  121. /// <param name="p"></param>
  122. /// <param name="onStart"></param>
  123. /// <param name="onStartParams"></param>
  124. /// <param name="onUpdate"></param>
  125. /// <param name="onUpdateParams"></param>
  126. /// <param name="onComplete"></param>
  127. /// <param name="onCompleteParams"></param>
  128. /// <returns></returns>
  129. public static bool addTween(Point point, TweenParameter p, TweenEvent onStart, object[] onStartParams, TweenEvent onUpdate, object[] onUpdateParams, TweenEvent onComplete, object[] onCompleteParams)
  130. {
  131. TweenListItem t = new TweenListItem(point, p, onStart, onStartParams, onUpdate, onUpdateParams, onComplete, onCompleteParams);
  132. _tweenlist.Add(t);
  133. t.Completed += RemoveTween;
  134. if (p.TimerParent != null)
  135. if (_timer == null) InitTimer(p.TimerParent);
  136. return true;
  137. }
  138. /// <summary>
  139. /// Removes a TweenListItem from the collection
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private static void RemoveTween(object sender, EventArgs e)
  144. {
  145. TweenListItem ti = (TweenListItem)sender;
  146. _tweenlist.Remove(ti);
  147. if (ti.OnComplete != null)
  148. ti.OnComplete(ti.OnCompleteParams);
  149. }
  150. /// <summary>
  151. /// Initialize the storyboard timer
  152. /// </summary>
  153. /// <param name="target"></param>
  154. private static void InitTimer(FrameworkElement target)
  155. {
  156. tickInterval = new TimeSpan(0, 0, 0, 0, 20);
  157. timer = new Storyboard();
  158. timer.Duration = new Duration(tickInterval);
  159. // If parent of target exists then add timer to this canvas otherwise
  160. // add the timer to the target canvas
  161. ((FrameworkElement)((FrameworkElement)target.Parent).Parent).Resources.Add("tweenStoryboard", timer);
  162. timer.Completed += new EventHandler(Tick);
  163. timer.Begin();
  164. }
  165. /// <summary>
  166. /// Handles the overall storyboard timer tick, calls HandleTick for each TweenListItem
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private static void Tick(Object sender, EventArgs e)
  171. {
  172. // Notify all items in the tweenlist of the tick event
  173. _tweenlist.ForEach(delegate(TweenListItem t)
  174. {
  175. t.HandleTick(tickInterval);
  176. });
  177. timer.Begin();
  178. }
  179. }
  180. }