committed by
GitHub
23 changed files with 1679 additions and 19 deletions
@ -0,0 +1,101 @@ |
|||||
|
<UserControl |
||||
|
xmlns="https://github.com/avaloniaui" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
|
x:Class="RenderDemo.Pages.TransitionsPage"> |
||||
|
<UserControl.Styles> |
||||
|
<Styles> |
||||
|
<Styles.Resources> |
||||
|
<Template x:Key="Acorn"> |
||||
|
<Path Fill="White" Stretch="Uniform" |
||||
|
Data="F1 M 16.6309,18.6563C 17.1309, |
||||
|
8.15625 29.8809,14.1563 29.8809, |
||||
|
14.1563C 30.8809,11.1563 34.1308, |
||||
|
11.4063 34.1308,11.4063C 33.5,12 |
||||
|
34.6309,13.1563 34.6309,13.1563C |
||||
|
32.1309,13.1562 31.1309,14.9062 |
||||
|
31.1309,14.9062C 41.1309,23.9062 |
||||
|
32.6309,27.9063 32.6309,27.9062C |
||||
|
24.6309,24.9063 21.1309,22.1562 |
||||
|
16.6309,18.6563 Z M 16.6309,19.9063C |
||||
|
21.6309,24.1563 25.1309,26.1562 |
||||
|
31.6309,28.6562C 31.6309,28.6562 |
||||
|
26.3809,39.1562 18.3809,36.1563C |
||||
|
18.3809,36.1563 18,38 16.3809,36.9063C |
||||
|
15,36 16.3809,34.9063 16.3809,34.9063C |
||||
|
16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z"/> |
||||
|
</Template> |
||||
|
<Template x:Key="Heart"> |
||||
|
<Path Fill="Red" Stretch="Uniform" Data=" |
||||
|
M 272.70141,238.71731 |
||||
|
C 206.46141,238.71731 152.70146,292.4773 152.70146,358.71731 |
||||
|
C 152.70146,493.47282 288.63461,528.80461 381.26391,662.02535 |
||||
|
C 468.83815,529.62199 609.82641,489.17075 609.82641,358.71731 |
||||
|
C 609.82641,292.47731 556.06651,238.7173 489.82641,238.71731 |
||||
|
C 441.77851,238.71731 400.42481,267.08774 381.26391,307.90481 |
||||
|
C 362.10311,267.08773 320.74941,238.7173 272.70141,238.71731 z "/> |
||||
|
</Template> |
||||
|
</Styles.Resources> |
||||
|
|
||||
|
<Style Selector="Border.Test"> |
||||
|
<Setter Property="Margin" Value="15"/> |
||||
|
<Setter Property="Width" Value="100"/> |
||||
|
<Setter Property="Height" Value="100"/> |
||||
|
<Setter Property="Child" Value="{StaticResource Acorn}"/> |
||||
|
<Setter Property="Transitions"> |
||||
|
<Transitions> |
||||
|
<TransformOperationsTransition Property="RenderTransform" Duration="0:0:1" /> |
||||
|
</Transitions> |
||||
|
</Setter> |
||||
|
<Setter Property="RenderTransform" Value="none" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect1:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="rotate(120deg) scale(1.5)" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect2:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="scale(0.8)" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect3"> |
||||
|
<Setter Property="Child" Value="{StaticResource Heart}"/> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect3:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="rotate(1turn)" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect4:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="translateY(-100px)" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect5:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="skewX(-20deg)" /> |
||||
|
</Style> |
||||
|
|
||||
|
<Style Selector="Border.Rect5:pointerover"> |
||||
|
<Setter Property="RenderTransform" Value="skewX(-20deg)" /> |
||||
|
</Style> |
||||
|
|
||||
|
</Styles> |
||||
|
</UserControl.Styles> |
||||
|
|
||||
|
<Grid> |
||||
|
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="False"> |
||||
|
<StackPanel.Clock> |
||||
|
<Clock /> |
||||
|
</StackPanel.Clock> |
||||
|
<StackPanel Orientation="Horizontal" VerticalAlignment="Center"> |
||||
|
<TextBlock VerticalAlignment="Center">Hover to activate Transform Keyframe Animations.</TextBlock> |
||||
|
<Button Content="{Binding PlayStateText}" Command="{Binding TogglePlayState}" Click="ToggleClock" /> |
||||
|
</StackPanel> |
||||
|
<WrapPanel ClipToBounds="False"> |
||||
|
<Border Classes="Test Rect1" Background="DarkRed"/> |
||||
|
<Border Classes="Test Rect2" Background="Magenta"/> |
||||
|
<Border Classes="Test Rect3"/> |
||||
|
<Border Classes="Test Rect4" Background="Navy"/> |
||||
|
<Border Classes="Test Rect5" Background="SeaGreen"/> |
||||
|
</WrapPanel> |
||||
|
</StackPanel> |
||||
|
</Grid> |
||||
|
</UserControl> |
||||
@ -0,0 +1,37 @@ |
|||||
|
using Avalonia.Animation; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.Markup.Xaml; |
||||
|
using RenderDemo.ViewModels; |
||||
|
|
||||
|
namespace RenderDemo.Pages |
||||
|
{ |
||||
|
public class TransitionsPage : UserControl |
||||
|
{ |
||||
|
public TransitionsPage() |
||||
|
{ |
||||
|
InitializeComponent(); |
||||
|
this.DataContext = new AnimationsPageViewModel(); |
||||
|
} |
||||
|
|
||||
|
private void InitializeComponent() |
||||
|
{ |
||||
|
AvaloniaXamlLoader.Load(this); |
||||
|
} |
||||
|
|
||||
|
private void ToggleClock(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
var button = sender as Button; |
||||
|
var clock = button.Clock; |
||||
|
|
||||
|
if (clock.PlayState == PlayState.Run) |
||||
|
{ |
||||
|
clock.PlayState = PlayState.Pause; |
||||
|
} |
||||
|
else if (clock.PlayState == PlayState.Pause) |
||||
|
{ |
||||
|
clock.PlayState = PlayState.Run; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Media; |
||||
|
using Avalonia.Media.Transformation; |
||||
|
|
||||
|
namespace Avalonia.Animation.Animators |
||||
|
{ |
||||
|
public class TransformOperationsAnimator : Animator<TransformOperations> |
||||
|
{ |
||||
|
public TransformOperationsAnimator() |
||||
|
{ |
||||
|
Validate = ValidateTransform; |
||||
|
} |
||||
|
|
||||
|
public override TransformOperations Interpolate(double progress, TransformOperations oldValue, TransformOperations newValue) |
||||
|
{ |
||||
|
var oldTransform = EnsureOperations(oldValue); |
||||
|
var newTransform = EnsureOperations(newValue); |
||||
|
|
||||
|
return TransformOperations.Interpolate(oldTransform, newTransform, progress); |
||||
|
} |
||||
|
|
||||
|
internal static TransformOperations EnsureOperations(ITransform value) |
||||
|
{ |
||||
|
return value as TransformOperations ?? TransformOperations.Identity; |
||||
|
} |
||||
|
|
||||
|
private void ValidateTransform(AnimatorKeyFrame kf) |
||||
|
{ |
||||
|
if (!(kf.Value is TransformOperations)) |
||||
|
{ |
||||
|
throw new InvalidOperationException($"All keyframes must be of type {typeof(TransformOperations)}."); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System; |
||||
|
using System.Reactive.Linq; |
||||
|
using Avalonia.Animation.Animators; |
||||
|
using Avalonia.Media; |
||||
|
|
||||
|
namespace Avalonia.Animation |
||||
|
{ |
||||
|
public class TransformOperationsTransition : Transition<ITransform> |
||||
|
{ |
||||
|
private static readonly TransformOperationsAnimator _operationsAnimator = new TransformOperationsAnimator(); |
||||
|
|
||||
|
public override IObservable<ITransform> DoTransition(IObservable<double> progress, |
||||
|
ITransform oldValue, |
||||
|
ITransform newValue) |
||||
|
{ |
||||
|
var oldTransform = TransformOperationsAnimator.EnsureOperations(oldValue); |
||||
|
var newTransform = TransformOperationsAnimator.EnsureOperations(newValue); |
||||
|
|
||||
|
return progress |
||||
|
.Select(p => |
||||
|
{ |
||||
|
var f = Easing.Ease(p); |
||||
|
|
||||
|
return _operationsAnimator.Interpolate(f, oldTransform, newTransform); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Avalonia.Media |
||||
|
{ |
||||
|
public interface IMutableTransform : ITransform |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Raised when the transform changes.
|
||||
|
/// </summary>
|
||||
|
event EventHandler Changed; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.ComponentModel; |
||||
|
|
||||
|
namespace Avalonia.Media |
||||
|
{ |
||||
|
[TypeConverter(typeof(TransformConverter))] |
||||
|
public interface ITransform |
||||
|
{ |
||||
|
Matrix Value { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel; |
||||
|
using System.Globalization; |
||||
|
using Avalonia.Media.Transformation; |
||||
|
|
||||
|
namespace Avalonia.Media |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Creates an <see cref="ITransform"/> from a string representation.
|
||||
|
/// </summary>
|
||||
|
public class TransformConverter : TypeConverter |
||||
|
{ |
||||
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
||||
|
{ |
||||
|
return sourceType == typeof(string); |
||||
|
} |
||||
|
|
||||
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
||||
|
{ |
||||
|
return TransformOperations.Parse((string)value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
namespace Avalonia.Media.Transformation |
||||
|
{ |
||||
|
internal static class InterpolationUtilities |
||||
|
{ |
||||
|
public static double InterpolateScalars(double from, double to, double progress) |
||||
|
{ |
||||
|
return from * (1d - progress) + to * progress; |
||||
|
} |
||||
|
|
||||
|
public static Vector InterpolateVectors(Vector from, Vector to, double progress) |
||||
|
{ |
||||
|
var x = InterpolateScalars(from.X, to.X, progress); |
||||
|
var y = InterpolateScalars(from.Y, to.Y, progress); |
||||
|
|
||||
|
return new Vector(x, y); |
||||
|
} |
||||
|
|
||||
|
public static Matrix ComposeTransform(Matrix.Decomposed decomposed) |
||||
|
{ |
||||
|
// According to https://www.w3.org/TR/css-transforms-1/#recomposing-to-a-2d-matrix
|
||||
|
|
||||
|
return Matrix.CreateTranslation(decomposed.Translate) * |
||||
|
Matrix.CreateRotation(decomposed.Angle) * |
||||
|
Matrix.CreateSkew(decomposed.Skew.X, decomposed.Skew.Y) * |
||||
|
Matrix.CreateScale(decomposed.Scale); |
||||
|
} |
||||
|
|
||||
|
public static Matrix.Decomposed InterpolateDecomposedTransforms(ref Matrix.Decomposed from, ref Matrix.Decomposed to, double progres) |
||||
|
{ |
||||
|
Matrix.Decomposed result = default; |
||||
|
|
||||
|
result.Translate = InterpolateVectors(from.Translate, to.Translate, progres); |
||||
|
result.Scale = InterpolateVectors(from.Scale, to.Scale, progres); |
||||
|
result.Skew = InterpolateVectors(from.Skew, to.Skew, progres); |
||||
|
result.Angle = InterpolateScalars(from.Angle, to.Angle, progres); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,230 @@ |
|||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
namespace Avalonia.Media.Transformation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents a single primitive transform (like translation, rotation, scale, etc.).
|
||||
|
/// </summary>
|
||||
|
public struct TransformOperation |
||||
|
{ |
||||
|
public OperationType Type; |
||||
|
public Matrix Matrix; |
||||
|
public DataLayout Data; |
||||
|
|
||||
|
public enum OperationType |
||||
|
{ |
||||
|
Translate, |
||||
|
Rotate, |
||||
|
Scale, |
||||
|
Skew, |
||||
|
Matrix, |
||||
|
Identity |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns whether operation produces the identity matrix.
|
||||
|
/// </summary>
|
||||
|
public bool IsIdentity => Matrix.IsIdentity; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Bakes this operation to a transform matrix.
|
||||
|
/// </summary>
|
||||
|
public void Bake() |
||||
|
{ |
||||
|
Matrix = Matrix.Identity; |
||||
|
|
||||
|
switch (Type) |
||||
|
{ |
||||
|
case OperationType.Translate: |
||||
|
{ |
||||
|
Matrix = Matrix.CreateTranslation(Data.Translate.X, Data.Translate.Y); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Rotate: |
||||
|
{ |
||||
|
Matrix = Matrix.CreateRotation(Data.Rotate.Angle); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Scale: |
||||
|
{ |
||||
|
Matrix = Matrix.CreateScale(Data.Scale.X, Data.Scale.Y); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Skew: |
||||
|
{ |
||||
|
Matrix = Matrix.CreateSkew(Data.Skew.X, Data.Skew.Y); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns new identity transform operation.
|
||||
|
/// </summary>
|
||||
|
public static TransformOperation Identity => |
||||
|
new TransformOperation { Matrix = Matrix.Identity, Type = OperationType.Identity }; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Attempts to interpolate between two transform operations.
|
||||
|
/// </summary>
|
||||
|
/// <param name="from">Source operation.</param>
|
||||
|
/// <param name="to">Target operation.</param>
|
||||
|
/// <param name="progress">Interpolation progress.</param>
|
||||
|
/// <param name="result">Interpolation result that will be filled in when operation was successful.</param>
|
||||
|
/// <remarks>
|
||||
|
/// Based upon https://www.w3.org/TR/css-transforms-1/#interpolation-of-transform-functions.
|
||||
|
/// </remarks>
|
||||
|
public static bool TryInterpolate(TransformOperation? from, TransformOperation? to, double progress, |
||||
|
ref TransformOperation result) |
||||
|
{ |
||||
|
bool fromIdentity = IsOperationIdentity(ref from); |
||||
|
bool toIdentity = IsOperationIdentity(ref to); |
||||
|
|
||||
|
if (fromIdentity && toIdentity) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
// ReSharper disable PossibleInvalidOperationException
|
||||
|
TransformOperation fromValue = fromIdentity ? Identity : from.Value; |
||||
|
TransformOperation toValue = toIdentity ? Identity : to.Value; |
||||
|
// ReSharper restore PossibleInvalidOperationException
|
||||
|
|
||||
|
var interpolationType = toIdentity ? fromValue.Type : toValue.Type; |
||||
|
|
||||
|
result.Type = interpolationType; |
||||
|
|
||||
|
switch (interpolationType) |
||||
|
{ |
||||
|
case OperationType.Translate: |
||||
|
{ |
||||
|
double fromX = fromIdentity ? 0 : fromValue.Data.Translate.X; |
||||
|
double fromY = fromIdentity ? 0 : fromValue.Data.Translate.Y; |
||||
|
|
||||
|
double toX = toIdentity ? 0 : toValue.Data.Translate.X; |
||||
|
double toY = toIdentity ? 0 : toValue.Data.Translate.Y; |
||||
|
|
||||
|
result.Data.Translate.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress); |
||||
|
result.Data.Translate.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress); |
||||
|
|
||||
|
result.Bake(); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Rotate: |
||||
|
{ |
||||
|
double fromAngle = fromIdentity ? 0 : fromValue.Data.Rotate.Angle; |
||||
|
|
||||
|
double toAngle = toIdentity ? 0 : toValue.Data.Rotate.Angle; |
||||
|
|
||||
|
result.Data.Rotate.Angle = InterpolationUtilities.InterpolateScalars(fromAngle, toAngle, progress); |
||||
|
|
||||
|
result.Bake(); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Scale: |
||||
|
{ |
||||
|
double fromX = fromIdentity ? 1 : fromValue.Data.Scale.X; |
||||
|
double fromY = fromIdentity ? 1 : fromValue.Data.Scale.Y; |
||||
|
|
||||
|
double toX = toIdentity ? 1 : toValue.Data.Scale.X; |
||||
|
double toY = toIdentity ? 1 : toValue.Data.Scale.Y; |
||||
|
|
||||
|
result.Data.Scale.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress); |
||||
|
result.Data.Scale.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress); |
||||
|
|
||||
|
result.Bake(); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Skew: |
||||
|
{ |
||||
|
double fromX = fromIdentity ? 0 : fromValue.Data.Skew.X; |
||||
|
double fromY = fromIdentity ? 0 : fromValue.Data.Skew.Y; |
||||
|
|
||||
|
double toX = toIdentity ? 0 : toValue.Data.Skew.X; |
||||
|
double toY = toIdentity ? 0 : toValue.Data.Skew.Y; |
||||
|
|
||||
|
result.Data.Skew.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress); |
||||
|
result.Data.Skew.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress); |
||||
|
|
||||
|
result.Bake(); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Matrix: |
||||
|
{ |
||||
|
var fromMatrix = fromIdentity ? Matrix.Identity : fromValue.Matrix; |
||||
|
var toMatrix = toIdentity ? Matrix.Identity : toValue.Matrix; |
||||
|
|
||||
|
if (!Matrix.TryDecomposeTransform(fromMatrix, out Matrix.Decomposed fromDecomposed) || |
||||
|
!Matrix.TryDecomposeTransform(toMatrix, out Matrix.Decomposed toDecomposed)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
var interpolated = |
||||
|
InterpolationUtilities.InterpolateDecomposedTransforms( |
||||
|
ref fromDecomposed, ref toDecomposed, |
||||
|
progress); |
||||
|
|
||||
|
result.Matrix = InterpolationUtilities.ComposeTransform(interpolated); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case OperationType.Identity: |
||||
|
{ |
||||
|
// Do nothing.
|
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private static bool IsOperationIdentity(ref TransformOperation? operation) |
||||
|
{ |
||||
|
return !operation.HasValue || operation.Value.IsIdentity; |
||||
|
} |
||||
|
|
||||
|
[StructLayout(LayoutKind.Explicit)] |
||||
|
public struct DataLayout |
||||
|
{ |
||||
|
[FieldOffset(0)] public SkewLayout Skew; |
||||
|
|
||||
|
[FieldOffset(0)] public ScaleLayout Scale; |
||||
|
|
||||
|
[FieldOffset(0)] public TranslateLayout Translate; |
||||
|
|
||||
|
[FieldOffset(0)] public RotateLayout Rotate; |
||||
|
|
||||
|
public struct SkewLayout |
||||
|
{ |
||||
|
public double X; |
||||
|
public double Y; |
||||
|
} |
||||
|
|
||||
|
public struct ScaleLayout |
||||
|
{ |
||||
|
public double X; |
||||
|
public double Y; |
||||
|
} |
||||
|
|
||||
|
public struct TranslateLayout |
||||
|
{ |
||||
|
public double X; |
||||
|
public double Y; |
||||
|
} |
||||
|
|
||||
|
public struct RotateLayout |
||||
|
{ |
||||
|
public double Angle; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,258 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Avalonia.Media.Transformation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Contains a list of <see cref="TransformOperation"/> that represent primitive transforms that will be
|
||||
|
/// applied in declared order.
|
||||
|
/// </summary>
|
||||
|
public sealed class TransformOperations : ITransform |
||||
|
{ |
||||
|
public static TransformOperations Identity { get; } = new TransformOperations(new List<TransformOperation>()); |
||||
|
|
||||
|
private readonly List<TransformOperation> _operations; |
||||
|
|
||||
|
private TransformOperations(List<TransformOperation> operations) |
||||
|
{ |
||||
|
_operations = operations ?? throw new ArgumentNullException(nameof(operations)); |
||||
|
|
||||
|
IsIdentity = CheckIsIdentity(); |
||||
|
|
||||
|
Value = ApplyTransforms(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns whether all operations combined together produce the identity matrix.
|
||||
|
/// </summary>
|
||||
|
public bool IsIdentity { get; } |
||||
|
|
||||
|
public IReadOnlyList<TransformOperation> Operations => _operations; |
||||
|
|
||||
|
public Matrix Value { get; } |
||||
|
|
||||
|
public static TransformOperations Parse(string s) |
||||
|
{ |
||||
|
return TransformParser.Parse(s); |
||||
|
} |
||||
|
|
||||
|
public static Builder CreateBuilder(int capacity) |
||||
|
{ |
||||
|
return new Builder(capacity); |
||||
|
} |
||||
|
|
||||
|
public static TransformOperations Interpolate(TransformOperations from, TransformOperations to, double progress) |
||||
|
{ |
||||
|
TransformOperations result = Identity; |
||||
|
|
||||
|
if (!TryInterpolate(from, to, progress, ref result)) |
||||
|
{ |
||||
|
// If the matrices cannot be interpolated, fallback to discrete animation logic.
|
||||
|
// See https://drafts.csswg.org/css-transforms/#matrix-interpolation
|
||||
|
result = progress < 0.5 ? from : to; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
private Matrix ApplyTransforms(int startOffset = 0) |
||||
|
{ |
||||
|
Matrix matrix = Matrix.Identity; |
||||
|
|
||||
|
for (var i = startOffset; i < _operations.Count; i++) |
||||
|
{ |
||||
|
TransformOperation operation = _operations[i]; |
||||
|
matrix *= operation.Matrix; |
||||
|
} |
||||
|
|
||||
|
return matrix; |
||||
|
} |
||||
|
|
||||
|
private bool CheckIsIdentity() |
||||
|
{ |
||||
|
foreach (TransformOperation operation in _operations) |
||||
|
{ |
||||
|
if (!operation.IsIdentity) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private static bool TryInterpolate(TransformOperations from, TransformOperations to, double progress, ref TransformOperations result) |
||||
|
{ |
||||
|
bool fromIdentity = from.IsIdentity; |
||||
|
bool toIdentity = to.IsIdentity; |
||||
|
|
||||
|
if (fromIdentity && toIdentity) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
int matchingPrefixLength = ComputeMatchingPrefixLength(from, to); |
||||
|
int fromSize = fromIdentity ? 0 : from._operations.Count; |
||||
|
int toSize = toIdentity ? 0 : to._operations.Count; |
||||
|
int numOperations = Math.Max(fromSize, toSize); |
||||
|
|
||||
|
var builder = new Builder(matchingPrefixLength); |
||||
|
|
||||
|
for (int i = 0; i < matchingPrefixLength; i++) |
||||
|
{ |
||||
|
TransformOperation interpolated = new TransformOperation |
||||
|
{ |
||||
|
Type = TransformOperation.OperationType.Identity |
||||
|
}; |
||||
|
|
||||
|
if (!TransformOperation.TryInterpolate( |
||||
|
i >= fromSize ? default(TransformOperation?) : from._operations[i], |
||||
|
i >= toSize ? default(TransformOperation?) : to._operations[i], |
||||
|
progress, |
||||
|
ref interpolated)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
builder.Append(interpolated); |
||||
|
} |
||||
|
|
||||
|
if (matchingPrefixLength < numOperations) |
||||
|
{ |
||||
|
if (!ComputeDecomposedTransform(from, matchingPrefixLength, out Matrix.Decomposed fromDecomposed) || |
||||
|
!ComputeDecomposedTransform(to, matchingPrefixLength, out Matrix.Decomposed toDecomposed)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
var transform = InterpolationUtilities.InterpolateDecomposedTransforms(ref fromDecomposed, ref toDecomposed, progress); |
||||
|
|
||||
|
builder.AppendMatrix(InterpolationUtilities.ComposeTransform(transform)); |
||||
|
} |
||||
|
|
||||
|
result = builder.Build(); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private static bool ComputeDecomposedTransform(TransformOperations operations, int startOffset, out Matrix.Decomposed decomposed) |
||||
|
{ |
||||
|
Matrix transform = operations.ApplyTransforms(startOffset); |
||||
|
|
||||
|
if (!Matrix.TryDecomposeTransform(transform, out decomposed)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private static int ComputeMatchingPrefixLength(TransformOperations from, TransformOperations to) |
||||
|
{ |
||||
|
int numOperations = Math.Min(from._operations.Count, to._operations.Count); |
||||
|
|
||||
|
for (int i = 0; i < numOperations; i++) |
||||
|
{ |
||||
|
if (from._operations[i].Type != to._operations[i].Type) |
||||
|
{ |
||||
|
return i; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// If the operations match to the length of the shorter list, then pad its
|
||||
|
// length with the matching identity operations.
|
||||
|
// https://drafts.csswg.org/css-transforms/#transform-function-lists
|
||||
|
return Math.Max(from._operations.Count, to._operations.Count); |
||||
|
} |
||||
|
|
||||
|
public readonly struct Builder |
||||
|
{ |
||||
|
private readonly List<TransformOperation> _operations; |
||||
|
|
||||
|
public Builder(int capacity) |
||||
|
{ |
||||
|
_operations = new List<TransformOperation>(capacity); |
||||
|
} |
||||
|
|
||||
|
public void AppendTranslate(double x, double y) |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Translate; |
||||
|
toAdd.Data.Translate.X = x; |
||||
|
toAdd.Data.Translate.Y = y; |
||||
|
|
||||
|
toAdd.Bake(); |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void AppendRotate(double angle) |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Rotate; |
||||
|
toAdd.Data.Rotate.Angle = angle; |
||||
|
|
||||
|
toAdd.Bake(); |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void AppendScale(double x, double y) |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Scale; |
||||
|
toAdd.Data.Scale.X = x; |
||||
|
toAdd.Data.Scale.Y = y; |
||||
|
|
||||
|
toAdd.Bake(); |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void AppendSkew(double x, double y) |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Skew; |
||||
|
toAdd.Data.Skew.X = x; |
||||
|
toAdd.Data.Skew.Y = y; |
||||
|
|
||||
|
toAdd.Bake(); |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void AppendMatrix(Matrix matrix) |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Matrix; |
||||
|
toAdd.Matrix = matrix; |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void AppendIdentity() |
||||
|
{ |
||||
|
var toAdd = new TransformOperation(); |
||||
|
|
||||
|
toAdd.Type = TransformOperation.OperationType.Identity; |
||||
|
|
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public void Append(TransformOperation toAdd) |
||||
|
{ |
||||
|
_operations.Add(toAdd); |
||||
|
} |
||||
|
|
||||
|
public TransformOperations Build() |
||||
|
{ |
||||
|
return new TransformOperations(_operations); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,444 @@ |
|||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using Avalonia.Utilities; |
||||
|
|
||||
|
namespace Avalonia.Media.Transformation |
||||
|
{ |
||||
|
public static class TransformParser |
||||
|
{ |
||||
|
private static readonly (string, TransformFunction)[] s_functionMapping = |
||||
|
{ |
||||
|
("translate", TransformFunction.Translate), |
||||
|
("translateX", TransformFunction.TranslateX), |
||||
|
("translateY", TransformFunction.TranslateY), |
||||
|
("scale", TransformFunction.Scale), |
||||
|
("scaleX", TransformFunction.ScaleX), |
||||
|
("scaleY", TransformFunction.ScaleY), |
||||
|
("skew", TransformFunction.Skew), |
||||
|
("skewX", TransformFunction.SkewX), |
||||
|
("skewY", TransformFunction.SkewY), |
||||
|
("rotate", TransformFunction.Rotate), |
||||
|
("matrix", TransformFunction.Matrix) |
||||
|
}; |
||||
|
|
||||
|
private static readonly (string, Unit)[] s_unitMapping = |
||||
|
{ |
||||
|
("deg", Unit.Degree), |
||||
|
("grad", Unit.Gradian), |
||||
|
("rad", Unit.Radian), |
||||
|
("turn", Unit.Turn), |
||||
|
("px", Unit.Pixel) |
||||
|
}; |
||||
|
|
||||
|
public static TransformOperations Parse(string s) |
||||
|
{ |
||||
|
void ThrowInvalidFormat() |
||||
|
{ |
||||
|
throw new FormatException($"Invalid transform string: '{s}'."); |
||||
|
} |
||||
|
|
||||
|
if (string.IsNullOrEmpty(s)) |
||||
|
{ |
||||
|
throw new ArgumentException(nameof(s)); |
||||
|
} |
||||
|
|
||||
|
var span = s.AsSpan().Trim(); |
||||
|
|
||||
|
if (span.Equals("none".AsSpan(), StringComparison.OrdinalIgnoreCase)) |
||||
|
{ |
||||
|
return TransformOperations.Identity; |
||||
|
} |
||||
|
|
||||
|
var builder = TransformOperations.CreateBuilder(0); |
||||
|
|
||||
|
while (true) |
||||
|
{ |
||||
|
var beginIndex = span.IndexOf('('); |
||||
|
var endIndex = span.IndexOf(')'); |
||||
|
|
||||
|
if (beginIndex == -1 || endIndex == -1) |
||||
|
{ |
||||
|
ThrowInvalidFormat(); |
||||
|
} |
||||
|
|
||||
|
var namePart = span.Slice(0, beginIndex).Trim(); |
||||
|
|
||||
|
var function = ParseTransformFunction(in namePart); |
||||
|
|
||||
|
if (function == TransformFunction.Invalid) |
||||
|
{ |
||||
|
ThrowInvalidFormat(); |
||||
|
} |
||||
|
|
||||
|
var valuePart = span.Slice(beginIndex + 1, endIndex - beginIndex - 1).Trim(); |
||||
|
|
||||
|
ParseFunction(in valuePart, function, in builder); |
||||
|
|
||||
|
span = span.Slice(endIndex + 1); |
||||
|
|
||||
|
if (span.IsWhiteSpace()) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return builder.Build(); |
||||
|
} |
||||
|
|
||||
|
private static void ParseFunction( |
||||
|
in ReadOnlySpan<char> functionPart, |
||||
|
TransformFunction function, |
||||
|
in TransformOperations.Builder builder) |
||||
|
{ |
||||
|
static UnitValue ParseValue(ReadOnlySpan<char> part) |
||||
|
{ |
||||
|
int unitIndex = -1; |
||||
|
|
||||
|
for (int i = 0; i < part.Length; i++) |
||||
|
{ |
||||
|
char c = part[i]; |
||||
|
|
||||
|
if (char.IsDigit(c) || c == '-' || c == '.') |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
unitIndex = i; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
Unit unit = Unit.None; |
||||
|
|
||||
|
if (unitIndex != -1) |
||||
|
{ |
||||
|
var unitPart = part.Slice(unitIndex, part.Length - unitIndex); |
||||
|
|
||||
|
unit = ParseUnit(unitPart); |
||||
|
|
||||
|
part = part.Slice(0, unitIndex); |
||||
|
} |
||||
|
|
||||
|
var value = double.Parse(part.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture); |
||||
|
|
||||
|
return new UnitValue(unit, value); |
||||
|
} |
||||
|
|
||||
|
static int ParseValuePair( |
||||
|
in ReadOnlySpan<char> part, |
||||
|
ref UnitValue leftValue, |
||||
|
ref UnitValue rightValue) |
||||
|
{ |
||||
|
var commaIndex = part.IndexOf(','); |
||||
|
|
||||
|
if (commaIndex != -1) |
||||
|
{ |
||||
|
var leftPart = part.Slice(0, commaIndex).Trim(); |
||||
|
var rightPart = part.Slice(commaIndex + 1, part.Length - commaIndex - 1).Trim(); |
||||
|
|
||||
|
leftValue = ParseValue(leftPart); |
||||
|
rightValue = ParseValue(rightPart); |
||||
|
|
||||
|
return 2; |
||||
|
} |
||||
|
|
||||
|
leftValue = ParseValue(part); |
||||
|
|
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
static int ParseCommaDelimitedValues(ReadOnlySpan<char> part, in Span<UnitValue> outValues) |
||||
|
{ |
||||
|
int valueIndex = 0; |
||||
|
|
||||
|
while (true) |
||||
|
{ |
||||
|
if (valueIndex >= outValues.Length) |
||||
|
{ |
||||
|
throw new FormatException("Too many provided values."); |
||||
|
} |
||||
|
|
||||
|
var commaIndex = part.IndexOf(','); |
||||
|
|
||||
|
if (commaIndex == -1) |
||||
|
{ |
||||
|
if (!part.IsWhiteSpace()) |
||||
|
{ |
||||
|
outValues[valueIndex++] = ParseValue(part); |
||||
|
} |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
var valuePart = part.Slice(0, commaIndex).Trim(); |
||||
|
|
||||
|
outValues[valueIndex++] = ParseValue(valuePart); |
||||
|
|
||||
|
part = part.Slice(commaIndex + 1, part.Length - commaIndex - 1); |
||||
|
} |
||||
|
|
||||
|
return valueIndex; |
||||
|
} |
||||
|
|
||||
|
switch (function) |
||||
|
{ |
||||
|
case TransformFunction.Scale: |
||||
|
case TransformFunction.ScaleX: |
||||
|
case TransformFunction.ScaleY: |
||||
|
{ |
||||
|
var scaleX = UnitValue.One; |
||||
|
var scaleY = UnitValue.One; |
||||
|
|
||||
|
int count = ParseValuePair(functionPart, ref scaleX, ref scaleY); |
||||
|
|
||||
|
if (count != 1 && (function == TransformFunction.ScaleX || function == TransformFunction.ScaleY)) |
||||
|
{ |
||||
|
ThrowFormatInvalidValueCount(function, 1); |
||||
|
} |
||||
|
|
||||
|
VerifyZeroOrUnit(function, in scaleX, Unit.None); |
||||
|
VerifyZeroOrUnit(function, in scaleY, Unit.None); |
||||
|
|
||||
|
if (function == TransformFunction.ScaleY) |
||||
|
{ |
||||
|
scaleY = scaleX; |
||||
|
scaleX = UnitValue.One; |
||||
|
} |
||||
|
else if (function == TransformFunction.Scale && count == 1) |
||||
|
{ |
||||
|
scaleY = scaleX; |
||||
|
} |
||||
|
|
||||
|
builder.AppendScale(scaleX.Value, scaleY.Value); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case TransformFunction.Skew: |
||||
|
case TransformFunction.SkewX: |
||||
|
case TransformFunction.SkewY: |
||||
|
{ |
||||
|
var skewX = UnitValue.Zero; |
||||
|
var skewY = UnitValue.Zero; |
||||
|
|
||||
|
int count = ParseValuePair(functionPart, ref skewX, ref skewY); |
||||
|
|
||||
|
if (count != 1 && (function == TransformFunction.SkewX || function == TransformFunction.SkewY)) |
||||
|
{ |
||||
|
ThrowFormatInvalidValueCount(function, 1); |
||||
|
} |
||||
|
|
||||
|
VerifyZeroOrAngle(function, in skewX); |
||||
|
VerifyZeroOrAngle(function, in skewY); |
||||
|
|
||||
|
if (function == TransformFunction.SkewY) |
||||
|
{ |
||||
|
skewY = skewX; |
||||
|
skewX = UnitValue.Zero; |
||||
|
} |
||||
|
|
||||
|
builder.AppendSkew(ToRadians(in skewX), ToRadians(in skewY)); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case TransformFunction.Rotate: |
||||
|
{ |
||||
|
var angle = UnitValue.Zero; |
||||
|
UnitValue _ = default; |
||||
|
|
||||
|
int count = ParseValuePair(functionPart, ref angle, ref _); |
||||
|
|
||||
|
if (count != 1) |
||||
|
{ |
||||
|
ThrowFormatInvalidValueCount(function, 1); |
||||
|
} |
||||
|
|
||||
|
VerifyZeroOrAngle(function, in angle); |
||||
|
|
||||
|
builder.AppendRotate(ToRadians(in angle)); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case TransformFunction.Translate: |
||||
|
case TransformFunction.TranslateX: |
||||
|
case TransformFunction.TranslateY: |
||||
|
{ |
||||
|
var translateX = UnitValue.Zero; |
||||
|
var translateY = UnitValue.Zero; |
||||
|
|
||||
|
int count = ParseValuePair(functionPart, ref translateX, ref translateY); |
||||
|
|
||||
|
if (count != 1 && (function == TransformFunction.TranslateX || function == TransformFunction.TranslateY)) |
||||
|
{ |
||||
|
ThrowFormatInvalidValueCount(function, 1); |
||||
|
} |
||||
|
|
||||
|
VerifyZeroOrUnit(function, in translateX, Unit.Pixel); |
||||
|
VerifyZeroOrUnit(function, in translateY, Unit.Pixel); |
||||
|
|
||||
|
if (function == TransformFunction.TranslateY) |
||||
|
{ |
||||
|
translateY = translateX; |
||||
|
translateX = UnitValue.Zero; |
||||
|
} |
||||
|
|
||||
|
builder.AppendTranslate(translateX.Value, translateY.Value); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case TransformFunction.Matrix: |
||||
|
{ |
||||
|
Span<UnitValue> values = stackalloc UnitValue[6]; |
||||
|
|
||||
|
int count = ParseCommaDelimitedValues(functionPart, in values); |
||||
|
|
||||
|
if (count != 6) |
||||
|
{ |
||||
|
ThrowFormatInvalidValueCount(function, 6); |
||||
|
} |
||||
|
|
||||
|
foreach (UnitValue value in values) |
||||
|
{ |
||||
|
VerifyZeroOrUnit(function, value, Unit.None); |
||||
|
} |
||||
|
|
||||
|
var matrix = new Matrix( |
||||
|
values[0].Value, |
||||
|
values[1].Value, |
||||
|
values[2].Value, |
||||
|
values[3].Value, |
||||
|
values[4].Value, |
||||
|
values[5].Value); |
||||
|
|
||||
|
builder.AppendMatrix(matrix); |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void VerifyZeroOrUnit(TransformFunction function, in UnitValue value, Unit unit) |
||||
|
{ |
||||
|
bool isZero = value.Unit == Unit.None && value.Value == 0d; |
||||
|
|
||||
|
if (!isZero && value.Unit != unit) |
||||
|
{ |
||||
|
ThrowFormatInvalidValue(function, in value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void VerifyZeroOrAngle(TransformFunction function, in UnitValue value) |
||||
|
{ |
||||
|
if (value.Value != 0d && !IsAngleUnit(value.Unit)) |
||||
|
{ |
||||
|
ThrowFormatInvalidValue(function, in value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static bool IsAngleUnit(Unit unit) |
||||
|
{ |
||||
|
switch (unit) |
||||
|
{ |
||||
|
case Unit.Radian: |
||||
|
case Unit.Gradian: |
||||
|
case Unit.Degree: |
||||
|
case Unit.Turn: |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
private static void ThrowFormatInvalidValue(TransformFunction function, in UnitValue value) |
||||
|
{ |
||||
|
var unitString = value.Unit == Unit.None ? string.Empty : value.Unit.ToString(); |
||||
|
|
||||
|
throw new FormatException($"Invalid value {value.Value} {unitString} for {function}"); |
||||
|
} |
||||
|
|
||||
|
private static void ThrowFormatInvalidValueCount(TransformFunction function, int count) |
||||
|
{ |
||||
|
throw new FormatException($"Invalid format. {function} expects {count} value(s)."); |
||||
|
} |
||||
|
|
||||
|
private static Unit ParseUnit(in ReadOnlySpan<char> part) |
||||
|
{ |
||||
|
foreach (var (name, unit) in s_unitMapping) |
||||
|
{ |
||||
|
if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase)) |
||||
|
{ |
||||
|
return unit; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
throw new FormatException($"Invalid unit: {part.ToString()}"); |
||||
|
} |
||||
|
|
||||
|
private static TransformFunction ParseTransformFunction(in ReadOnlySpan<char> part) |
||||
|
{ |
||||
|
foreach (var (name, transformFunction) in s_functionMapping) |
||||
|
{ |
||||
|
if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase)) |
||||
|
{ |
||||
|
return transformFunction; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return TransformFunction.Invalid; |
||||
|
} |
||||
|
|
||||
|
private static double ToRadians(in UnitValue value) |
||||
|
{ |
||||
|
return value.Unit switch |
||||
|
{ |
||||
|
Unit.Radian => value.Value, |
||||
|
Unit.Gradian => MathUtilities.Grad2Rad(value.Value), |
||||
|
Unit.Degree => MathUtilities.Deg2Rad(value.Value), |
||||
|
Unit.Turn => MathUtilities.Turn2Rad(value.Value), |
||||
|
_ => value.Value |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
private enum Unit |
||||
|
{ |
||||
|
None, |
||||
|
Pixel, |
||||
|
Radian, |
||||
|
Gradian, |
||||
|
Degree, |
||||
|
Turn |
||||
|
} |
||||
|
|
||||
|
private readonly struct UnitValue |
||||
|
{ |
||||
|
public readonly Unit Unit; |
||||
|
public readonly double Value; |
||||
|
|
||||
|
public UnitValue(Unit unit, double value) |
||||
|
{ |
||||
|
Unit = unit; |
||||
|
Value = value; |
||||
|
} |
||||
|
|
||||
|
public static UnitValue Zero => new UnitValue(Unit.None, 0); |
||||
|
|
||||
|
public static UnitValue One => new UnitValue(Unit.None, 1); |
||||
|
} |
||||
|
|
||||
|
private enum TransformFunction |
||||
|
{ |
||||
|
Invalid, |
||||
|
Translate, |
||||
|
TranslateX, |
||||
|
TranslateY, |
||||
|
Scale, |
||||
|
ScaleX, |
||||
|
ScaleY, |
||||
|
Skew, |
||||
|
SkewX, |
||||
|
SkewY, |
||||
|
Rotate, |
||||
|
Matrix |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
namespace Avalonia.Benchmarks.Visuals |
||||
|
{ |
||||
|
[MemoryDiagnoser, InProcess] |
||||
|
public class MatrixBenchmarks |
||||
|
{ |
||||
|
private static readonly Matrix s_data = Matrix.Identity; |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public bool Decompose() |
||||
|
{ |
||||
|
return Matrix.TryDecomposeTransform(s_data, out Matrix.Decomposed decomposed); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,229 @@ |
|||||
|
using Avalonia.Media.Transformation; |
||||
|
using Avalonia.Utilities; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Visuals.UnitTests.Media |
||||
|
{ |
||||
|
public class TransformOperationsTests |
||||
|
{ |
||||
|
[Theory] |
||||
|
[InlineData("translate(10px)", 10d, 0d)] |
||||
|
[InlineData("translate(10px, 10px)", 10d, 10d)] |
||||
|
[InlineData("translate(0px, 10px)", 0d, 10d)] |
||||
|
[InlineData("translate(10px, 0px)", 10d, 0d)] |
||||
|
[InlineData("translateX(10px)", 10d, 0d)] |
||||
|
[InlineData("translateY(10px)", 0d, 10d)] |
||||
|
public void Can_Parse_Translation(string data, double x, double y) |
||||
|
{ |
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Translate, operations[0].Type); |
||||
|
Assert.Equal(x, operations[0].Data.Translate.X); |
||||
|
Assert.Equal(y, operations[0].Data.Translate.Y); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("rotate(90deg)", 90d)] |
||||
|
[InlineData("rotate(0.5turn)", 180d)] |
||||
|
[InlineData("rotate(200grad)", 180d)] |
||||
|
[InlineData("rotate(3.14159265rad)", 180d)] |
||||
|
public void Can_Parse_Rotation(string data, double angleDeg) |
||||
|
{ |
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Rotate, operations[0].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(angleDeg), operations[0].Data.Rotate.Angle, 4); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("scale(10)", 10d, 10d)] |
||||
|
[InlineData("scale(10, 10)", 10d, 10d)] |
||||
|
[InlineData("scale(0, 10)", 0d, 10d)] |
||||
|
[InlineData("scale(10, 0)", 10d, 0d)] |
||||
|
[InlineData("scaleX(10)", 10d, 1d)] |
||||
|
[InlineData("scaleY(10)", 1d, 10d)] |
||||
|
public void Can_Parse_Scale(string data, double x, double y) |
||||
|
{ |
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type); |
||||
|
Assert.Equal(x, operations[0].Data.Scale.X); |
||||
|
Assert.Equal(y, operations[0].Data.Scale.Y); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("skew(90deg)", 90d, 0d)] |
||||
|
[InlineData("skew(0.5turn)", 180d, 0d)] |
||||
|
[InlineData("skew(200grad)", 180d, 0d)] |
||||
|
[InlineData("skew(3.14159265rad)", 180d, 0d)] |
||||
|
[InlineData("skewX(90deg)", 90d, 0d)] |
||||
|
[InlineData("skewX(0.5turn)", 180d, 0d)] |
||||
|
[InlineData("skewX(200grad)", 180d, 0d)] |
||||
|
[InlineData("skewX(3.14159265rad)", 180d, 0d)] |
||||
|
[InlineData("skew(0, 90deg)", 0d, 90d)] |
||||
|
[InlineData("skew(0, 0.5turn)", 0d, 180d)] |
||||
|
[InlineData("skew(0, 200grad)", 0d, 180d)] |
||||
|
[InlineData("skew(0, 3.14159265rad)", 0d, 180d)] |
||||
|
[InlineData("skewY(90deg)", 0d, 90d)] |
||||
|
[InlineData("skewY(0.5turn)", 0d, 180d)] |
||||
|
[InlineData("skewY(200grad)", 0d, 180d)] |
||||
|
[InlineData("skewY(3.14159265rad)", 0d, 180d)] |
||||
|
[InlineData("skew(90deg, 90deg)", 90d, 90d)] |
||||
|
[InlineData("skew(0.5turn, 0.5turn)", 180d, 180d)] |
||||
|
[InlineData("skew(200grad, 200grad)", 180d, 180d)] |
||||
|
[InlineData("skew(3.14159265rad, 3.14159265rad)", 180d, 180d)] |
||||
|
public void Can_Parse_Skew(string data, double x, double y) |
||||
|
{ |
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Skew, operations[0].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(x), operations[0].Data.Skew.X, 4); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(y), operations[0].Data.Skew.Y, 4); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Can_Parse_Compound_Operations() |
||||
|
{ |
||||
|
var data = "scale(1,2) translate(3px,4px) rotate(5deg) skew(6deg,7deg)"; |
||||
|
|
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type); |
||||
|
Assert.Equal(1, operations[0].Data.Scale.X); |
||||
|
Assert.Equal(2, operations[0].Data.Scale.Y); |
||||
|
|
||||
|
Assert.Equal(TransformOperation.OperationType.Translate, operations[1].Type); |
||||
|
Assert.Equal(3, operations[1].Data.Translate.X); |
||||
|
Assert.Equal(4, operations[1].Data.Translate.Y); |
||||
|
|
||||
|
Assert.Equal(TransformOperation.OperationType.Rotate, operations[2].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(5), operations[2].Data.Rotate.Angle); |
||||
|
|
||||
|
Assert.Equal(TransformOperation.OperationType.Skew, operations[3].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(6), operations[3].Data.Skew.X); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(7), operations[3].Data.Skew.Y); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Can_Parse_Matrix_Operation() |
||||
|
{ |
||||
|
var data = "matrix(1,2,3,4,5,6)"; |
||||
|
|
||||
|
var transform = TransformOperations.Parse(data); |
||||
|
|
||||
|
var operations = transform.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Matrix, operations[0].Type); |
||||
|
|
||||
|
var expectedMatrix = new Matrix(1, 2, 3, 4, 5, 6); |
||||
|
|
||||
|
Assert.Equal(expectedMatrix, operations[0].Matrix); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(0d, 10d, 0d)] |
||||
|
[InlineData(0.5d, 5d, 10d)] |
||||
|
[InlineData(1d, 0d, 20d)] |
||||
|
public void Can_Interpolate_Translation(double progress, double x, double y) |
||||
|
{ |
||||
|
var from = TransformOperations.Parse("translateX(10px)"); |
||||
|
var to = TransformOperations.Parse("translateY(20px)"); |
||||
|
|
||||
|
var interpolated = TransformOperations.Interpolate(from, to, progress); |
||||
|
|
||||
|
var operations = interpolated.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Translate, operations[0].Type); |
||||
|
Assert.Equal(x, operations[0].Data.Translate.X); |
||||
|
Assert.Equal(y, operations[0].Data.Translate.Y); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(0d, 10d, 1d)] |
||||
|
[InlineData(0.5d, 5.5d, 10.5d)] |
||||
|
[InlineData(1d, 1d, 20d)] |
||||
|
public void Can_Interpolate_Scale(double progress, double x, double y) |
||||
|
{ |
||||
|
var from = TransformOperations.Parse("scaleX(10)"); |
||||
|
var to = TransformOperations.Parse("scaleY(20)"); |
||||
|
|
||||
|
var interpolated = TransformOperations.Interpolate(from, to, progress); |
||||
|
|
||||
|
var operations = interpolated.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type); |
||||
|
Assert.Equal(x, operations[0].Data.Scale.X); |
||||
|
Assert.Equal(y, operations[0].Data.Scale.Y); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(0d, 10d, 0d)] |
||||
|
[InlineData(0.5d, 5d, 10d)] |
||||
|
[InlineData(1d, 0d, 20d)] |
||||
|
public void Can_Interpolate_Skew(double progress, double x, double y) |
||||
|
{ |
||||
|
var from = TransformOperations.Parse("skewX(10deg)"); |
||||
|
var to = TransformOperations.Parse("skewY(20deg)"); |
||||
|
|
||||
|
var interpolated = TransformOperations.Interpolate(from, to, progress); |
||||
|
|
||||
|
var operations = interpolated.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Skew, operations[0].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(x), operations[0].Data.Skew.X); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(y), operations[0].Data.Skew.Y); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(0d, 10d)] |
||||
|
[InlineData(0.5d, 15d)] |
||||
|
[InlineData(1d,20d)] |
||||
|
public void Can_Interpolate_Rotation(double progress, double angle) |
||||
|
{ |
||||
|
var from = TransformOperations.Parse("rotate(10deg)"); |
||||
|
var to = TransformOperations.Parse("rotate(20deg)"); |
||||
|
|
||||
|
var interpolated = TransformOperations.Interpolate(from, to, progress); |
||||
|
|
||||
|
var operations = interpolated.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Rotate, operations[0].Type); |
||||
|
Assert.Equal(MathUtilities.Deg2Rad(angle), operations[0].Data.Rotate.Angle); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Interpolation_Fallback_To_Matrix() |
||||
|
{ |
||||
|
double progress = 0.5d; |
||||
|
|
||||
|
var from = TransformOperations.Parse("rotate(45deg)"); |
||||
|
var to = TransformOperations.Parse("translate(100px, 100px) rotate(1215deg)"); |
||||
|
|
||||
|
var interpolated = TransformOperations.Interpolate(from, to, progress); |
||||
|
|
||||
|
var operations = interpolated.Operations; |
||||
|
|
||||
|
Assert.Single(operations); |
||||
|
Assert.Equal(TransformOperation.OperationType.Matrix, operations[0].Type); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue