MoonTools.Structs/Structs/Transform2D.cs

160 lines
4.3 KiB
C#
Raw Normal View History

2019-09-06 07:13:49 +00:00
using System;
2019-10-31 22:57:04 +00:00
using System.Numerics;
2019-09-06 07:13:49 +00:00
namespace MoonTools.Core.Structs
{
public struct Transform2D : IEquatable<Transform2D>
{
private static Transform2D _defaultTransform = new Transform2D
{
Position = Position2D.Zero,
Rotation = 0,
Scale = new Vector2(1, 1),
};
private Position2D _position;
private float _rotation;
private Vector2 _scale;
2019-10-31 22:57:04 +00:00
public Matrix4x4 TransformMatrix { get; private set; }
2019-09-06 07:13:49 +00:00
public Position2D Position
{
get
{
return _position;
}
set
{
_position = value;
TransformMatrix = CreateTransformMatrix(value, Rotation, Scale);
}
}
public float Rotation
{
get
{
return _rotation;
}
set
{
_rotation = value;
TransformMatrix = CreateTransformMatrix(Position, value, Scale);
}
}
public Vector2 Scale
{
get
{
return _scale;
}
set
{
_scale = value;
TransformMatrix = CreateTransformMatrix(Position, Rotation, value);
}
}
public static Transform2D DefaultTransform
{
get
{
2019-09-06 07:13:49 +00:00
return _defaultTransform;
}
}
public Transform2D(Position2D position)
{
2019-09-06 07:13:49 +00:00
_position = position;
_rotation = 0f;
_scale = new Vector2(1, 1);
TransformMatrix = CreateTransformMatrix(position, _rotation, _scale);
}
public Transform2D(Vector2 position)
{
_position = new Position2D(position.X, position.Y);
_rotation = 0;
_scale = new Vector2(1, 1);
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
}
public Transform2D(Position2D position, float rotation)
{
2019-09-06 07:13:49 +00:00
_position = position;
_rotation = rotation;
_scale = new Vector2(1, 1);
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
}
public Transform2D(Vector2 position, float rotation)
{
_position = new Position2D(position.X, position.Y);
_rotation = rotation;
_scale = new Vector2(1, 1);
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
}
public Transform2D(Position2D position, float rotation, Vector2 scale)
{
2019-09-06 07:13:49 +00:00
_position = position;
_rotation = rotation;
_scale = scale;
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
}
public Transform2D(Vector2 position, float rotation, Vector2 scale)
{
2019-09-06 07:13:49 +00:00
_position = new Position2D(position.X, position.Y);
_rotation = rotation;
_scale = scale;
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
}
public Transform2D Compose(Transform2D other)
{
2019-09-06 07:13:49 +00:00
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
}
2019-10-31 22:57:04 +00:00
private static Matrix4x4 CreateTransformMatrix(Position2D translation, float rotation, Vector2 scale)
{
2019-10-31 22:57:04 +00:00
return Matrix4x4.CreateScale(scale.X, scale.Y, 1) *
Matrix4x4.CreateRotationZ(rotation) *
Matrix4x4.CreateTranslation(translation.X, translation.Y, 0);
}
public override bool Equals(Object other)
{
if (other is Transform2D otherTransform)
{
return Equals(otherTransform);
}
return false;
}
2019-09-06 07:13:49 +00:00
public bool Equals(Transform2D other)
{
return TransformMatrix == other.TransformMatrix;
}
public override int GetHashCode()
2019-09-06 07:13:49 +00:00
{
return TransformMatrix.GetHashCode();
}
public static bool operator ==(Transform2D a, Transform2D b)
{
return a.Equals(b);
}
public static bool operator !=(Transform2D a, Transform2D b)
{
return !(a == b);
2019-09-06 07:13:49 +00:00
}
}
}