Animator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. try
  41. {
  42. AnimationList[index].UpdateFrame();
  43. }
  44. catch
  45. {
  46. }
  47. });
  48. Thread.Sleep((int)Interval);
  49. }
  50. }
  51. public static void Request(Animation Anim, bool ReplaceIfExists = true)
  52. {
  53. if (AnimatorThread == null || IsWork == false)
  54. {
  55. Start();
  56. }
  57. Debug.WriteLine("Запуск анимации: " + Anim.ID + "| TargetValue: " + Anim.TargetValue);
  58. Anim.Status = Animation.AnimationStatus.Requested;
  59. Animation dupAnim = GetDuplicate(Anim);
  60. if (dupAnim != null)
  61. {
  62. if (ReplaceIfExists == true)
  63. {
  64. dupAnim.Status = Animation.AnimationStatus.Completed;
  65. }
  66. else
  67. {
  68. return;
  69. }
  70. }
  71. AnimationList.Add(Anim);
  72. }
  73. private static Animation GetDuplicate(Animation Anim)
  74. {
  75. try
  76. {
  77. //if (Anim.ID != null)
  78. return AnimationList.Find(a => a.ID == Anim.ID);
  79. }
  80. catch { return null; }
  81. }
  82. }
  83. }