Exemple #1
0
 /// <summary>
 /// Sets the value of the <see cref="AnchorProperty"/> property.
 /// </summary>
 /// <param name="d">The element.</param>
 /// <param name="anchor">The value to set.</param>
 public static void SetAnchor(UIElement d, PositionAnchor anchor)
 {
     d.SetValue(AnchorProperty, anchor);
 }
        /// <summary>Get the top-left pixel coordinate for a rectangle so that it renders flush against the given anchor position, with an optional offset.</summary>
        /// <param name="offsetX">The X pixel offset from the anchor position, with positive numbers moving inwards from the screen edge.</param>
        /// <param name="offsetY">The Y pixel offset from the anchor position, with positive numbers moving inwards from the screen edge.</param>
        /// <param name="width">The rectangle width to position.</param>
        /// <param name="height">The rectangle height to position.</param>
        /// <param name="anchor">The screen position from which to offset the rectangle.</param>
        /// <param name="uiScale">Whether to apply UI scaling, or <c>null</c> to use the game's current UI scaling mode.</param>
        /// <param name="zoom">The zoom factor to apply to the <paramref name="width"/> and <paramref name="height"/> values.</param>
        public static Vector2 GetPositionFromAnchor(int offsetX, int offsetY, int width, int height, PositionAnchor anchor, bool?uiScale = null, int zoom = Game1.pixelZoom)
        {
            var screen = uiScale ?? Game1.uiMode
                ? Game1.uiViewport
                : Game1.viewport;

            if (anchor is PositionAnchor.BottomRight or PositionAnchor.TopRight)
            {
                offsetX = screen.Width - (width * zoom) - offsetX;
            }
            if (anchor is PositionAnchor.BottomLeft or PositionAnchor.BottomRight)
            {
                offsetY = screen.Height - (height * zoom) - offsetY;
            }

            return(new Vector2(offsetX, offsetY));
        }
 public static Rect GetRect(Vector2 size, PositionAnchor anchor, Vector2 parent)
 {
     return(GetRect(size, anchor, parent, Vector2.zero));
 }
        /* Возвращает Rect окна с привязкой к размерам родительского и относительной позиции */
        public static Rect GetRect(Vector2 size, PositionAnchor anchor, Vector2 parent, Vector2 positionRelAnchor)
        {
            Vector2 position = new Vector2();

            switch (anchor)
            {
            case PositionAnchor.Center:
            {
                position.x = (parent.x - size.x) / 2 + positionRelAnchor.x;
                position.y = (parent.y - size.y) / 2 + positionRelAnchor.y;
                break;
            }

            case PositionAnchor.Down:
            {
                position.x = (parent.x - size.x) / 2 + positionRelAnchor.x;
                position.y = (parent.y - size.y) + positionRelAnchor.y;
                break;
            }

            case PositionAnchor.Up:
            {
                position.x = (parent.x - size.x) / 2 + positionRelAnchor.x;
                position.y = positionRelAnchor.y;
                break;
            }

            case PositionAnchor.Left:
            {
                position.x = positionRelAnchor.x;
                position.y = (parent.y - size.y) / 2 + positionRelAnchor.y;
                break;
            }

            case PositionAnchor.Right:
            {
                position.x = (parent.x - size.x) + positionRelAnchor.x;
                position.y = (parent.y - size.y) / 2 + positionRelAnchor.y;
                break;
            }

            case PositionAnchor.LeftUp:
            {
                position.x = positionRelAnchor.x;
                position.y = positionRelAnchor.y;
                break;
            }

            case PositionAnchor.LeftDown:
            {
                position.x = positionRelAnchor.x;
                position.y = (parent.y - size.y) + positionRelAnchor.y;
                break;
            }

            case PositionAnchor.RightUp:
            {
                position.x = (parent.x - size.x) + positionRelAnchor.x;
                position.y = positionRelAnchor.y;
                break;
            }

            case PositionAnchor.RightDown:
            {
                position.x = (parent.x - size.x) + positionRelAnchor.x;
                position.y = (parent.y - size.y) + positionRelAnchor.y;
                break;
            }
            }
            return(new Rect(position, size));
        }
 public static Rect GetRect(Vector2 size, PositionAnchor anchor)
 {
     return(GetRect(size, anchor, new Vector2(Screen.width, Screen.height), Vector2.zero));
 }