Skip to content

Commit f932647

Browse files
authored
feat(scene): implement Drawable base class, transforms, Anchor/Origin, and invalidation (#88)
1 parent e15e9b4 commit f932647

7 files changed

Lines changed: 1494 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using kajarlabs.Framework.Numerics;
2+
3+
namespace kajarlabs.Framework.Scene;
4+
5+
/// <summary>
6+
/// Stores accumulated color and alpha state inherited through a <see cref="Drawable"/> hierarchy.
7+
/// </summary>
8+
public readonly struct DrawColorInfo : IEquatable<DrawColorInfo>
9+
{
10+
/// <summary>
11+
/// Gets an opaque white <see cref="DrawColorInfo"/>.
12+
/// </summary>
13+
public static readonly DrawColorInfo WHITE = new(Color4.WHITE);
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="DrawColorInfo"/> struct.
17+
/// </summary>
18+
public DrawColorInfo(Color4 color)
19+
{
20+
Color = color;
21+
}
22+
23+
/// <summary>
24+
/// Gets the accumulated RGBA color.
25+
/// </summary>
26+
public Color4 Color { get; }
27+
28+
/// <summary>
29+
/// Determines whether two <see cref="DrawColorInfo"/> values are equal.
30+
/// </summary>
31+
public static bool operator ==(DrawColorInfo left, DrawColorInfo right) => left.Equals(right);
32+
33+
/// <summary>
34+
/// Determines whether two <see cref="DrawColorInfo"/> values are not equal.
35+
/// </summary>
36+
public static bool operator !=(DrawColorInfo left, DrawColorInfo right) => !left.Equals(right);
37+
38+
/// <summary>
39+
/// Returns a new <see cref="DrawColorInfo"/> combining this color with a child's local <paramref name="localColor"/>
40+
/// and <paramref name="localAlpha"/>.
41+
/// </summary>
42+
public DrawColorInfo Multiply(Color4 localColor, float localAlpha)
43+
=> new(Color * localColor.MultiplyAlpha(localAlpha));
44+
45+
/// <inheritdoc />
46+
public bool Equals(DrawColorInfo other) => Color.Equals(other.Color);
47+
48+
/// <inheritdoc />
49+
public override bool Equals(object? obj) => obj is DrawColorInfo other && Equals(other);
50+
51+
/// <inheritdoc />
52+
public override int GetHashCode() => Color.GetHashCode();
53+
}

‎libs/framework/Scene/DrawInfo.cs‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Numerics;
2+
using kajarlabs.Framework.Numerics;
3+
4+
namespace kajarlabs.Framework.Scene;
5+
6+
/// <summary>
7+
/// Stores a cached draw-space 2D affine transform matrix and its inverse.
8+
/// </summary>
9+
public readonly struct DrawInfo : IEquatable<DrawInfo>
10+
{
11+
/// <summary>
12+
/// Gets an identity <see cref="DrawInfo"/> with identity matrix and inverse.
13+
/// </summary>
14+
public static readonly DrawInfo IDENTITY = new(Matrix3x2.Identity, Matrix3x2.Identity, isInvertible: true);
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="DrawInfo"/> struct.
18+
/// </summary>
19+
public DrawInfo(Matrix3x2 matrix, Matrix3x2 matrixInverse, bool isInvertible)
20+
{
21+
Matrix = matrix;
22+
MatrixInverse = matrixInverse;
23+
IsInvertible = isInvertible;
24+
}
25+
26+
/// <summary>
27+
/// Gets the local-to-draw-space row-vector affine matrix.
28+
/// </summary>
29+
public Matrix3x2 Matrix { get; }
30+
31+
/// <summary>
32+
/// Gets the cached draw-space-to-local inverse matrix, or <see cref="Matrix3x2.Identity"/> when <see cref="IsInvertible"/> is <see langword="false"/>.
33+
/// </summary>
34+
public Matrix3x2 MatrixInverse { get; }
35+
36+
/// <summary>
37+
/// Gets a value indicating whether <see cref="Matrix"/> is finite and non-singular.
38+
/// </summary>
39+
public bool IsInvertible { get; }
40+
41+
/// <summary>
42+
/// Creates a <see cref="DrawInfo"/> from <paramref name="matrix"/>, computing its inverse when non-singular.
43+
/// </summary>
44+
public static DrawInfo Create(Matrix3x2 matrix)
45+
{
46+
var isInvertible = matrix.TryInvert(out var inverse);
47+
return new DrawInfo(matrix, isInvertible ? inverse : Matrix3x2.Identity, isInvertible);
48+
}
49+
50+
/// <summary>
51+
/// Determines whether two <see cref="DrawInfo"/> values are equal.
52+
/// </summary>
53+
public static bool operator ==(DrawInfo left, DrawInfo right) => left.Equals(right);
54+
55+
/// <summary>
56+
/// Determines whether two <see cref="DrawInfo"/> values are not equal.
57+
/// </summary>
58+
public static bool operator !=(DrawInfo left, DrawInfo right) => !left.Equals(right);
59+
60+
/// <summary>
61+
/// Composes <paramref name="localTransform"/> onto this parent <see cref="DrawInfo"/> in row-vector order
62+
/// (<c>localTransform * Matrix</c>).
63+
/// </summary>
64+
public DrawInfo ApplyLocal(Matrix3x2 localTransform)
65+
=> Create(localTransform * Matrix);
66+
67+
/// <inheritdoc />
68+
public bool Equals(DrawInfo other)
69+
=> IsInvertible == other.IsInvertible
70+
&& Matrix.Equals(other.Matrix)
71+
&& MatrixInverse.Equals(other.MatrixInverse);
72+
73+
/// <inheritdoc />
74+
public override bool Equals(object? obj) => obj is DrawInfo other && Equals(other);
75+
76+
/// <inheritdoc />
77+
public override int GetHashCode() => HashCode.Combine(Matrix, MatrixInverse, IsInvertible);
78+
}

0 commit comments

Comments
 (0)