MirAnimatedButton.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. namespace Client.MirControls
  2. {
  3. public class MirAnimatedButton : MirButton
  4. {
  5. public static List<MirAnimatedButton> Animations = new List<MirAnimatedButton>();
  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
  130. {
  131. if (!Enabled)
  132. return base.Index;
  133. if (PressedIndex >= 0 && ActiveControl == this && MouseControl == this)
  134. return PressedIndex;
  135. if (HoverIndex >= 0 && MouseControl == this)
  136. return HoverIndex;
  137. return base.Index + OffSet;
  138. }
  139. set { base.Index = value; }
  140. }
  141. #region Loop
  142. private bool _loop;
  143. public event EventHandler LoopChanged;
  144. public bool Loop
  145. {
  146. get { return _loop; }
  147. set
  148. {
  149. if (_loop == value) return;
  150. _loop = value;
  151. OnLoopChanged(EventArgs.Empty);
  152. }
  153. }
  154. protected virtual void OnLoopChanged(EventArgs e)
  155. {
  156. if (LoopChanged != null)
  157. LoopChanged.Invoke(this, e);
  158. }
  159. #endregion
  160. #region OffSet
  161. private int _offSet;
  162. public event EventHandler OffSetChanged;
  163. public virtual int OffSet
  164. {
  165. protected get { return _offSet; }
  166. set
  167. {
  168. if (_offSet == value) return;
  169. _offSet = value;
  170. OnOffSetChanged(EventArgs.Empty);
  171. }
  172. }
  173. protected virtual void OnOffSetChanged(EventArgs e)
  174. {
  175. OnIndexChanged();
  176. if (OffSetChanged != null)
  177. OffSetChanged.Invoke(this, e);
  178. }
  179. private long _nextOffSet;
  180. #endregion
  181. public MirAnimatedButton()
  182. {
  183. _loop = true;
  184. _nextFadeTime = CMain.Time;
  185. _nextOffSet = CMain.Time;
  186. Animations.Add(this);
  187. }
  188. public void UpdateOffSet()
  189. {
  190. if (_fadeIn && CMain.Time > _nextFadeTime)
  191. {
  192. if ((Opacity += _fadeInRate) > 1F)
  193. {
  194. Opacity = 1F;
  195. _fadeIn = false;
  196. }
  197. _nextFadeTime = CMain.Time + _fadeInDelay;
  198. }
  199. if (!Visible || !_animated || _animationDelay == 0 || _animationCount == 0) return;
  200. if (CMain.Time < _nextOffSet) return;
  201. Redraw();
  202. _nextOffSet = CMain.Time + _animationDelay;
  203. if (++OffSet < _animationCount) return;
  204. EventHandler temp = AfterAnimation;
  205. AfterAnimation = null;
  206. if (!Loop)
  207. Animated = false;
  208. else
  209. OffSet = 0;
  210. if (temp != null)
  211. temp.Invoke(this, EventArgs.Empty);
  212. }
  213. #region Disposable
  214. protected override void Dispose(bool disposing)
  215. {
  216. base.Dispose(disposing);
  217. if (!disposing) return;
  218. AnimatedChanged = null;
  219. _animated = false;
  220. AnimationCountChanged = null;
  221. _animationCount = 0;
  222. AnimationDelayChanged = null;
  223. _animationDelay = 0;
  224. AfterAnimation = null;
  225. LoopChanged = null;
  226. _loop = false;
  227. OffSetChanged = null;
  228. _offSet = 0;
  229. _nextOffSet = 0;
  230. Animations.Remove(this);
  231. }
  232. #endregion
  233. }
  234. }