From 8b17870362edd929a901d7ef00e26b05b890b1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20Komosi=C5=84ski?= Date: Wed, 9 Jun 2021 17:18:55 +0200 Subject: [PATCH] Merge pull request #6029 from MarchingCube/brush-transition Transitions for solid color brushes. --- samples/RenderDemo/Pages/TransitionsPage.xaml | 36 +++++++++++ .../Animation/Transitions/BrushTransition.cs | 59 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/Avalonia.Visuals/Animation/Transitions/BrushTransition.cs diff --git a/samples/RenderDemo/Pages/TransitionsPage.xaml b/samples/RenderDemo/Pages/TransitionsPage.xaml index f9f69fb341..1985074b0f 100644 --- a/samples/RenderDemo/Pages/TransitionsPage.xaml +++ b/samples/RenderDemo/Pages/TransitionsPage.xaml @@ -141,6 +141,39 @@ + + + + + + + + @@ -166,6 +199,9 @@ + + + diff --git a/src/Avalonia.Visuals/Animation/Transitions/BrushTransition.cs b/src/Avalonia.Visuals/Animation/Transitions/BrushTransition.cs new file mode 100644 index 0000000000..cc5af1b4b1 --- /dev/null +++ b/src/Avalonia.Visuals/Animation/Transitions/BrushTransition.cs @@ -0,0 +1,59 @@ +using System; +using Avalonia.Animation.Animators; +using Avalonia.Animation.Easings; +using Avalonia.Media; + +#nullable enable + +namespace Avalonia.Animation +{ + /// + /// Transition class that handles with type. + /// Only values of will transition correctly at the moment. + /// + public class BrushTransition : Transition + { + private static readonly ISolidColorBrushAnimator s_animator = new ISolidColorBrushAnimator(); + + public override IObservable DoTransition(IObservable progress, IBrush? oldValue, IBrush? newValue) + { + var oldSolidColorBrush = TryGetSolidColorBrush(oldValue); + var newSolidColorBrush = TryGetSolidColorBrush(newValue); + + if (oldSolidColorBrush != null && newSolidColorBrush != null) + { + return new AnimatorTransitionObservable( + s_animator, progress, Easing, oldSolidColorBrush, newSolidColorBrush); + } + + return new IncompatibleTransitionObservable(progress, Easing, oldValue, newValue); + } + + private static ISolidColorBrush? TryGetSolidColorBrush(IBrush? brush) + { + if (brush is null) + { + return Brushes.Transparent; + } + + return brush as ISolidColorBrush; + } + + private class IncompatibleTransitionObservable : TransitionObservableBase + { + private readonly IBrush? _from; + private readonly IBrush? _to; + + public IncompatibleTransitionObservable(IObservable progress, Easing easing, IBrush? from, IBrush? to) : base(progress, easing) + { + _from = from; + _to = to; + } + + protected override IBrush? ProduceValue(double progress) + { + return progress < 0.5 ? _from : _to; + } + } + } +}