Browse Source

Cleanup and add comments.

pull/4092/head
Dariusz Komosinski 6 years ago
parent
commit
0f6bf8d9ba
  1. 11
      src/Avalonia.Visuals/Matrix.cs
  2. 43
      src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs
  3. 8
      src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs

11
src/Avalonia.Visuals/Matrix.cs

@ -9,8 +9,6 @@ namespace Avalonia
/// </summary>
public readonly struct Matrix : IEquatable<Matrix>
{
private const float DecomposeEpsilon = 0.0001f;
private readonly double _m11;
private readonly double _m12;
private readonly double _m21;
@ -322,14 +320,19 @@ namespace Avalonia
}
}
/// <summary>
/// Decomposes given matrix into transform operations.
/// </summary>
/// <param name="matrix">Matrix to decompose.</param>
/// <param name="decomposed">Decomposed matrix.</param>
/// <returns>The status of the operation.</returns>
public static bool TryDecomposeTransform(Matrix matrix, out Decomposed decomposed)
{
decomposed = default;
var determinant = matrix.GetDeterminant();
// Based upon constant in System.Numerics.Matrix4x4.
if (Math.Abs(determinant) < DecomposeEpsilon)
if (MathUtilities.IsZero(determinant))
{
return false;
}

43
src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs

@ -2,6 +2,9 @@ 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;
@ -18,8 +21,14 @@ namespace Avalonia.Media.Transformation
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;
@ -53,11 +62,22 @@ namespace Avalonia.Media.Transformation
}
}
public static bool IsOperationIdentity(ref TransformOperation? operation)
{
return !operation.HasValue || operation.Value.IsIdentity;
}
/// <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)
{
@ -69,8 +89,10 @@ namespace Avalonia.Media.Transformation
return true;
}
TransformOperation fromValue = fromIdentity ? default : from.Value;
TransformOperation toValue = toIdentity ? default : to.Value;
// 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;
@ -139,7 +161,7 @@ namespace Avalonia.Media.Transformation
{
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))
{
@ -165,6 +187,11 @@ namespace Avalonia.Media.Transformation
return true;
}
private static bool IsOperationIdentity(ref TransformOperation? operation)
{
return !operation.HasValue || operation.Value.IsIdentity;
}
[StructLayout(LayoutKind.Explicit)]
public struct DataLayout
{

8
src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
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>());
@ -19,6 +22,9 @@ namespace Avalonia.Media.Transformation
Value = ApplyTransforms();
}
/// <summary>
/// Returns whether all operations combined together produce the identity matrix.
/// </summary>
public bool IsIdentity { get; }
public IReadOnlyList<TransformOperation> Operations => _operations;

Loading…
Cancel
Save