12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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 =>
- {
- AnimationList[index].UpdateFrame();
- });
- 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
- {
- return AnimationList.Find(a => a.ID == Anim.ID);
- }
- catch { return null; }
- }
- }
- }
|