committed by
GitHub
29 changed files with 479 additions and 35 deletions
@ -0,0 +1,25 @@ |
|||||
|
<UserControl |
||||
|
xmlns="https://github.com/avaloniaui" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
|
xmlns:pages="clr-namespace:RenderDemo.Pages" |
||||
|
x:Class="RenderDemo.Pages.CustomAnimatorPage" |
||||
|
MaxWidth="600"> |
||||
|
<Grid> |
||||
|
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"> |
||||
|
<TextBlock.Styles> |
||||
|
<Style Selector="TextBlock"> |
||||
|
<Style.Animations> |
||||
|
<Animation Duration="0:0:1" IterationCount="Infinite"> |
||||
|
<KeyFrame Cue="0%"> |
||||
|
<Setter Property="Text" Value="" Animation.Animator="{x:Type pages:CustomStringAnimator}"/> |
||||
|
</KeyFrame> |
||||
|
<KeyFrame Cue="100%"> |
||||
|
<Setter Property="Text" Value="0123456789" Animation.Animator="{x:Type pages:CustomStringAnimator}"/> |
||||
|
</KeyFrame> |
||||
|
</Animation> |
||||
|
</Style.Animations> |
||||
|
</Style> |
||||
|
</TextBlock.Styles> |
||||
|
</TextBlock> |
||||
|
</Grid> |
||||
|
</UserControl> |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System.Reactive.Linq; |
||||
|
using Avalonia; |
||||
|
using Avalonia.Animation; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Controls.Shapes; |
||||
|
using Avalonia.Data; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.Markup.Xaml; |
||||
|
using Avalonia.Media; |
||||
|
using RenderDemo.ViewModels; |
||||
|
|
||||
|
namespace RenderDemo.Pages |
||||
|
{ |
||||
|
public class CustomAnimatorPage : UserControl |
||||
|
{ |
||||
|
public CustomAnimatorPage() |
||||
|
{ |
||||
|
InitializeComponent(); |
||||
|
} |
||||
|
|
||||
|
private void InitializeComponent() |
||||
|
{ |
||||
|
AvaloniaXamlLoader.Load(this); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Avalonia.Animation.Animators; |
||||
|
|
||||
|
namespace RenderDemo.Pages |
||||
|
{ |
||||
|
public class CustomStringAnimator : Animator<string> |
||||
|
{ |
||||
|
public override string Interpolate(double progress, string oldValue, string newValue) |
||||
|
{ |
||||
|
if (newValue.Length == 0) return ""; |
||||
|
var step = 1.0 / newValue.Length; |
||||
|
var length = (int)(progress / step); |
||||
|
var result = newValue.Substring(0, length + 1); |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Animation.Animators; |
||||
|
using Avalonia.Animation.Easings; |
||||
|
using Avalonia.Media; |
||||
|
|
||||
|
#nullable enable |
||||
|
|
||||
|
namespace Avalonia.Animation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Transition class that handles <see cref="AvaloniaProperty"/> with <see cref="IBrush"/> type.
|
||||
|
/// Only values of <see cref="ISolidColorBrush"/> will transition correctly at the moment.
|
||||
|
/// </summary>
|
||||
|
public class BrushTransition : Transition<IBrush?> |
||||
|
{ |
||||
|
private static readonly ISolidColorBrushAnimator s_animator = new ISolidColorBrushAnimator(); |
||||
|
|
||||
|
public override IObservable<IBrush?> DoTransition(IObservable<double> progress, IBrush? oldValue, IBrush? newValue) |
||||
|
{ |
||||
|
var oldSolidColorBrush = TryGetSolidColorBrush(oldValue); |
||||
|
var newSolidColorBrush = TryGetSolidColorBrush(newValue); |
||||
|
|
||||
|
if (oldSolidColorBrush != null && newSolidColorBrush != null) |
||||
|
{ |
||||
|
return new AnimatorTransitionObservable<ISolidColorBrush, ISolidColorBrushAnimator>( |
||||
|
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<IBrush?> |
||||
|
{ |
||||
|
private readonly IBrush? _from; |
||||
|
private readonly IBrush? _to; |
||||
|
|
||||
|
public IncompatibleTransitionObservable(IObservable<double> 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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using Avalonia.Collections; |
||||
|
|
||||
|
namespace Avalonia.Media |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents a set of line segments defined by a points collection with each Point specifying the end point of a line segment.
|
||||
|
/// </summary>
|
||||
|
public sealed class PolyLineSegment : PathSegment |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="Points"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly StyledProperty<Points> PointsProperty |
||||
|
= AvaloniaProperty.Register<PolyLineSegment, Points>(nameof(Points)); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the points.
|
||||
|
/// </summary>
|
||||
|
/// <value>
|
||||
|
/// The points.
|
||||
|
/// </value>
|
||||
|
public AvaloniaList<Point> Points |
||||
|
{ |
||||
|
get => GetValue(PointsProperty); |
||||
|
set => SetValue(PointsProperty, value); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PolyLineSegment"/> class.
|
||||
|
/// </summary>
|
||||
|
public PolyLineSegment() |
||||
|
{ |
||||
|
Points = new Points(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PolyLineSegment"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="points">The points.</param>
|
||||
|
public PolyLineSegment(IEnumerable<Point> points) : this() |
||||
|
{ |
||||
|
Points.AddRange(points); |
||||
|
} |
||||
|
|
||||
|
protected internal override void ApplyTo(StreamGeometryContext ctx) |
||||
|
{ |
||||
|
var points = Points; |
||||
|
if (points.Count > 0) |
||||
|
{ |
||||
|
for (int i = 0; i < points.Count; i++) |
||||
|
{ |
||||
|
ctx.LineTo(points[i]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public override string ToString() |
||||
|
=> Points.Count >= 1 ? "L " + string.Join(" ", Points) : ""; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue