Question

How do you Draw a list of strings with a spritebatch so that it looks like this;

Item1 Item2
Item3 Item4

Instead of Item 1 Item 2 Item 3 Item 4 or instead of:

Item1
Item2
Item3
Item4
Was it helpful?

Solution

Something like this would make it a little easier to change later on:

int y = startPointY;
int x = startPointX;
int switchAt = items.Count/2; //<--- or where ever you might want to break up the strings
int max = 0;

for(int i = 0; i < items.Count; i++)
{
    spriteBatch.DrawString(font, items[i], new Vector2(x, y), Color);

    if(max < spriteFont.MeasureString(items[i]))
    {
        //this is to make sure the next column of strings are aligned
        max = spriteFont.MeasureString(items[i]);
    }

    y += someYSpace;

    if(i == switchAt)
    {
         x += max + someXSpace;//someXSpace not neccessary
         y = startPointY;         
    }         
}

Now, you can either guess the approximate position to center the strings.... or, you can find the lengths of the strings that will be side by side, measure the total amount of space that would be between them, and some other tricky stuff to figure out which strings should be placed where... In other words, getting them perfectly centered would be a pain and probably a lot more trouble than its worth, as far as I can tell.

Your refactored code:

        Color color;
        int linePadding = 3;
        int switchAt = buttonList.Count / 2 - 1;//minus one because the list starts at 0, and goes up to 3, so 1 is the switch point


        else
        {
            spriteBatch.Draw(mMenuPhoto, new Rectangle(800, 150, mMenuPhoto.Width, mMenuPhoto.Height), Color.White);  
            float X = 210;
            float Y = 600 - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding));
            float max = 0;
            float linesXSpace = 30;//to seperate this lines to make room for '|'

            for (int i = 0; i < buttonList.Count; i++)
            {
                color = (i == selected) ? Color.LightGray : Color.DarkSlateGray;
                if (max < spriteFont.MeasureString(buttonList[i]).X)
                {
                    max = spriteFont.MeasureString(buttonList[i]).X;
                }

                if (i != selected)
                {
                    spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2(X, Y), color);
                    Y += spriteFont.MeasureString(buttonList[i]).Y;    
                    if (i == switchAt)
                    {
                        X += max + linesXSpace;
                        Y = 600 - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding));
                    }                       

                }
                else
                {     
                    spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2(X, Y), color);
                    spriteBatch.DrawString(spriteFont2, "|", new Vector2(X - 20, Y), Color.DarkGreen);
                    Y += spriteFont.MeasureString(buttonList[i]).Y;   
                    if (i == switchAt)
                    {
                        X += max + linesXSpace;
                        Y = 600 - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i);
                    }                                             
                }
            }
        }

P.S. I like the way it looks so far ^^.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top