void AutoAnchor(Vector2 worldPosition, Vector2 border, out HorizontalAnchor horizontal, out VerticalAnchor vertical) { // Anchor position depends on world position and screen borders. // The tooltip should overall be as centered as possible. horizontal = HorizontalAnchor.Center; if (worldPosition.x < m_WorldSpaceScreenRect.xMin + border.x) { horizontal = HorizontalAnchor.Left; } if (worldPosition.x > m_WorldSpaceScreenRect.xMax - border.x) { horizontal = HorizontalAnchor.Right; } // Center-Center is not a valid combination. vertical = horizontal == HorizontalAnchor.Center ? (worldPosition.y > m_WorldSpaceScreenRect.height * 0.5f ? VerticalAnchor.Top : VerticalAnchor.Bottom) : VerticalAnchor.Center; if (worldPosition.y < m_WorldSpaceScreenRect.yMin + border.y) { vertical = VerticalAnchor.Bottom; } if (worldPosition.y > m_WorldSpaceScreenRect.yMax - border.y) { vertical = VerticalAnchor.Top; } }
public TransformationComponent(Vector2 position, int widthInPixels, int heightInPixels, HorizontalAnchor horizontalAnchor, VerticalAnchor verticalAnchor) { this.Z = 0f; this.HorizontalAnchor = horizontalAnchor; this.VerticalAnchor = verticalAnchor; _originalWidth = (float)widthInPixels; _originalHeight = (float)heightInPixels; this.Position = position; this.PositionOffset = Vector2.Zero; CalculateBounds(); }
public RectTransformSpec( Vector2 size = new Vector2(), TextAnchor pivot = TextAnchor.MiddleCenter, Vector2 position = new Vector2(), VerticalAnchor verticalAnchor = VerticalAnchor.Top, HorizontalAnchor horizontalAnchor = HorizontalAnchor.Left) { Size = size; Position = position; var anchors = AnchorValues(verticalAnchor, horizontalAnchor); AnchorMin = anchors.Item1; AnchorMax = anchors.Item2; Pivot = PivotValue(pivot); EulerRotation = Vector2.zero; Scale = Vector3.one; }
public void SetStartFrame(Position textureOffset, Size size, Position centerPosition = null, float initialScale = 1.0f, bool mirrorX = false, Size customTextureSize = null, HorizontalAnchor anchorX = HorizontalAnchor.Center, VerticalAnchor anchorY = VerticalAnchor.Center) { if (centerPosition != null) { baseSpriteLocation = new Position(centerPosition); } baseSpriteSize = new Size(size); baseTextureCoords = new Position(textureOffset); sprite.TextureAtlasOffset = new Position(textureOffset); sprite.TextureSize = customTextureSize ?? baseSpriteSize; sprite.MirrorX = mirrorX; AnchorX = anchorX; AnchorY = anchorY; Scale = startScale = initialScale; }
// Rotation applied to an arrow pointing upwards at rotation_z=0 static float ArrowRotation(HorizontalAnchor horizontalAnchor, VerticalAnchor verticalAnchor) { switch (verticalAnchor) { case VerticalAnchor.Top: return(0); case VerticalAnchor.Bottom: return(180); case VerticalAnchor.Center: { switch (horizontalAnchor) { case HorizontalAnchor.Left: return(90); case HorizontalAnchor.Right: return(-90); default: return(0); } } default: return(0); } }
// Offset applied to an arrow with a central pivot. static Vector2 ArrowOffsetDirection(HorizontalAnchor horizontalAnchor, VerticalAnchor verticalAnchor) { var val = Vector2.zero; switch (horizontalAnchor) { case HorizontalAnchor.Left: val.x = 1; break; case HorizontalAnchor.Center: val.x = 0; break; case HorizontalAnchor.Right: val.x = -1; break; } switch (verticalAnchor) { case VerticalAnchor.Bottom: val.y = -1; break; case VerticalAnchor.Center: val.x *= -1; val.y = 0; break; case VerticalAnchor.Top: val.y = 1; break; } return(val); }
public void SetPosition(HorizontalAnchor anchorX, float offsetX, VerticalAnchor anchorY, float offsetY) { this.anchorX = anchorX; this.anchorY = anchorY; absXoffset = offsetX; absYoffset = offsetY; recalculatePosition(); }
public ButtonElem Button(string name, int x, int y, Visual up, Visual down = null, VerticalAnchor vertAnchor = VerticalAnchor.Top, HorizontalAnchor horAnchor = HorizontalAnchor.Left) { if (horAnchor == HorizontalAnchor.Center) { x -= (int)(up.Width * 0.5f); } if (horAnchor == HorizontalAnchor.Right) { x -= up.Width; } if (vertAnchor == VerticalAnchor.Center) { y += (int)(up.Height * 0.5f); } if (vertAnchor == VerticalAnchor.Bottom) { y -= up.Height; } var button = new ButtonElem(x, y, up, down); mElems.Add(name, button); return(button); }
private static Tuple <Vector2, Vector2> AnchorValues( VerticalAnchor verticalAnchor, HorizontalAnchor horizontalAnchor) { float minX; float minY; float maxX; float maxY; switch (horizontalAnchor) { case HorizontalAnchor.Right: minX = 1f; maxX = 1f; break; case HorizontalAnchor.Center: minX = 0.5f; maxX = 0.5f; break; case HorizontalAnchor.Left: minX = 0f; maxX = 0f; break; case HorizontalAnchor.Stretch: minX = 0f; maxX = 1f; break; default: throw Errors.UnknownEnumValue(verticalAnchor); } switch (verticalAnchor) { case VerticalAnchor.Top: minY = 1f; maxY = 1f; break; case VerticalAnchor.Middle: minY = 0.5f; maxY = 0.5f; break; case VerticalAnchor.Bottom: minY = 0f; maxY = 0f; break; case VerticalAnchor.Stretch: minY = 0f; maxY = 1f; break; default: throw Errors.UnknownEnumValue(verticalAnchor); } return(Tuple.Create(new Vector2(minX, minY), new Vector2(maxX, maxY))); }
/// <summary> /// Returns a rectangle of the given width and height, anchored as instructed to the Screen rect. /// </summary> /// <param name="width">Desired Width</param> /// <param name="height">Desired Height</param> /// <param name="verticalAlignment">Vertical placement</param> /// <param name="horizontalAlignment">Horizontal placement</param> /// <returns></returns> public static Rect GiveRect(float width, float height, VerticalAnchor verticalAlignment, HorizontalAnchor horizontalAlignment, float vOffset = 0f, float hOffset = 0f) { var screen = ScreenRect; float x = 0; float y = 0; if (verticalAlignment == VerticalAnchor.Bottom) { y = screen.yMax - height; } else if (verticalAlignment == VerticalAnchor.Center) { y = screen.center.y - height / 2; } if (horizontalAlignment == HorizontalAnchor.Right) { x = screen.xMax - width; } else if (horizontalAlignment == HorizontalAnchor.Center) { x = screen.center.x - width / 2; } x += hOffset; y += vOffset; return(new Rect(x, y, width, height)); }
/// <summary> /// Places the rectangle inside the given rectangle according to the given Anchors; /// if no anchors are given the rect stays the same. /// </summary> /// <param name="r"></param> /// <param name="other"></param> /// <param name="verticalAlignment"></param> /// <param name="horizontalAlignment"></param> public static Rect Place(this Rect r, Rect other, VerticalAnchor verticalAlignment = VerticalAnchor.NA, HorizontalAnchor horizontalAlignment = HorizontalAnchor.NA) { var x = r.x; var y = r.y; var width = r.width; var height = r.height; if (verticalAlignment == VerticalAnchor.Top) { y = other.yMin; } else if (verticalAlignment == VerticalAnchor.Bottom) { y = other.yMax - r.height; } else if (verticalAlignment == VerticalAnchor.Center) { y = other.center.y - r.height / 2; } if (horizontalAlignment == HorizontalAnchor.Left) { x = other.xMin; } else if (horizontalAlignment == HorizontalAnchor.Right) { x = other.xMax - r.width; } else if (horizontalAlignment == HorizontalAnchor.Center) { x = other.center.x - r.width / 2; } return(new Rect(x, y, width, height)); }
/// <summary> /// Places the rectangle on the screen according to the given Anchors. /// If no anchors are provided the rect stays the same. /// </summary> /// <param name="r"></param> /// <param name="verticalAlignment"></param> /// <param name="horizontalAlignment"></param> public static Rect Place(this Rect r, VerticalAnchor verticalAlignment = VerticalAnchor.NA, HorizontalAnchor horizontalAlignment = HorizontalAnchor.NA) { return(r.Place(ScreenRect, verticalAlignment, horizontalAlignment)); }