using System; using Unity.UIWidgets.ui; namespace Unity.UIWidgets.painting { public class Alignment : IEquatable { public Alignment(float x, float y) { this.x = x; this.y = y; } public readonly float x; public readonly float y; public static readonly Alignment topLeft = new Alignment(-1.0f, -1.0f); public static readonly Alignment topCenter = new Alignment(0, -1.0f); public static readonly Alignment topRight = new Alignment(1.0f, -1.0f); public static readonly Alignment centerLeft = new Alignment(-1.0f, 0.0f); public static readonly Alignment center = new Alignment(0.0f, 0.0f); public static readonly Alignment centerRight = new Alignment(1.0f, 0.0f); public static readonly Alignment bottomLeft = new Alignment(-1.0f, 1.0f); public static readonly Alignment bottomCenter = new Alignment(0.0f, 1.0f); public static readonly Alignment bottomRight = new Alignment(1.0f, 1.0f); public static Alignment operator -(Alignment a, Alignment b) { return new Alignment(a.x - b.x, a.y - b.y); } public static Alignment operator +(Alignment a, Alignment b) { return new Alignment(a.x + b.x, a.y + b.y); } public static Alignment operator -(Alignment a) { return new Alignment(-a.x, -a.y); } public static Alignment operator *(Alignment a, float b) { return new Alignment(a.x * b, a.y * b); } public static Alignment operator /(Alignment a, float b) { return new Alignment(a.x / b, a.y / b); } public static Alignment operator %(Alignment a, float b) { return new Alignment(a.x % b, a.y % b); } public Offset alongOffset(Offset other) { float centerX = other.dx / 2.0f; float centerY = other.dy / 2.0f; return new Offset(centerX + x * centerX, centerY + y * centerY); } public Offset alongSize(Size other) { float centerX = other.width / 2.0f; float centerY = other.height / 2.0f; return new Offset(centerX + x * centerX, centerY + y * centerY); } public Offset withinRect(Rect rect) { float halfWidth = rect.width / 2.0f; float halfHeight = rect.height / 2.0f; return new Offset( rect.left + halfWidth + x * halfWidth, rect.top + halfHeight + y * halfHeight ); } public Rect inscribe(Size size, Rect rect) { float halfWidthDelta = (rect.width - size.width) / 2.0f; float halfHeightDelta = (rect.height - size.height) / 2.0f; return Rect.fromLTWH( rect.left + halfWidthDelta + x * halfWidthDelta, rect.top + halfHeightDelta + y * halfHeightDelta, size.width, size.height ); } public static Alignment lerp(Alignment a, Alignment b, float t) { if (a == null && b == null) { return null; } if (a == null) { return new Alignment(MathUtils.lerpFloat(0.0f, b.x, t), MathUtils.lerpFloat(0.0f, b.y, t)); } if (b == null) { return new Alignment(MathUtils.lerpFloat(a.x, 0.0f, t), MathUtils.lerpFloat(a.y, 0.0f, t)); } return new Alignment(MathUtils.lerpFloat(a.x, b.x, t), MathUtils.lerpFloat(a.y, b.y, t)); } public bool Equals(Alignment other) { if (ReferenceEquals(null, other)) { return false; } if (ReferenceEquals(this, other)) { return true; } return x.Equals(other.x) && y.Equals(other.y); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (obj.GetType() != GetType()) { return false; } return Equals((Alignment) obj); } public override int GetHashCode() { unchecked { return (x.GetHashCode() * 397) ^ y.GetHashCode(); } } public static bool operator ==(Alignment a, Alignment b) { return Equals(a, b); } public static bool operator !=(Alignment a, Alignment b) { return !(a == b); } public override string ToString() { if (x == -1.0f && y == -1.0f) { return "topLeft"; } if (x == 0.0f && y == -1.0f) { return "topCenter"; } if (x == 1.0f && y == -1.0f) { return "topRight"; } if (x == -1.0f && y == 0.0f) { return "centerLeft"; } if (x == 0.0f && y == 0.0f) { return "center"; } if (x == 1.0f && y == 0.0f) { return "centerRight"; } if (x == -1.0f && y == 1.0f) { return "bottomLeft"; } if (x == 0.0f && y == 1.0f) { return "bottomCenter"; } if (x == 1.0f && y == 1.0f) { return "bottomRight"; } return $"Alignment({x:F1}, {y:F1})"; } } }