TCG Prototype

by Patrick on August 01, 2013 at 00:00

I started working on this when a friend was dissapointed by the lack of flexibility with the official game. It's still far from finished, but the UI and graphics rendering are largely complete.

This project uses C# and XNA, which may not have been for the best given its abandonment by Microsoft.

The last piece I was working on was a layout manager for the menu screens. It previously relied on a large quantity of magic numbers. Putting together a simple fluid layout turned out to be pretty simple.

private void PositionLeft()
{
    Rectangle prevElementPos = this.Position;
    prevElementPos.X += Margin;
    prevElementPos.Width -= Margin;
    prevElementPos.Height = 0;
    // The right-most (x) coordinate filled by a positioned element.
    // Used to track where the next column should start.
    int rightPoint = 0;
    foreach (IClickable element in Elements)
    {
        // Check if we've reached the bottom of the container
        if ((element.HitBox.Height + prevElementPos.Bottom + (Margin*2)) > this.Position.Bottom)
        {
            // Make sure there's enough horizontal space left
            if ((element.HitBox.Width + rightPoint + Margin) > this.Position.Right)
                break;
            prevElementPos = this.Position; // reset position
            prevElementPos.X += rightPoint + Margin; // move into next column
            prevElementPos.Height = 0;
        }
        // Add the next element in-line below the last element
        element.Position = new Vector2(
            prevElementPos.X,
            prevElementPos.Y + prevElementPos.Height + Margin);
        // Track the right-most point a positioned element occupies
        if (element.HitBox.Right > rightPoint)
            rightPoint = element.HitBox.Right;
        prevElementPos = element.HitBox;
    }
}