MirAnimatedControl.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. namespace Client.MirControls
  2. {
  3. public class MirAnimatedControl : MirImageControl
  4. {
  5. public static List<MirAnimatedControl> Animations = new List<MirAnimatedControl>();
  6. #region Animated
  7. private bool _animated;
  8. public event EventHandler AnimatedChanged;
  9. public bool Animated
  10. {
  11. get { return _animated; }
  12. set
  13. {
  14. if (_animated == value) return;
  15. _animated = value;
  16. _nextOffSet = CMain.Time + _fadeInDelay;
  17. OnAnimatedChanged(EventArgs.Empty);
  18. }
  19. }
  20. protected virtual void OnAnimatedChanged(EventArgs e)
  21. {
  22. Redraw();
  23. if (AnimatedChanged != null)
  24. AnimatedChanged.Invoke(this, e);
  25. }
  26. #endregion
  27. #region Animation Count
  28. private int _animationCount;
  29. public event EventHandler AnimationCountChanged;
  30. public virtual int AnimationCount
  31. {
  32. get { return _animationCount; }
  33. set
  34. {
  35. if (_animationCount == value) return;
  36. _animationCount = value;
  37. OnAnimationCountChanged(EventArgs.Empty);
  38. }
  39. }
  40. protected virtual void OnAnimationCountChanged(EventArgs e)
  41. {
  42. if (AnimationCountChanged != null)
  43. AnimationCountChanged.Invoke(this, e);
  44. }
  45. #endregion
  46. #region Animation Delay
  47. private long _animationDelay;
  48. public event EventHandler AnimationDelayChanged;
  49. public long AnimationDelay
  50. {
  51. get { return _animationDelay; }
  52. set
  53. {
  54. if (_animationDelay == value) return;
  55. _animationDelay = value;
  56. OnAnimationDelayChanged();
  57. }
  58. }
  59. protected virtual void OnAnimationDelayChanged()
  60. {
  61. if (AnimationDelayChanged != null)
  62. AnimationDelayChanged.Invoke(this, EventArgs.Empty);
  63. }
  64. #endregion
  65. #region FadeIn
  66. private long _nextFadeTime;
  67. private bool _fadeIn;
  68. public event EventHandler FadeInChanged;
  69. public bool FadeIn
  70. {
  71. get { return _fadeIn; }
  72. set
  73. {
  74. if (_fadeIn == value) return;
  75. _nextFadeTime = CMain.Time + _fadeInDelay;
  76. _fadeIn = value;
  77. OnFadeInChanged(EventArgs.Empty);
  78. }
  79. }
  80. protected virtual void OnFadeInChanged(EventArgs e)
  81. {
  82. if (FadeInChanged != null)
  83. FadeInChanged.Invoke(this, e);
  84. }
  85. #endregion
  86. #region FadeIn Rate
  87. private float _fadeInRate;
  88. public event EventHandler FadeInRateChanged;
  89. public virtual float FadeInRate
  90. {
  91. get { return _fadeInRate; }
  92. set
  93. {
  94. if (_fadeInRate == value) return;
  95. _fadeInRate = value;
  96. OnFadeInRateChanged(EventArgs.Empty);
  97. }
  98. }
  99. protected virtual void OnFadeInRateChanged(EventArgs e)
  100. {
  101. if (FadeInRateChanged != null)
  102. FadeInRateChanged.Invoke(this, e);
  103. }
  104. #endregion
  105. #region FadeIn Delay
  106. private long _fadeInDelay;
  107. public event EventHandler FadeInDelayChanged;
  108. public long FadeInDelay
  109. {
  110. get { return _fadeInDelay; }
  111. set
  112. {
  113. if (_fadeInDelay == value) return;
  114. _fadeInDelay = value;
  115. OnFadeInDelayChanged();
  116. }
  117. }
  118. protected virtual void OnFadeInDelayChanged()
  119. {
  120. if (FadeInDelayChanged != null)
  121. FadeInDelayChanged.Invoke(this, EventArgs.Empty);
  122. }
  123. #endregion
  124. #region Events
  125. public event EventHandler AfterAnimation;
  126. #endregion
  127. public override int Index
  128. {
  129. get { return base.Index + OffSet; }
  130. set { base.Index = value; }
  131. }
  132. #region Loop
  133. private bool _loop;
  134. public event EventHandler LoopChanged;
  135. public bool Loop
  136. {
  137. get { return _loop; }
  138. set
  139. {
  140. if (_loop == value) return;
  141. _loop = value;
  142. OnLoopChanged(EventArgs.Empty);
  143. }
  144. }
  145. protected virtual void OnLoopChanged(EventArgs e)
  146. {
  147. if (LoopChanged != null)
  148. LoopChanged.Invoke(this, e);
  149. }
  150. #endregion
  151. #region OffSet
  152. private int _offSet;
  153. public event EventHandler OffSetChanged;
  154. public virtual int OffSet
  155. {
  156. protected get { return _offSet; }
  157. set
  158. {
  159. if (_offSet == value) return;
  160. _offSet = value;
  161. OnOffSetChanged(EventArgs.Empty);
  162. }
  163. }
  164. protected virtual void OnOffSetChanged(EventArgs e)
  165. {
  166. OnIndexChanged();
  167. if (OffSetChanged != null)
  168. OffSetChanged.Invoke(this, e);
  169. }
  170. private long _nextOffSet;
  171. #endregion
  172. public MirAnimatedControl()
  173. {
  174. _loop = true;
  175. _nextFadeTime = CMain.Time;
  176. _nextOffSet = CMain.Time;
  177. Animations.Add(this);
  178. }
  179. public void UpdateOffSet()
  180. {
  181. if (_fadeIn && CMain.Time > _nextFadeTime)
  182. {
  183. if ((Opacity += _fadeInRate) > 1F)
  184. {
  185. Opacity = 1F;
  186. _fadeIn = false;
  187. }
  188. _nextFadeTime = CMain.Time + _fadeInDelay;
  189. }
  190. if (!Visible || !_animated || _animationDelay == 0 || _animationCount == 0) return;
  191. if (CMain.Time < _nextOffSet) return;
  192. Redraw();
  193. _nextOffSet = CMain.Time + _animationDelay;
  194. if (++OffSet < _animationCount) return;
  195. EventHandler temp = AfterAnimation;
  196. //AfterAnimation = null;
  197. if (!Loop)
  198. Animated = false;
  199. else
  200. OffSet = 0;
  201. if (temp != null)
  202. temp.Invoke(this, EventArgs.Empty);
  203. }
  204. #region Disposable
  205. protected override void Dispose(bool disposing)
  206. {
  207. base.Dispose(disposing);
  208. if (!disposing) return;
  209. AnimatedChanged = null;
  210. _animated = false;
  211. AnimationCountChanged = null;
  212. _animationCount = 0;
  213. AnimationDelayChanged = null;
  214. _animationDelay = 0;
  215. AfterAnimation = null;
  216. LoopChanged = null;
  217. _loop = false;
  218. OffSetChanged = null;
  219. _offSet = 0;
  220. _nextOffSet = 0;
  221. Animations.Remove(this);
  222. }
  223. #endregion
  224. }
  225. }