/// <summary> /// Refreshes the sprite's position according to its anchor information. /// </summary> /// <param name="sprite"></param> public static void refreshPosition(this IPositionable sprite) { // Get sprite depth var depth = sprite.position.z; // Get anchor info var anchorInfo = sprite.anchorInfo; // Get parent anchor position var position = parentAnchorPosition(anchorInfo.ParentUIObject, anchorInfo.ParentUIyAnchor, anchorInfo.ParentUIxAnchor); // Add position offset if (anchorInfo.UIPrecision == UIPrecision.Percentage) { position.x += UIRelative.xPercentFrom(anchorInfo.UIxAnchor, parentWidth(anchorInfo.ParentUIObject), anchorInfo.OffsetX); position.y -= UIRelative.yPercentFrom(anchorInfo.UIyAnchor, parentHeight(anchorInfo.ParentUIObject), anchorInfo.OffsetY); } else { position.x += UIRelative.xPixelsFrom(anchorInfo.UIxAnchor, anchorInfo.OffsetX); position.y -= UIRelative.yPixelsFrom(anchorInfo.UIyAnchor, anchorInfo.OffsetY); } // Adjust for anchor offset position.x -= UIRelative.xAnchorAdjustment(anchorInfo.UIxAnchor, sprite.width, anchorInfo.OriginUIxAnchor); position.y += UIRelative.yAnchorAdjustment(anchorInfo.UIyAnchor, sprite.height, anchorInfo.OriginUIyAnchor); // Set depth position.z = depth; // Set new position sprite.position = position; }
/// <summary> /// Positions a sprite relatively from the top-left corner of the screen. Values are pixels from the corner. /// </summary public static void pixelsFromTopLeft(this UISprite sprite, int pixelsFromTop, int pixelsFromLeft) { var pos = sprite.position; pos.x = UIRelative.xPixelsFrom(UIxAnchor.Left, pixelsFromLeft); pos.y = -UIRelative.yPixelsFrom(UIyAnchor.Top, pixelsFromTop); sprite.position = pos; }
/// <summary> /// Positions a sprite relatively from the top-right corner of the screen. Values are pixels from the corner. /// The right boundary will be measured from the sprites right-most point /// </summary public static void pixelsFromTopRight(this UISprite sprite, int pixelsFromTop, int pixelsFromRight) { var pos = sprite.position; pos.x = UIRelative.xPixelsFrom(UIxAnchor.Right, pixelsFromRight, sprite.width); pos.y = -UIRelative.yPixelsFrom(UIyAnchor.Top, pixelsFromTop); sprite.position = pos; }
/// <summary> /// Positions a sprite relatively from the bottom-left corner of the screen. Values are pixels from the corner. /// The bottom boundary will be measured from the sprites bottom-most point /// </summary public static void pixelsFromBottomLeft(this UISprite sprite, int pixelsFromBottom, int pixelsFromLeft) { var pos = sprite.position; pos.x = UIRelative.xPixelsFrom(UIxAnchor.Left, pixelsFromLeft); pos.y = -UIRelative.yPixelsFrom(UIyAnchor.Bottom, pixelsFromBottom, sprite.height); sprite.position = pos; }