From 2a455edbc58577768ac8256fe64c7e934c9de2df Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 18 Jul 2018 15:52:43 -0500 Subject: [PATCH] Clean up some of the animation code. --- src/Avalonia.Animation/Animation.cs | 17 +++++++------- src/Avalonia.Animation/Animator`1.cs | 35 +++++++++++----------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index aa436f5f4e..d15a9b78bb 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -82,7 +82,7 @@ namespace Avalonia.Animation private void InterpretKeyframes() { - var handlerList = new List<(Type, AvaloniaProperty)>(); + var handlerList = new List<(Type type, AvaloniaProperty property)>(); var kfList = new List(); foreach (var keyframe in this) @@ -116,18 +116,17 @@ namespace Avalonia.Animation var newAnimatorInstances = new List<(Type handler, AvaloniaProperty prop, IAnimator inst)>(); - foreach (var handler in handlerList) + foreach (var (handlerType, property) in handlerList) { - var newInstance = (IAnimator)Activator.CreateInstance(handler.Item1); - newInstance.Property = handler.Item2; - newAnimatorInstances.Add((handler.Item1, handler.Item2, newInstance)); + var newInstance = (IAnimator)Activator.CreateInstance(handlerType); + newInstance.Property = property; + newAnimatorInstances.Add((handlerType, property, newInstance)); } foreach (var kf in kfList) { - var parent = newAnimatorInstances.Where(p => p.handler == kf.Handler && - p.prop == kf.Property) - .First(); + var parent = newAnimatorInstances.First(p => p.handler == kf.Handler && + p.prop == kf.Property); parent.inst.Add(kf); } @@ -163,4 +162,4 @@ namespace Avalonia.Animation return this; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 6d4ae7d8e2..2b9f0645b7 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -19,7 +19,7 @@ namespace Avalonia.Animation /// /// List of type-converted keyframes. /// - private Dictionary _convertedKeyframes = new Dictionary(); + private readonly IDictionary _convertedKeyframes = new SortedDictionary(); private bool _isVerfifiedAndConverted; @@ -38,10 +38,9 @@ namespace Avalonia.Animation public virtual IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch) { if (!_isVerfifiedAndConverted) - VerifyConvertKeyFrames(animation, typeof(T)); + VerifyConvertKeyFrames(animation); return obsMatch - .Where(p => p == true) // Ignore triggers when global timers are paused. .Where(p => Timing.GetGlobalPlayState() != PlayState.Pause) .Subscribe(_ => @@ -61,7 +60,7 @@ namespace Avalonia.Animation protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) { KeyValuePair firstCue, lastCue; - int kvCount = _convertedKeyframes.Count(); + int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { if (DoubleUtils.AboutEqual(t, 0.0) || t < 0.0) @@ -76,8 +75,8 @@ namespace Avalonia.Animation } else { - firstCue = _convertedKeyframes.Where(j => j.Key <= t).Last(); - lastCue = _convertedKeyframes.Where(j => j.Key >= t).First(); + firstCue = _convertedKeyframes.Last(j => j.Key <= t); + lastCue = _convertedKeyframes.First(j => j.Key >= t); } } else @@ -103,10 +102,7 @@ namespace Avalonia.Animation Timing.AnimationStateTimer .TakeWhile(_ => !_kfStateMach._unsubscribe) - .Subscribe(p => - { - _kfStateMach.Step(p, DoInterpolation); - }); + .Subscribe(p => _kfStateMach.Step(p, DoInterpolation)); return control.Bind(Property, _kfStateMach, BindingPriority.Animation); } @@ -119,9 +115,9 @@ namespace Avalonia.Animation /// /// Verifies and converts keyframe values according to this class's target type. /// - private void VerifyConvertKeyFrames(Animation animation, Type type) + private void VerifyConvertKeyFrames(Animation animation) { - var typeConv = TypeDescriptor.GetConverter(type); + var typeConv = TypeDescriptor.GetConverter(typeof(T)); foreach (AnimatorKeyFrame k in this) { @@ -129,12 +125,12 @@ namespace Avalonia.Animation { throw new ArgumentNullException($"KeyFrame value can't be null."); } - if (!typeConv.CanConvertTo(k.Value.GetType())) + if (!typeConv.CanConvertFrom(k.Value.GetType())) { throw new InvalidCastException($"KeyFrame value doesnt match property type."); } - T convertedValue = (T)typeConv.ConvertTo(k.Value, type); + T convertedValue = (T)typeConv.ConvertTo(k.Value, typeof(T)); Cue _normalizedCue = k.Cue; @@ -146,12 +142,12 @@ namespace Avalonia.Animation _convertedKeyframes.Add(_normalizedCue.CueValue, (convertedValue, false)); } - SortKeyFrameCues(_convertedKeyframes); + AddNeutralKeyFramesIfNeeded(); _isVerfifiedAndConverted = true; } - private void SortKeyFrameCues(Dictionary convertedValues) + private void AddNeutralKeyFramesIfNeeded() { bool hasStartKey, hasEndKey; hasStartKey = hasEndKey = false; @@ -171,12 +167,9 @@ namespace Avalonia.Animation if (!hasStartKey || !hasEndKey) AddNeutralKeyFrames(hasStartKey, hasEndKey, _convertedKeyframes); - - _convertedKeyframes = _convertedKeyframes.OrderBy(p => p.Key) - .ToDictionary((k) => k.Key, (v) => v.Value); } - private void AddNeutralKeyFrames(bool hasStartKey, bool hasEndKey, Dictionary convertedKeyframes) + private void AddNeutralKeyFrames(bool hasStartKey, bool hasEndKey, IDictionary convertedKeyframes) { if (!hasStartKey) { @@ -189,4 +182,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +}