Example #1
0
        /// <summary>
        /// Computes the relative anchor in local space given a resolution.
        /// </summary>
        /// <param name="res">The element's parent's current dimension</param><z
        /// <returns>The anchor in local space</returns>
        public static int2 AnchoredToRelative(this AnchoredState state, int2 res)
        {
            switch (state)
            {
            case AnchoredState.BottomLeft:
                return(-res / 2);

            case AnchoredState.MiddleLeft:
                return(new int2(-res.x / 2, 0));

            case AnchoredState.TopLeft:
                return(new int2(-res.x / 2, res.y / 2));

            case AnchoredState.BottomCenter:
                return(new int2(0, -res.y / 2));

            case AnchoredState.MiddleCenter:
                return(new int2(0, 0));

            case AnchoredState.TopCenter:
                return(new int2(0, res.y / 2));

            case AnchoredState.BottomRight:
                return(new int2(res.x / 2, -res.y / 2));

            case AnchoredState.MiddleRight:
                return(new int2(res.x / 2, 0));

            case AnchoredState.TopRight:
                return(res / 2);

            default:
                throw new System.ArgumentException($"{state} is not a valid anchor!");
            }
        }
Example #2
0
        /// <summary>
        /// Returns the position that the anchor is supposedly anchored to.
        /// </summary>
        /// <param name="state">The current state of the anchor.</param>
        /// <param name="res">The current resolution we want to consider.</param>
        /// <returns>The relative screenspace position that the anchor is referencing.</returns>
        public static int2 AnchoredTo(this AnchoredState state, int2 res)
        {
            switch (state)
            {
            case AnchoredState.LeftColumn | AnchoredState.BottomRow:
                return(default);

            case AnchoredState.LeftColumn | AnchoredState.MiddleRow:
                return(new int2(0, res.y / 2));

            case AnchoredState.LeftColumn | AnchoredState.TopRow:
                return(new int2(0, res.y));

            case AnchoredState.MiddleColumn | AnchoredState.BottomRow:
                return(new int2(res.x / 2, 0));

            case AnchoredState.MiddleColumn | AnchoredState.MiddleRow:
                return(res / 2);

            case AnchoredState.MiddleColumn | AnchoredState.TopRow:
                return(new int2(res.x / 2, res.y));

            case AnchoredState.RightColumn | AnchoredState.BottomRow:
                return(new int2(res.x, 0));

            case AnchoredState.RightColumn | AnchoredState.MiddleRow:
                return(new int2(res.x, res.y / 2));

            case AnchoredState.RightColumn | AnchoredState.TopRow:
                return(res);

            default:
                throw new System.ArgumentException($"{state} is not a valid state to anchor to!");
            }
        }
Example #3
0
        /// <summary>
        /// Transforms a rect transform's anchor preset to an enum.
        /// </summary>
        public static AnchoredState ToAnchor(this RectTransform transform)
        {
            var min = transform.anchorMin;

            AnchoredState anchor = default;

            { // Bottom Row
                if (min == default)
                {
                    anchor = AnchoredState.BottomLeft;
                }

                if (min == new Vector2(0.5f, 0f))
                {
                    anchor = AnchoredState.BottomCenter;
                }

                if (min == new Vector2(1f, 0f))
                {
                    anchor = AnchoredState.BottomRight;
                }
            }

            { // Middle Row
                if (min == new Vector2(0f, 0.5f))
                {
                    anchor = AnchoredState.MiddleLeft;
                }

                if (min == new Vector2(0.5f, 0.5f))
                {
                    anchor = AnchoredState.MiddleCenter;
                }

                if (min == new Vector2(1f, 0.5f))
                {
                    anchor = AnchoredState.MiddleRight;
                }
            }

            { // Top Row
                if (min == new Vector2(0f, 1f))
                {
                    anchor = AnchoredState.TopLeft;
                }

                if (min == new Vector2(0.5f, 1f))
                {
                    anchor = AnchoredState.TopCenter;
                }

                if (min == new Vector2(1f, 1f))
                {
                    anchor = AnchoredState.TopRight;
                }
            }

            return(anchor);
        }
Example #4
0
        public static bool IsAtColumn(this AnchoredState state, AnchoredState column)
        {
            switch (state)
            {
            case var _ when IsAtRow(state, AnchoredState.TopRow):
                return((state.ShiftRightBy(2) & (int)column) > 0);

            case var _ when IsAtRow(state, AnchoredState.MiddleRow):
                return((state.ShiftRightBy(1) & (int)column) > 0);

            case var _ when IsAtRow(state, AnchoredState.BottomRow):
                return((state & column) > 0);

            default:
                return(false);
            }
        }
Example #5
0
        public static AnchoredState ToAnchor(this RectTransform transform)
        {
            var min = transform.anchorMin;

            AnchoredState anchor = 0;

            switch (min.x)
            {
            case 0f:
                anchor = AnchoredState.LeftColumn;
                break;

            case 0.5f:
                anchor = AnchoredState.MiddleColumn;
                break;

            case 1f:
                anchor = AnchoredState.RightColumn;
                break;

            default:
                throw new System.ArgumentException($"Cannot map: {min.x} as a valid AnchoredState!");
            }

            switch (min.y)
            {
            case 0f:
                anchor |= AnchoredState.BottomRow;
                break;

            case 0.5f:
                anchor |= AnchoredState.MiddleRow;
                break;

            case 1f:
                anchor |= AnchoredState.RightColumn;
                break;

            default:
                throw new System.ArgumentException($"Cannot map: {min.y} as a valid AnchoredState!");
            }

            return(anchor);
        }
Example #6
0
 public static void AnchorOf(this RectTransform transform, out float2 distance, out AnchoredState anchor)
 {
     anchor   = transform.ToAnchor();
     distance = anchor.AnchoredTo() - new float2(transform.position.x, transform.position.y);
 }
Example #7
0
        public static int2 AnchoredTo(this AnchoredState state)
        {
            var res = new int2(Screen.width, Screen.height);

            return(state.AnchoredTo(res));
        }
Example #8
0
        internal static int ShiftRightBy(this AnchoredState state, int shift)
        {
            var value = (int)state;

            return(value >> shift);
        }
Example #9
0
 public static bool IsAtRow(this AnchoredState lhs, AnchoredState rhs)
 {
     return((lhs & rhs) > 0);
 }