Animator.cs 2.2 KB

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