namespace UIWidgets { using UnityEngine; /// /// Base class for the SnapGrid. /// public abstract partial class SnapGridBase : UIBehaviourConditional { /// /// Point. /// public struct Point { /// /// X. /// public float X; /// /// Y. /// public float Y; /// /// Point on the left side. /// public bool Left; /// /// Point on the right side. /// public bool Right; /// /// Point on the top side. /// public bool Top; /// /// Point on the bottom side. /// public bool Bottom; /// /// Initializes a new instance of the struct. /// /// X. /// Y. /// Point on the left side. /// Point on the top side. public Point(float x, float y, bool left, bool top) { X = x; Y = y; Left = left; Top = top; Right = !left; Bottom = !top; } /// /// Initializes a new instance of the struct. /// /// Target. /// Snap grid. /// Point on the left side. /// Point on the top side. public Point(RectTransform target, SnapGridBase snapGrid, bool left, bool top) { var grid_rt = snapGrid.RectTransform; var angle = 360f - (target.rotation.eulerAngles.z - grid_rt.rotation.eulerAngles.z); if (angle >= 360f) { angle -= 360f; } else if (angle <= -360f) { angle += 360f; } var angle_rad = angle * Mathf.Deg2Rad; var size = target.rect.size; var target_scale = target.lossyScale; var grid_scale = grid_rt.lossyScale; size.x *= target_scale.x / grid_scale.x; size.y *= target_scale.y / grid_scale.y; var sin = Mathf.Sin(angle_rad); var cos = Mathf.Cos(angle_rad); var pivot = new Vector2(left ? 0f : 1f, top ? 1f : 0f) - target.pivot; var dx = (size.x * pivot.x * cos) + (size.y * pivot.y * sin); var dy = (size.y * pivot.y * cos) - (size.x * pivot.x * sin); var position = target.position; X = (position.x / target_scale.x) + dx; Y = (position.y / target_scale.y) + dy; Left = left; Top = top; Right = !left; Bottom = !top; } /// /// Converts this instance to the string. /// /// String. public override string ToString() { var args = new object[] { X.ToString(), Y.ToString(), Left.ToString(), Top.ToString(), }; return string.Format("X: {0}; Y: {1}; left: {2}; top: {3}", args); } } } }