public void CancelAnimation() { lock (_locker) { if (State != EAnimatorState.Running) { throw new InvalidOperationException($"Cannot cancel animation because it {State}"); } State = EAnimatorState.Suspended; _cancellationTokenSource.Cancel(); } }
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; }
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; } }
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; }