Browse Source

Clean up cue handling.

Jeremy Koritzinsky 7 years ago
parent
commit
1a84fe9186

+ 18 - 13
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<AnimatorKeyFrame>();
+            var animatorKeyFrames = new List<AnimatorKeyFrame>();
 
             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<IAnimator>();
 
             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);
 
         }
 

+ 1 - 3
src/Avalonia.Animation/AnimatorKeyFrame.cs

@@ -13,10 +13,8 @@ namespace Avalonia.Animation
     /// </summary>
     public class AnimatorKeyFrame
     {
-        public Type Handler;
+        public Type AnimatorType;
         public Cue Cue;
-        public TimeSpan KeyTime;
-        internal KeyFrameTimingMode TimingMode;
         public AvaloniaProperty Property;
         public object Value;
     }

+ 8 - 15
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
         /// </summary>
         private IDisposable RunKeyFrames(Animation animation, Animatable control)
         {
-            var _kfStateMach = new AnimatorStateMachine<T>();
-            _kfStateMach.Initialize(animation, control, this);
+            var stateMachine = new AnimatorStateMachine<T>();
+            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);
         }
 
         /// <summary>
@@ -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();