TimerDialog.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using S = ServerPackets;
  2. using Client.MirControls;
  3. using Client.MirGraphics;
  4. namespace Client.MirScenes.Dialogs
  5. {
  6. public sealed class TimerDialog : MirControl
  7. {
  8. private bool _timerStarted = false;
  9. private int _timerCounter = 0;
  10. private long _timerTime = 0;
  11. private readonly MirAnimatedControl _eggTimer = null;
  12. private readonly MirImageControl _1000 = null;
  13. private readonly MirImageControl _100 = null;
  14. private readonly MirImageControl _10 = null;
  15. private readonly MirImageControl _1 = null;
  16. private readonly MirImageControl _colon = null;
  17. private readonly int _libraryOffset = 900;
  18. private readonly List<ClientTimer> ActiveTimers = new List<ClientTimer>();
  19. private ClientTimer CurrentTimer = null;
  20. public TimerDialog()
  21. {
  22. Location = new Point(Settings.ScreenWidth - 120, Settings.ScreenHeight - 230);
  23. NotControl = true;
  24. Size = new Size(120, 100);
  25. Movable = false;
  26. Sort = true;
  27. _eggTimer = new MirAnimatedControl
  28. {
  29. Index = 960,
  30. AnimationCount = 6,
  31. AnimationDelay = 333,
  32. Library = Libraries.Prguse2,
  33. Parent = this,
  34. Location = new Point(23, 0),
  35. NotControl = true,
  36. UseOffSet = true,
  37. Animated = true,
  38. Loop = false,
  39. Opacity = 1F
  40. };
  41. _1000 = new MirImageControl
  42. {
  43. Parent = this,
  44. Index = _libraryOffset + 0,
  45. Library = Libraries.Prguse2,
  46. NotControl = true,
  47. UseOffSet = true,
  48. Location = new Point(0, 70),
  49. Visible = false
  50. };
  51. _100 = new MirImageControl
  52. {
  53. Parent = this,
  54. Index = _libraryOffset + 0,
  55. Library = Libraries.Prguse2,
  56. NotControl = true,
  57. UseOffSet = true,
  58. Location = new Point(22, 70),
  59. Visible = false
  60. };
  61. _colon = new MirImageControl
  62. {
  63. Parent = this,
  64. Index = _libraryOffset + 10,
  65. Library = Libraries.Prguse2,
  66. NotControl = true,
  67. UseOffSet = true,
  68. Location = new Point(44, 70),
  69. Visible = false
  70. };
  71. _10 = new MirImageControl
  72. {
  73. Parent = this,
  74. Index = _libraryOffset + 0,
  75. Library = Libraries.Prguse2,
  76. NotControl = true,
  77. UseOffSet = true,
  78. Location = new Point(58, 70),
  79. Visible = false
  80. };
  81. _1 = new MirImageControl
  82. {
  83. Parent = this,
  84. Index = _libraryOffset + 0,
  85. Library = Libraries.Prguse2,
  86. NotControl = true,
  87. UseOffSet = true,
  88. Location = new Point(80, 70),
  89. Visible = false
  90. };
  91. }
  92. public void Process()
  93. {
  94. var timer = GetBestTimer();
  95. if (timer != null)
  96. {
  97. if (timer != CurrentTimer || timer.Refresh)
  98. {
  99. CurrentTimer = timer;
  100. CurrentTimer.Refresh = false;
  101. _timerStarted = true;
  102. _timerTime = CMain.Time + 1000;
  103. _timerCounter = (int)(CurrentTimer.RelativeTime - (CMain.Time / 1000));
  104. UpdateTimeGraphic();
  105. }
  106. }
  107. if (CurrentTimer == null || _timerStarted == false || CMain.Time < _timerTime) return;
  108. _timerCounter--;
  109. _timerTime = CMain.Time + 1000;
  110. if (_timerCounter < 0 && _eggTimer != null)
  111. {
  112. Visible = false;
  113. _1000.Visible = _100.Visible = _10.Visible = _1.Visible = _colon.Visible = false;
  114. _eggTimer.Loop = false;
  115. _timerStarted = false;
  116. ActiveTimers.Remove(CurrentTimer);
  117. return;
  118. }
  119. UpdateTimeGraphic();
  120. }
  121. private ClientTimer GetBestTimer()
  122. {
  123. return ActiveTimers.OrderBy(x => x.RelativeTime).FirstOrDefault();
  124. }
  125. public ClientTimer GetTimer(string key)
  126. {
  127. return ActiveTimers.FirstOrDefault(x => x.Key == key);
  128. }
  129. public void AddTimer(S.SetTimer p)
  130. {
  131. var currentTimer = GetTimer(p.Key);
  132. if (currentTimer != null)
  133. {
  134. currentTimer.Update(p.Seconds, p.Type);
  135. return;
  136. }
  137. ActiveTimers.Add(new ClientTimer(p.Key, p.Seconds, p.Type));
  138. }
  139. public void ExpireTimer(string key)
  140. {
  141. var timer = ActiveTimers.FirstOrDefault(x => x.Key == key);
  142. if (timer != null)
  143. {
  144. timer.RelativeTime = 0;
  145. if (timer == CurrentTimer)
  146. {
  147. _timerCounter = 0;
  148. }
  149. }
  150. }
  151. private void UpdateTimeGraphic()
  152. {
  153. TimeSpan ts = new TimeSpan(0, 0, _timerCounter);
  154. if (ts.Hours > 0)
  155. {
  156. _1000.Index = _libraryOffset + (ts.Hours / 10);
  157. _100.Index = _libraryOffset + (ts.Hours % 10);
  158. _10.Index = _libraryOffset + (ts.Minutes / 10);
  159. _1.Index = _libraryOffset + (ts.Minutes % 10);
  160. }
  161. else
  162. {
  163. _1000.Index = _libraryOffset + (ts.Minutes / 10);
  164. _100.Index = _libraryOffset + (ts.Minutes % 10);
  165. _10.Index = _libraryOffset + (ts.Seconds / 10);
  166. _1.Index = _libraryOffset + (ts.Seconds % 10);
  167. }
  168. Visible = true;
  169. _1000.Visible = _100.Visible = _10.Visible = _1.Visible = _colon.Visible = true;
  170. switch (CurrentTimer.Type)
  171. {
  172. default:
  173. case 0:
  174. _eggTimer.Visible = false;
  175. _eggTimer.Index = 960;
  176. break;
  177. case 1:
  178. _eggTimer.Visible = true;
  179. _eggTimer.Index = 960;
  180. break;
  181. case 2:
  182. _eggTimer.Visible = true;
  183. _eggTimer.Index = 440;
  184. break;
  185. }
  186. _eggTimer.Loop = true;
  187. }
  188. }
  189. public class ClientTimer
  190. {
  191. public string Key;
  192. public byte Type;
  193. public int Seconds;
  194. public long RelativeTime;
  195. public bool Refresh;
  196. public ClientTimer(string key, int time, byte type)
  197. {
  198. Key = key;
  199. Update(time, type);
  200. }
  201. public void Update(int time, byte type)
  202. {
  203. Seconds = time;
  204. Type = type;
  205. RelativeTime = Seconds + (CMain.Time / 1000);
  206. Refresh = true;
  207. }
  208. }
  209. }