From fe66f3837e6ce8d6e9e65ee496fc5252a282cba8 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 22 Mar 2018 17:47:47 +0800 Subject: [PATCH] *Added numerical transition classes. *Disabled old animations code in CrossFade, PageSlide, CarouselPresenter and ProgressBar. --- .../Transitions/DoubleTransition.cs | 11 ++---- .../Transitions/FloatTransition.cs | 23 +++++++++++ .../Transitions/ITransition.cs | 26 +++++++++++++ .../Transitions/IntegerTransition.cs | 23 +++++++++++ .../Transitions/Transition.cs | 25 +++--------- .../Presenters/CarouselPresenter.cs | 2 +- src/Avalonia.Controls/ProgressBar.cs | 38 +++++++++---------- src/Avalonia.Visuals/Animation/CrossFade.cs | 3 ++ src/Avalonia.Visuals/Animation/PageSlide.cs | 3 ++ 9 files changed, 108 insertions(+), 46 deletions(-) create mode 100644 src/Avalonia.Animation/Transitions/FloatTransition.cs create mode 100644 src/Avalonia.Animation/Transitions/ITransition.cs create mode 100644 src/Avalonia.Animation/Transitions/IntegerTransition.cs diff --git a/src/Avalonia.Animation/Transitions/DoubleTransition.cs b/src/Avalonia.Animation/Transitions/DoubleTransition.cs index a4cb255545..e7ae53ef81 100644 --- a/src/Avalonia.Animation/Transitions/DoubleTransition.cs +++ b/src/Avalonia.Animation/Transitions/DoubleTransition.cs @@ -8,19 +8,16 @@ using System.Reactive.Linq; namespace Avalonia.Animation { /// - /// Transitions object that handles properties with types. + /// Transition class that handles with types. /// public class DoubleTransition : Transition { /// - public override void DoInterpolation(Animatable control, IObservable progress, double oldValue, double newValue) + public override IObservable DoInterpolation(IObservable progress, double oldValue, double newValue) { var delta = newValue - oldValue; - var transition = progress.Select(p => - { - return Easing.Ease(p) * delta + oldValue; - }); - control.Bind(Property, transition.Select(p=>(object)p), Data.BindingPriority.Animation); + return progress + .Select(p => Easing.Ease(p) * delta + oldValue); } } } diff --git a/src/Avalonia.Animation/Transitions/FloatTransition.cs b/src/Avalonia.Animation/Transitions/FloatTransition.cs new file mode 100644 index 0000000000..c1d2fb0b0b --- /dev/null +++ b/src/Avalonia.Animation/Transitions/FloatTransition.cs @@ -0,0 +1,23 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Metadata; +using System; +using System.Reactive.Linq; + +namespace Avalonia.Animation +{ + /// + /// Transition class that handles with types. + /// + public class FloatTransition : Transition + { + /// + public override IObservable DoInterpolation(IObservable progress, float oldValue, float newValue) + { + var delta = newValue - oldValue; + return progress + .Select(p => (float)Easing.Ease(p) * delta + oldValue); + } + } +} diff --git a/src/Avalonia.Animation/Transitions/ITransition.cs b/src/Avalonia.Animation/Transitions/ITransition.cs new file mode 100644 index 0000000000..124a770c8d --- /dev/null +++ b/src/Avalonia.Animation/Transitions/ITransition.cs @@ -0,0 +1,26 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Metadata; +using System; +using System.Reactive.Linq; + +namespace Avalonia.Animation +{ + /// + /// Interface for objects. + /// + public interface ITransition + { + /// + /// Applies the transition to the specified . + /// + IDisposable Apply(Animatable control, object oldValue, object newValue); + + /// + /// Gets the property to be animated. + /// + AvaloniaProperty Property { get; set; } + + } +} diff --git a/src/Avalonia.Animation/Transitions/IntegerTransition.cs b/src/Avalonia.Animation/Transitions/IntegerTransition.cs new file mode 100644 index 0000000000..cb56956a80 --- /dev/null +++ b/src/Avalonia.Animation/Transitions/IntegerTransition.cs @@ -0,0 +1,23 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Metadata; +using System; +using System.Reactive.Linq; + +namespace Avalonia.Animation +{ + /// + /// Transition class that handles with types. + /// + public class IntegerTransition : Transition + { + /// + public override IObservable DoInterpolation(IObservable progress, int oldValue, int newValue) + { + var delta = newValue - oldValue; + return progress + .Select(p => (int)(Easing.Ease(p) * delta + oldValue)); + } + } +} diff --git a/src/Avalonia.Animation/Transitions/Transition.cs b/src/Avalonia.Animation/Transitions/Transition.cs index 4232a1214f..728908c234 100644 --- a/src/Avalonia.Animation/Transitions/Transition.cs +++ b/src/Avalonia.Animation/Transitions/Transition.cs @@ -3,24 +3,10 @@ using Avalonia.Metadata; using System; +using System.Reactive.Linq; namespace Avalonia.Animation { - public interface ITransition - { - /// - /// Applies the transition to the specified . - /// - void Apply(Animatable control, object oldValue, object newValue); - - /// - /// Gets the property to be animated. - /// - AvaloniaProperty Property { get; set; } - - - } - /// /// Defines how a property should be animated using a transition. /// @@ -49,7 +35,7 @@ namespace Avalonia.Animation { if (!(typeof(T) == value.PropertyType)) throw new InvalidCastException - ($"Invalid property type {typeof(T).Name} for this {GetType().Name}"); + ($"Invalid property type \"{typeof(T).Name}\" for this {GetType().Name} transition."); _prop = value; } @@ -58,12 +44,13 @@ namespace Avalonia.Animation /// /// Apply interpolation to the property. /// - public abstract void DoInterpolation(Animatable control, IObservable progress, T oldValue, T newValue); + public abstract IObservable DoInterpolation(IObservable progress, T oldValue, T newValue); /// - public void Apply(Animatable control, object oldValue, object newValue) + public IDisposable Apply(Animatable control, object oldValue, object newValue) { - DoInterpolation(control, Timing.GetTimer(Duration), (T)oldValue, (T)newValue); + var transition = DoInterpolation(Timing.GetTimer(Duration), (T)oldValue, (T)newValue).Select(p => (object)p); + return control.Bind(Property, transition, Data.BindingPriority.Animation); } } diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index 5156a11b30..c6ef5a14e9 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -39,7 +39,7 @@ namespace Avalonia.Controls.Presenters Carousel.PageTransitionProperty.AddOwner(); private int _selectedIndex = -1; - private Task _current; + // private Task _current; private Task _currentTransition; private int _queuedTransitionIndex = -1; diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index c195df6d26..e41d86636a 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -105,8 +105,8 @@ namespace Avalonia.Controls private class IndeterminateAnimation : IDisposable { private WeakReference _progressBar; - private IDisposable _indeterminateBindSubscription; - private TimeSpan _startTime; + //private IDisposable _indeterminateBindSubscription; + //private TimeSpan _startTime; private bool _disposed; public bool Disposed => _disposed; @@ -129,31 +129,31 @@ namespace Avalonia.Controls private Rect GetAnimationRect(TimeSpan time) { - if (_progressBar.TryGetTarget(out var progressBar)) - { - if (progressBar.Orientation == Orientation.Horizontal) - return new Rect(-progressBar._indicator.Width - 5 + (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Width + progressBar._indicator.Width + 10), 0, progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height); - else - return new Rect(0, progressBar.Bounds.Height + 5 - (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Height + progressBar._indicator.Height + 10), progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height); - } - else - { - _indeterminateBindSubscription.Dispose(); - return Rect.Empty; - } + //if (_progressBar.TryGetTarget(out var progressBar)) + //{ + // if (progressBar.Orientation == Orientation.Horizontal) + // return new Rect(-progressBar._indicator.Width - 5 + (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Width + progressBar._indicator.Width + 10), 0, progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height); + // else + // return new Rect(0, progressBar.Bounds.Height + 5 - (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Height + progressBar._indicator.Height + 10), progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height); + //} + //else + //{ + // _indeterminateBindSubscription.Dispose(); + return Rect.Empty; + //} } private void AnimationTick(Rect rect) { - if (_progressBar.TryGetTarget(out var progressBar)) - progressBar._indicator.Arrange(rect); - else - _indeterminateBindSubscription.Dispose(); + //if (_progressBar.TryGetTarget(out var progressBar)) + // progressBar._indicator.Arrange(rect); + //else + // _indeterminateBindSubscription.Dispose(); } public void Dispose() { - _indeterminateBindSubscription?.Dispose(); + //_indeterminateBindSubscription?.Dispose(); _disposed = true; } } diff --git a/src/Avalonia.Visuals/Animation/CrossFade.cs b/src/Avalonia.Visuals/Animation/CrossFade.cs index c35011e5a0..bf532c62c3 100644 --- a/src/Avalonia.Visuals/Animation/CrossFade.cs +++ b/src/Avalonia.Visuals/Animation/CrossFade.cs @@ -83,6 +83,9 @@ namespace Avalonia.Animation //await Task.WhenAll(tasks.ToArray()); + // FIXME: This is temporary until animations are fixed. + await Task.Delay(1); + if (from != null) { from.IsVisible = false; diff --git a/src/Avalonia.Visuals/Animation/PageSlide.cs b/src/Avalonia.Visuals/Animation/PageSlide.cs index 483bb2b44d..2eb0426bc5 100644 --- a/src/Avalonia.Visuals/Animation/PageSlide.cs +++ b/src/Avalonia.Visuals/Animation/PageSlide.cs @@ -103,6 +103,9 @@ namespace Avalonia.Animation //await Task.WhenAll(tasks.ToArray()); + // FIXME: This is temporary until animations are fixed. + await Task.Delay(1); + if (from != null) { from.IsVisible = false;