123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ImpulseVision
- {
- public static class Animator
- {
- public static List<Animation> AnimationList = new List<Animation>();
- public static int Count()
- {
- return AnimationList.Count;
- }
- private static Thread AnimatorThread;
- private static double Interval;
- public static bool IsWork = false;
- public static void Start()
- {
- if (IsWork) return;
- IsWork = true;
- Interval = 14; // FPS ~66
- AnimatorThread = new Thread(AnimationInvoker)
- {
- IsBackground = true,
- Name = "UI Animation"
- };
- AnimatorThread.Start();
- }
- private static void AnimationInvoker()
- {
- while (IsWork)
- {
- AnimationList.RemoveAll(a => a == null || a.Status == Animation.AnimationStatus.Completed);
- Parallel.For(0, Count(), index =>
- {
- try
- {
- AnimationList[index].UpdateFrame();
- }
- catch
- {
-
- }
- });
- Thread.Sleep((int)Interval);
- }
- }
- public static void Request(Animation Anim, bool ReplaceIfExists = true)
- {
- if (AnimatorThread == null || IsWork == false)
- {
- Start();
- }
- Debug.WriteLine("Запуск анимации: " + Anim.ID + "| TargetValue: " + Anim.TargetValue);
- Anim.Status = Animation.AnimationStatus.Requested;
- Animation dupAnim = GetDuplicate(Anim);
- if (dupAnim != null)
- {
- if (ReplaceIfExists == true)
- {
- dupAnim.Status = Animation.AnimationStatus.Completed;
- }
- else
- {
- return;
- }
- }
- AnimationList.Add(Anim);
- }
- private static Animation GetDuplicate(Animation Anim)
- {
- try
- {
- //if (Anim.ID != null)
- return AnimationList.Find(a => a.ID == Anim.ID);
- }
- catch { return null; }
- }
- }
- }
|