From 1a84fe9186d7ffcaa69798e34e4623c5f05d0546 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 19 Jul 2018 16:41:24 -0500 Subject: [PATCH] Clean up cue handling. --- src/Avalonia.Animation/Animation.cs | 31 +++++++++++++--------- src/Avalonia.Animation/AnimatorKeyFrame.cs | 4 +-- src/Avalonia.Animation/Animator`1.cs | 23 ++++++---------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 3b4afa637a..685f160745 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -83,7 +83,7 @@ namespace Avalonia.Animation private void InterpretKeyframes() { var handlerList = new List<(Type type, AvaloniaProperty property)>(); - var kfList = new List(); + var animatorKeyFrames = new List(); foreach (var keyframe in this) { @@ -99,38 +99,43 @@ namespace Avalonia.Animation if (!handlerList.Contains((handler, setter.Property))) handlerList.Add((handler, setter.Property)); + var cue = keyframe.Cue; + + if (keyframe.TimingMode == KeyFrameTimingMode.TimeSpan) + { + cue = new Cue(keyframe.KeyTime.Ticks / Duration.Ticks); + } + var newKF = new AnimatorKeyFrame() { - Handler = handler, + AnimatorType = handler, Property = setter.Property, - Cue = keyframe.Cue, - KeyTime = keyframe.KeyTime, - TimingMode = keyframe.TimingMode, + Cue = cue, Value = setter.Value }; - kfList.Add(newKF); + animatorKeyFrames.Add(newKF); } } - var newAnimatorInstances = new List<(Type handler, AvaloniaProperty prop, IAnimator inst)>(); + var newAnimatorInstances = new List(); foreach (var (handlerType, property) in handlerList) { var newInstance = (IAnimator)Activator.CreateInstance(handlerType); newInstance.Property = property; - newAnimatorInstances.Add((handlerType, property, newInstance)); + newAnimatorInstances.Add(newInstance); } - foreach (var kf in kfList) + foreach (var keyframe in animatorKeyFrames) { - var parent = newAnimatorInstances.First(p => p.handler == kf.Handler && - p.prop == kf.Property); - parent.inst.Add(kf); + var animator = newAnimatorInstances.First(a => a.GetType() == keyframe.AnimatorType && + a.Property == keyframe.Property); + animator.Add(keyframe); } foreach(var instance in newAnimatorInstances) - _animators.Add(instance.inst); + _animators.Add(instance); } diff --git a/src/Avalonia.Animation/AnimatorKeyFrame.cs b/src/Avalonia.Animation/AnimatorKeyFrame.cs index 74061d76db..1b0c38e0fc 100644 --- a/src/Avalonia.Animation/AnimatorKeyFrame.cs +++ b/src/Avalonia.Animation/AnimatorKeyFrame.cs @@ -13,10 +13,8 @@ namespace Avalonia.Animation /// public class AnimatorKeyFrame { - public Type Handler; + public Type AnimatorType; public Cue Cue; - public TimeSpan KeyTime; - internal KeyFrameTimingMode TimingMode; public AvaloniaProperty Property; public object Value; } diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index dde69cbd65..327a2ebd62 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -42,7 +42,7 @@ namespace Avalonia.Animation return obsMatch // Ignore triggers when global timers are paused. - .Where(p => Timing.GetGlobalPlayState() != PlayState.Pause) + .Where(p => p && Timing.GetGlobalPlayState() != PlayState.Pause) .Subscribe(_ => { var timerObs = RunKeyFrames(animation, control); @@ -97,14 +97,14 @@ namespace Avalonia.Animation /// private IDisposable RunKeyFrames(Animation animation, Animatable control) { - var _kfStateMach = new AnimatorStateMachine(); - _kfStateMach.Initialize(animation, control, this); + var stateMachine = new AnimatorStateMachine(); + stateMachine.Initialize(animation, control, this); Timing.AnimationStateTimer - .TakeWhile(_ => !_kfStateMach._unsubscribe) - .Subscribe(p => _kfStateMach.Step(p, DoInterpolation)); + .TakeWhile(_ => !stateMachine._unsubscribe) + .Subscribe(p => stateMachine.Step(p, DoInterpolation)); - return control.Bind(Property, _kfStateMach, BindingPriority.Animation); + return control.Bind(Property, stateMachine, BindingPriority.Animation); } /// @@ -131,15 +131,8 @@ namespace Avalonia.Animation } T convertedValue = (T)typeConv.ConvertTo(k.Value, typeof(T)); - - Cue _normalizedCue = k.Cue; - - if (k.TimingMode == KeyFrameTimingMode.TimeSpan) - { - _normalizedCue = new Cue(k.KeyTime.Ticks / animation.Duration.Ticks); - } - - _convertedKeyframes.Add(_normalizedCue.CueValue, (convertedValue, false)); + + _convertedKeyframes.Add(k.Cue.CueValue, (convertedValue, false)); } AddNeutralKeyFramesIfNeeded();