コード例 #1
0
        private void PositionItem(ILayoutable item, HorizontalPosition horizontalPosition, VerticalPosition verticalPosition, LayoutOrigin layoutFrom)
        {
            // Make sure this is still the item's parent
            if (item.Parent != _backgroundSprite)
            {
                if (_items.ContainsKey(item))
                {
                    _items.Remove(item);
                }

                return;
            }

            // Position the item correctly
            float posX;
            float posY;

            // Figure out starting horizontal and vertical coordinates
            if (horizontalPosition.Alignment == HorizontalPosition.PositionAlignment.Left)
            {
                posX = 0 - ScaleX;
            }
            else if (horizontalPosition.Alignment == HorizontalPosition.PositionAlignment.Right)
            {
                posX = ScaleX;
            }
            else
            {
                posX = 0;
            }

            if (verticalPosition.Alignment == VerticalPosition.PositionAlignment.Top)
            {
                posY = ScaleY;
            }
            else if (verticalPosition.Alignment == VerticalPosition.PositionAlignment.Bottom)
            {
                posY = 0 - ScaleY;
            }
            else
            {
                posY = 0;
            }

            // Calculate offsets
            if (horizontalPosition.OffsetIsPercentage)
            {
                var total = ScaleX * 2;
                posX += (horizontalPosition.Offset * total);
            }
            else
            {
                posX += horizontalPosition.Offset;
            }

            if (verticalPosition.OffsetIsPercentage)
            {
                var total = ScaleY * 2;
                posY += (verticalPosition.Offset * total);
            }
            else
            {
                posY += verticalPosition.Offset;
            }

            // Adjust for the specified layout origin
            switch (layoutFrom)
            {
            case LayoutOrigin.TopLeft:
            {
                posX += item.ScaleX;
                posY -= item.ScaleY;
                break;
            }

            case LayoutOrigin.TopRight:
            {
                posX -= item.ScaleX;
                posY -= item.ScaleY;
                break;
            }

            case LayoutOrigin.BottomLeft:
            {
                posX += item.ScaleX;
                posY += item.ScaleY;
                break;
            }

            case LayoutOrigin.BottomRight:
            {
                posX -= item.ScaleX;
                posY += item.ScaleY;
                break;
            }

            case LayoutOrigin.BottomCenter:
            {
                posY += item.ScaleY;
                break;
            }

            case LayoutOrigin.TopCenter:
            {
                posY -= item.ScaleY;
                break;
            }

            case LayoutOrigin.Center:
            default:
            {
                // PosX and PosY remain at center
                break;
            }
            }

            // Set the item's position to the calculated spot
            item.RelativeX = posX;
            item.RelativeY = posY;
        }
コード例 #2
0
        public void AddItem(ILayoutable item, HorizontalPosition horizontalPosition, VerticalPosition verticalPosition, LayoutOrigin layoutFrom = LayoutOrigin.Center)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            var position = new OverallPosition
            {
                HorizontalPosition = horizontalPosition,
                VerticalPosition   = verticalPosition,
                LayoutOrigin       = layoutFrom
            };

            _items.Add(item, position);
            item.Alpha = _alpha;
            item.AttachTo(_backgroundSprite, true);
            item.RelativeZ    = 0.1f;
            item.ParentLayout = this;

            if (item.OnAddedToLayout != null)
            {
                item.OnAddedToLayout(this);
            }

            PositionItem(item, horizontalPosition, verticalPosition, layoutFrom);

            // When the size changes, make sure to reposition the item so it's still in the same spot
            item.OnSizeChangeHandler = new LayoutableEvent(delegate(ILayoutable sender) { PositionItem(item, horizontalPosition, verticalPosition, layoutFrom); });
        }