feat: use animator instead of transition.

This commit is contained in:
rabbitism
2024-02-17 15:07:26 +08:00
parent 46cd9c8dac
commit 35d0d5a7b1

View File

@@ -3,6 +3,7 @@ using Avalonia.Animation;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Styling;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -11,6 +12,7 @@ public abstract class NumberDisplayerBase : TemplatedControl
public static readonly DirectProperty<NumberDisplayerBase, string> InternalTextProperty = AvaloniaProperty.RegisterDirect<NumberDisplayerBase, string>( public static readonly DirectProperty<NumberDisplayerBase, string> InternalTextProperty = AvaloniaProperty.RegisterDirect<NumberDisplayerBase, string>(
nameof(InternalText), o => o.InternalText, (o, v) => o.InternalText = v); nameof(InternalText), o => o.InternalText, (o, v) => o.InternalText = v);
private string _internalText; private string _internalText;
public string InternalText public string InternalText
{ {
get => _internalText; get => _internalText;
@@ -38,6 +40,9 @@ public abstract class NumberDisplayerBase : TemplatedControl
public abstract class NumberDisplayer<T>: NumberDisplayerBase public abstract class NumberDisplayer<T>: NumberDisplayerBase
{ {
private Animation? _animation;
private CancellationTokenSource _cts = new ();
public static readonly StyledProperty<T?> ValueProperty = AvaloniaProperty.Register<NumberDisplayer<T>, T?>( public static readonly StyledProperty<T?> ValueProperty = AvaloniaProperty.Register<NumberDisplayer<T>, T?>(
nameof(Value), defaultBindingMode:BindingMode.TwoWay); nameof(Value), defaultBindingMode:BindingMode.TwoWay);
@@ -60,7 +65,7 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
{ {
ValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) => ValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) =>
{ {
item.InternalValue = args.NewValue.Value; item.OnValueChanged(args.OldValue.Value, args.NewValue.Value);
}); });
InternalValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) => InternalValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) =>
{ {
@@ -68,29 +73,62 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
}); });
DurationProperty.Changed.AddClassHandler<NumberDisplayer<T>, TimeSpan>((item, args) =>item.OnDurationChanged(args)); DurationProperty.Changed.AddClassHandler<NumberDisplayer<T>, TimeSpan>((item, args) =>item.OnDurationChanged(args));
} }
private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args) protected override void OnInitialized()
{ {
this.Transitions ??= new Transitions(); base.OnInitialized();
this.Transitions?.Clear(); _animation = new Animation();
this.Transitions?.Add(GetTransition(args.NewValue.Value)); _animation.Duration = Duration;
_animation.FillMode = FillMode.Forward;
_animation.Children.Add(new KeyFrame()
{
Cue = new Cue(0.0),
Setters = { new Setter{Property = InternalValueProperty } }
});
_animation.Children.Add(new KeyFrame()
{
Cue = new Cue(1.0),
Setters = { new Setter{Property = InternalValueProperty } }
});
Animation.SetAnimator(_animation.Children[0].Setters[0], GetAnimator());
} }
protected abstract ITransition GetTransition(TimeSpan duration); private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args)
{
if (_animation is null) return;
_animation.Duration = args.NewValue.Value;
}
protected virtual void OnValueChanged(T? oldValue, T? newValue)
{
_cts.Cancel();
_cts = new CancellationTokenSource();
(_animation?.Children[0].Setters[0] as Setter)!.Value = oldValue;
(_animation?.Children[1].Setters[0] as Setter)!.Value = newValue;
_animation?.RunAsync(this, _cts.Token);
}
protected abstract InterpolatingAnimator<T> GetAnimator();
protected abstract string GetString(T? value); protected abstract string GetString(T? value);
} }
public class Int32Displayer : NumberDisplayer<int> public class Int32Displayer : NumberDisplayer<int>
{ {
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase); protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
protected override ITransition GetTransition(TimeSpan duration) protected override InterpolatingAnimator<int> GetAnimator()
{ {
return new IntegerTransition() return new IntAnimator();
}
private class IntAnimator : InterpolatingAnimator<int>
{
public override int Interpolate(double progress, int oldValue, int newValue)
{ {
Property = InternalValueProperty, return oldValue + (int)((newValue - oldValue) * progress);
Duration = duration }
};
} }
protected override string GetString(int value) protected override string GetString(int value)
@@ -103,13 +141,17 @@ public class DoubleDisplayer : NumberDisplayer<double>
{ {
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase); protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
protected override ITransition GetTransition(TimeSpan duration) protected override InterpolatingAnimator<double> GetAnimator()
{ {
return new DoubleTransition() return new DoubleAnimator();
}
private class DoubleAnimator : InterpolatingAnimator<double>
{
public override double Interpolate(double progress, double oldValue, double newValue)
{ {
Property = InternalValueProperty, return oldValue + (newValue - oldValue) * progress;
Duration = duration }
};
} }
protected override string GetString(double value) protected override string GetString(double value)