diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index d06f1d2dbc..ae6deb585c 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -175,18 +175,18 @@ namespace Avalonia.Animation } /// - /// Obselete: Do not use this property, use instead. + /// Obsolete: Do not use this property, use instead. /// /// - [Obsolete] + [Obsolete("This property has been superceded by IterationCount.")] public string RepeatCount { get { return IterationCount.ToString(); } set { var val = value.ToUpper(); - val = val .Replace("LOOP", "INFINITE"); - val = val .Replace("NONE", "1"); + val = val.Replace("LOOP", "INFINITE"); + val = val.Replace("NONE", "1"); IterationCount = IterationCount.Parse(val); } } diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index f660fd1fa3..bc7e106a2a 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -20,7 +20,7 @@ namespace Avalonia.Animation private ulong _currentIteration; private bool _gotFirstKFValue; private FillMode _fillMode; - private PlaybackDirection _animationDirection; + private PlaybackDirection _playbackDirection; private Animator _animator; private Animation _animation; private Animatable _targetControl; @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (_animation.SpeedRatio < 0d) throw new ArgumentOutOfRangeException("SpeedRatio value should not be negative."); - + if (_animation.Duration.TotalSeconds <= 0) throw new InvalidOperationException("Duration value cannot be negative or zero."); @@ -70,7 +70,7 @@ namespace Avalonia.Animation else _iterationCount = null; - _animationDirection = _animation.PlaybackDirection; + _playbackDirection = _animation.PlaybackDirection; _fillMode = _animation.FillMode; } @@ -169,13 +169,28 @@ namespace Avalonia.Animation // Normalize time for interpolation. var normalizedTime = playbackTime / iterDuration; - // Check if normalized time needs to be reversed. - bool isCurIterReverse = _animationDirection == PlaybackDirection.Normal ? false : - _animationDirection == PlaybackDirection.Alternate ? (_currentIteration % 2 == 0) ? false : true : - _animationDirection == PlaybackDirection.AlternateReverse ? (_currentIteration % 2 == 0) ? true : false : - _animationDirection == PlaybackDirection.Reverse ? true : false; + // Check if normalized time needs to be reversed according to PlaybackDirection - if (isCurIterReverse) + bool playbackReversed; + switch (_playbackDirection) + { + case PlaybackDirection.Normal: + playbackReversed = false; + break; + case PlaybackDirection.Reverse: + playbackReversed = true; + break; + case PlaybackDirection.Alternate: + playbackReversed = (_currentIteration % 2 == 0) ? false : true; + break; + case PlaybackDirection.AlternateReverse: + playbackReversed = (_currentIteration % 2 == 0) ? true : false; + break; + default: + throw new InvalidOperationException($"Animation direction value is unknown: {_playbackDirection}"); + } + + if (playbackReversed) normalizedTime = 1 - normalizedTime; // Ease and interpolate @@ -197,4 +212,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Animators/ByteAnimator.cs b/src/Avalonia.Animation/Animators/ByteAnimator.cs index 952df9de36..0fb8f7fdc1 100644 --- a/src/Avalonia.Animation/Animators/ByteAnimator.cs +++ b/src/Avalonia.Animation/Animators/ByteAnimator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class ByteAnimator : Animator { - static double maxVal = (double)byte.MaxValue; + const double maxVal = (double)byte.MaxValue; /// public override byte Interpolate(double progress, byte oldValue, byte newValue) diff --git a/src/Avalonia.Animation/Animators/FloatAnimator.cs b/src/Avalonia.Animation/Animators/FloatAnimator.cs index 140b453ddf..0fd3bf8729 100644 --- a/src/Avalonia.Animation/Animators/FloatAnimator.cs +++ b/src/Avalonia.Animation/Animators/FloatAnimator.cs @@ -11,7 +11,7 @@ namespace Avalonia.Animation.Animators /// public override float Interpolate(double progress, float oldValue, float newValue) { - return ((newValue - oldValue) * (float)progress) + oldValue; + return (float)(((newValue - oldValue) * progress) + oldValue); } } } diff --git a/src/Avalonia.Animation/Animators/Int16Animator.cs b/src/Avalonia.Animation/Animators/Int16Animator.cs index 60b324552b..d7e7da5d38 100644 --- a/src/Avalonia.Animation/Animators/Int16Animator.cs +++ b/src/Avalonia.Animation/Animators/Int16Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class Int16Animator : Animator { - static double maxVal = (double)Int16.MaxValue; + const double maxVal = (double)Int16.MaxValue; /// public override Int16 Interpolate(double progress, Int16 oldValue, Int16 newValue) diff --git a/src/Avalonia.Animation/Animators/Int32Animator.cs b/src/Avalonia.Animation/Animators/Int32Animator.cs index 21520e2fe3..792b810652 100644 --- a/src/Avalonia.Animation/Animators/Int32Animator.cs +++ b/src/Avalonia.Animation/Animators/Int32Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class Int32Animator : Animator { - static double maxVal = (double)Int32.MaxValue; + const double maxVal = (double)Int32.MaxValue; /// public override Int32 Interpolate(double progress, Int32 oldValue, Int32 newValue) diff --git a/src/Avalonia.Animation/Animators/Int64Animator.cs b/src/Avalonia.Animation/Animators/Int64Animator.cs index b6c495796d..ca5817924e 100644 --- a/src/Avalonia.Animation/Animators/Int64Animator.cs +++ b/src/Avalonia.Animation/Animators/Int64Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class Int64Animator : Animator { - static double maxVal = (double)Int64.MaxValue; + const double maxVal = (double)Int64.MaxValue; /// public override Int64 Interpolate(double progress, Int64 oldValue, Int64 newValue) diff --git a/src/Avalonia.Animation/Animators/UInt16Animator.cs b/src/Avalonia.Animation/Animators/UInt16Animator.cs index 486fd7218f..4b5463dade 100644 --- a/src/Avalonia.Animation/Animators/UInt16Animator.cs +++ b/src/Avalonia.Animation/Animators/UInt16Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class UInt16Animator : Animator { - static double maxVal = (double)UInt16.MaxValue; + const double maxVal = (double)UInt16.MaxValue; /// public override UInt16 Interpolate(double progress, UInt16 oldValue, UInt16 newValue) diff --git a/src/Avalonia.Animation/Animators/UInt32Animator.cs b/src/Avalonia.Animation/Animators/UInt32Animator.cs index 19df9e5929..c1f09e3518 100644 --- a/src/Avalonia.Animation/Animators/UInt32Animator.cs +++ b/src/Avalonia.Animation/Animators/UInt32Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class UInt32Animator : Animator { - static double maxVal = (double)UInt32.MaxValue; + const double maxVal = (double)UInt32.MaxValue; /// public override UInt32 Interpolate(double progress, UInt32 oldValue, UInt32 newValue) diff --git a/src/Avalonia.Animation/Animators/UInt64Animator.cs b/src/Avalonia.Animation/Animators/UInt64Animator.cs index 5303e164c9..0fd9fcb30a 100644 --- a/src/Avalonia.Animation/Animators/UInt64Animator.cs +++ b/src/Avalonia.Animation/Animators/UInt64Animator.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation.Animators /// public class UInt64Animator : Animator { - static double maxVal = (double)UInt64.MaxValue; + const double maxVal = (double)UInt64.MaxValue; /// public override UInt64 Interpolate(double progress, UInt64 oldValue, UInt64 newValue) diff --git a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs index ec02772dbc..8776d3a7b7 100644 --- a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs +++ b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs @@ -11,18 +11,18 @@ namespace Avalonia.Animation.Animators /// public class SolidColorBrushAnimator : Animator { - ColorAnimator colorAnimator; + ColorAnimator _colorAnimator; void InitializeColorAnimator() { - colorAnimator = new ColorAnimator(); + _colorAnimator = new ColorAnimator(); foreach (AnimatorKeyFrame keyframe in this) { - colorAnimator.Add(keyframe); + _colorAnimator.Add(keyframe); } - colorAnimator.Property = SolidColorBrush.ColorProperty; + _colorAnimator.Property = SolidColorBrush.ColorProperty; } public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete) @@ -48,7 +48,7 @@ namespace Avalonia.Animation.Animators // Continue if target prop is not empty & is a SolidColorBrush derivative. if (typeof(ISolidColorBrush).IsAssignableFrom(targetVal.GetType())) { - if (colorAnimator == null) + if (_colorAnimator == null) InitializeColorAnimator(); SolidColorBrush finalTarget; @@ -63,7 +63,7 @@ namespace Avalonia.Animation.Animators finalTarget = targetVal as SolidColorBrush; - return colorAnimator.Apply(animation, finalTarget, clock ?? control.Clock, match, onComplete); + return _colorAnimator.Apply(animation, finalTarget, clock ?? control.Clock, match, onComplete); } return Disposable.Empty; diff --git a/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs index 144c1dda88..e7be272f13 100644 --- a/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs @@ -9,7 +9,7 @@ namespace Avalonia.Animation.Animators /// public class TransformAnimator : Animator { - DoubleAnimator childAnimator; + DoubleAnimator _doubleAnimator; /// public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete) @@ -27,7 +27,7 @@ namespace Avalonia.Animation.Animators // default RenderTransform order. normalTransform.Children.Add(new ScaleTransform()); - normalTransform.Children.Add(new SkewTransform()); + normalTransform.Children.Add(new SkewTransform()); normalTransform.Children.Add(new RotateTransform()); normalTransform.Children.Add(new TranslateTransform()); @@ -36,15 +36,22 @@ namespace Avalonia.Animation.Animators var renderTransformType = ctrl.RenderTransform.GetType(); - if (childAnimator == null) + if (_doubleAnimator == null) { - InitializeChildAnimator(); + _doubleAnimator = new DoubleAnimator(); + + foreach (AnimatorKeyFrame keyframe in this) + { + _doubleAnimator.Add(keyframe); + } + + _doubleAnimator.Property = Property; } // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childAnimator.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); + return _doubleAnimator.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +60,7 @@ namespace Avalonia.Animation.Animators { if (transform.GetType() == Property.OwnerType) { - return childAnimator.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); + return _doubleAnimator.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); } } } @@ -75,17 +82,5 @@ namespace Avalonia.Animation.Animators /// public override double Interpolate(double p, double o, double n) => 0; - - void InitializeChildAnimator() - { - childAnimator = new DoubleAnimator(); - - foreach (AnimatorKeyFrame keyframe in this) - { - childAnimator.Add(keyframe); - } - - childAnimator.Property = Property; - } } -} \ No newline at end of file +} diff --git a/tests/Avalonia.Animation.UnitTests/AnimationIterationTests.cs b/tests/Avalonia.Animation.UnitTests/AnimationIterationTests.cs index 7e0743a56f..d89b8469df 100644 --- a/tests/Avalonia.Animation.UnitTests/AnimationIterationTests.cs +++ b/tests/Avalonia.Animation.UnitTests/AnimationIterationTests.cs @@ -18,15 +18,17 @@ namespace Avalonia.Animation.UnitTests { var keyframe1 = new KeyFrame() { - Setters = { + Setters = + { new Setter(Border.WidthProperty, 200d), }, Cue = new Cue(1d) }; - + var keyframe2 = new KeyFrame() { - Setters = { + Setters = + { new Setter(Border.WidthProperty, 100d), }, Cue = new Cue(0d) @@ -72,4 +74,4 @@ namespace Avalonia.Animation.UnitTests Assert.Equal(border.Width, 100d); } } -} \ No newline at end of file +}