feat: use animator instead of transition.
This commit is contained in:
@@ -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) =>
|
||||||
{
|
{
|
||||||
@@ -69,14 +74,42 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,13 +117,18 @@ 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>
|
||||||
{
|
{
|
||||||
Property = InternalValueProperty,
|
public override int Interpolate(double progress, int oldValue, int newValue)
|
||||||
Duration = duration
|
{
|
||||||
};
|
return oldValue + (int)((newValue - oldValue) * progress);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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>
|
||||||
{
|
{
|
||||||
Property = InternalValueProperty,
|
public override double Interpolate(double progress, double oldValue, double newValue)
|
||||||
Duration = duration
|
{
|
||||||
};
|
return oldValue + (newValue - oldValue) * progress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override string GetString(double value)
|
protected override string GetString(double value)
|
||||||
|
|||||||
Reference in New Issue
Block a user