private static Vector2 GetRealOffset(UIAnchorSide side, float inset, Vector2 baseOffset) { var result = baseOffset; switch (side) { case UIAnchorSide.TopLeft: result.X += inset; result.Y += inset; break; case UIAnchorSide.TopRight: result.X -= inset; result.Y += inset; break; case UIAnchorSide.BottomLeft: result.X += inset; result.Y -= inset; break; case UIAnchorSide.BottomRight: result.X -= inset; result.Y -= inset; break; case UIAnchorSide.Top: result.Y += inset; break; case UIAnchorSide.Bottom: result.Y -= inset; break; case UIAnchorSide.Left: result.X += inset; break; case UIAnchorSide.Right: result.X -= inset; break; case UIAnchorSide.Center: break; default: throw new ArgumentException($"{nameof(UIAnchorSide)} has an invalid value."); } return(result); }
public static UIAnchorSide Opposite(this UIAnchorSide self) { return(self switch { UIAnchorSide.TopLeft => UIAnchorSide.BottomRight, UIAnchorSide.TopRight => UIAnchorSide.BottomLeft, UIAnchorSide.BottomLeft => UIAnchorSide.TopRight, UIAnchorSide.BottomRight => UIAnchorSide.TopLeft, UIAnchorSide.Center => UIAnchorSide.Center, UIAnchorSide.Top => UIAnchorSide.Bottom, UIAnchorSide.Bottom => UIAnchorSide.Top, UIAnchorSide.Left => UIAnchorSide.Right, UIAnchorSide.Right => UIAnchorSide.Left, _ => throw new ArgumentException($"{nameof(UIAnchorSide)} has an invalid value."), });
public UIAnchor(UIAnchorSide from, Vector2 offset, UIAnchorSide to) { this.From = from; this.Offset = offset; this.To = to; }
public UIAnchor(UIAnchorSide from, float inset, Vector2 offset, UIAnchorSide to) : this(from, GetRealOffset(from, inset, offset), to) { }
public void DrawItem(SpriteBatch batch, SObject @object, Vector2 rectLocation, Vector2 rectSize, Color color, UIAnchorSide rectAnchorSide = UIAnchorSide.TopLeft, Vector2?scale = null, float layerDepth = 0f) { var texture = GetItemTexture(@object); var sourceRectangle = GetItemSourceRectangle(@object); var itemSize = new Vector2(sourceRectangle.Size.X, sourceRectangle.Size.Y); if (itemSize.X < rectSize.X && itemSize.Y < rectSize.Y) { itemSize *= Math.Max(rectSize.X, rectSize.Y); } if (itemSize.X > rectSize.X) { itemSize = itemSize / itemSize.X * rectSize.X; } if (itemSize.Y > rectSize.Y) { itemSize = itemSize / itemSize.Y * rectSize.Y; } var itemPosition = rectAnchorSide.GetAnchorPoint( new Vector2( rectLocation.X + (rectSize.X - itemSize.X) * 0.5f, rectLocation.Y + (rectSize.Y - itemSize.Y) * 0.5f ), -rectSize ); var finalScale = itemSize.X / sourceRectangle.Width; var origin = new Vector2(sourceRectangle.Width * 0.5f, sourceRectangle.Height); batch.Draw(texture, itemPosition + origin * finalScale, sourceRectangle, color, 0f, origin, finalScale * (scale ?? Vector2.One), SpriteEffects.None, layerDepth); if (@object is ColoredObject coloredObject) { batch.Draw(texture, itemPosition + origin * finalScale, GetColoredItemSourceRectangle(coloredObject), Color.Lerp(color, coloredObject.color.Value, 0.5f), 0f, origin, finalScale * (scale ?? Vector2.One), SpriteEffects.None, layerDepth); } }