XNA - How to change orientation of Drawing a List of Strings

浪尽此生 提交于 2019-12-11 02:41:53

问题


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

回答1:


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 ^^.



来源:https://stackoverflow.com/questions/16576796/xna-how-to-change-orientation-of-drawing-a-list-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!