namespace UIWidgets { using UnityEngine; /// /// Base class for the SnapGrid. /// public abstract partial class SnapGridBase : UIBehaviourConditional { /// /// Distance. /// public struct Distance { DistanceValue left; DistanceValue right; DistanceValue top; DistanceValue bottom; /// /// Initializes a new instance of the struct. /// /// Default value. public Distance(float defaultValue) { left = new DistanceValue(defaultValue); right = new DistanceValue(defaultValue); top = new DistanceValue(defaultValue); bottom = new DistanceValue(defaultValue); } /// /// Update distance at left direction. /// /// New value. public void Left(float value) { left.Update(value); } /// /// Update distance at right direction. /// /// New value. public void Right(float value) { right.Update(value); } /// /// Update distance at top direction. /// /// New value. public void Top(float value) { top.Update(value); } /// /// Update distance at bottom direction. /// /// New value. public void Bottom(float value) { bottom.Update(value); } float X(float snapDistance, out bool snapped) { if (left.Abs < right.Abs) { if (left.Abs <= snapDistance) { snapped = true; return left.Value; } } else { if (right.Abs <= snapDistance) { snapped = true; return right.Value; } } snapped = false; return 0f; } float Y(float snapDistance, out bool snapped) { if (top.Abs < bottom.Abs) { if (top.Abs <= snapDistance) { snapped = true; return top.Value; } } else { if (bottom.Abs <= snapDistance) { snapped = true; return bottom.Value; } } snapped = false; return 0f; } /// /// Snap. /// /// Snap distance. /// Distance to the nearest lines. public Result Snap(Vector2 snapDistance) { bool snappedX; bool snappedY; var delta = new Vector2(X(snapDistance.x, out snappedX), Y(snapDistance.y, out snappedY)); return new Result(delta, snappedX, snappedY); } /// /// Converts this instance to the string. /// /// String. public override string ToString() { var args = new object[] { left.Value.ToString(), right.Value.ToString(), top.Value.ToString(), bottom.Value.ToString() }; return string.Format("left: {0}; right: {1}; top: {2}; bottom: {3}", args); } } } }