Animator.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace ImpulseVision
  10. {
  11. public static class Animator
  12. {
  13. public static List<Animation> AnimationList = new List<Animation>();
  14. public static int Count()
  15. {
  16. return AnimationList.Count;
  17. }
  18. private static Thread AnimatorThread;
  19. private static double Interval;
  20. public static bool IsWork = false;
  21. public static void Start()
  22. {
  23. if (IsWork) return;
  24. IsWork = true;
  25. Interval = 14; // FPS ~66
  26. AnimatorThread = new Thread(AnimationInvoker)
  27. {
  28. IsBackground = true,
  29. Name = "UI Animation"
  30. };
  31. AnimatorThread.Start();
  32. }
  33. private static void AnimationInvoker()
  34. {
  35. while (IsWork)
  36. {
  37. AnimationList.RemoveAll(a => a == null || a.Status == Animation.AnimationStatus.Completed);
  38. Parallel.For(0, Count(), index =>
  39. {
  40. AnimationList[index].UpdateFrame();
  41. });
  42. Thread.Sleep((int)Interval);
  43. }
  44. }
  45. public static void Request(Animation Anim, bool ReplaceIfExists = true)
  46. {
  47. if (AnimatorThread == null || IsWork == false)
  48. {
  49. Start();
  50. }
  51. Debug.WriteLine("Запуск анимации: " + Anim.ID + "| TargetValue: " + Anim.TargetValue);
  52. Anim.Status = Animation.AnimationStatus.Requested;
  53. Animation dupAnim = GetDuplicate(Anim);
  54. if (dupAnim != null)
  55. {
  56. if (ReplaceIfExists == true)
  57. {
  58. dupAnim.Status = Animation.AnimationStatus.Completed;
  59. }
  60. else
  61. {
  62. return;
  63. }
  64. }
  65. AnimationList.Add(Anim);
  66. }
  67. private static Animation GetDuplicate(Animation Anim)
  68. {
  69. try
  70. {
  71. return AnimationList.Find(a => a.ID == Anim.ID);
  72. }
  73. catch { return null; }
  74. }
  75. }
  76. }