コード例 #1
0
 public void CancelAnimation()
 {
     lock (_locker)
     {
         if (State != EAnimatorState.Running)
         {
             throw new InvalidOperationException($"Cannot cancel animation because it {State}");
         }
         State = EAnimatorState.Suspended;
         _cancellationTokenSource.Cancel();
     }
 }
コード例 #2
0
 public CPointAnimator(T animatable, CPoint from, CPoint to, TimeSpan totalDuration)
 {
     if (totalDuration <= TimeSpan.Zero)
     {
         throw new ArgumentException("Total duration value should be more than zero");
     }
     Animatable    = animatable;
     From          = from;
     To            = to;
     TotalDuration = totalDuration;
     State         = EAnimatorState.Unstarted;
 }
コード例 #3
0
 public void BeginAnimation()
 {
     lock (_locker)
     {
         if (State != EAnimatorState.Unstarted && State != EAnimatorState.Suspended)
         {
             throw new InvalidOperationException($"Cannot begin animation because it {State}");
         }
         _startTime = DateTime.UtcNow;
         _vector    = new CVector(From, To);
         _cancellationTokenSource = new CancellationTokenSource();
         CancellationToken token = _cancellationTokenSource.Token;
         _currentTask = new Task(() => DoAnimation(token), token);
         _currentTask.Start();
         State = EAnimatorState.Running;
     }
 }
コード例 #4
0
        private void DoAnimation(CancellationToken token)
        {
            TimeSpan durationFromStart;

            while ((durationFromStart = (DateTime.UtcNow - _startTime).Duration()) < TotalDuration)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                Double completedPart = durationFromStart.TotalMilliseconds / TotalDuration.TotalMilliseconds;
                Animatable.SetAnimationValue(From.MovePoint(completedPart * _vector));
                token.WaitHandle.WaitOne(5);
            }
            Animatable.SetAnimationValue(To);
            if (!token.IsCancellationRequested)
            {
                new Task(() => AnimationCompleted?.Invoke()).Start();
            }
            State = EAnimatorState.Stopped;
        }