本文介绍了XNA - 如何改变绘制字符串列表的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何画出一个字符串列表与spritebatch,使它看起来像这样;

 项目1项目2 
项目3 ITEM4

而不是项目1项目2项目3项目4或代替:

 项目1 
项目2
项目3
ITEM4


解决方案

这样的事情会更容易一点改变以后:

  INT Y = startPointY; 
INT X = startPointX;
INT switchAt = items.Count / 2; //< ---或者其它任何你可能要分手字符串
INT最大值= 0;

的for(int i = 0; I< items.Count;我++)
{
spriteBatch.DrawString(字体,项目[I],新Vector2(X,Y ),彩色);

如果(MAX< spriteFont.MeasureString(项目由[i]))
{
//这是为了确保字符串的下一列排列
最大= spriteFont.MeasureString(项目[I]);
}

Y + = someYSpace;

如果(我== switchAt)
{
X + = MAX + someXSpace; // someXSpace不neccessary
Y = startPointY;
}
}

现在,你可以猜测的大致位置中心琴弦....或者,你可以找到字符串,将并排的长度,测量,这将是他们之间的空间总量,以及其他一些棘手的问题要弄清楚哪些字符串应该放在哪里。 ..也就是说,让他们完全置将是一个痛苦和可能很多更多的麻烦比它的价值,据我可以告诉



您重构的代码:

 色彩色; 
INT linePadding = 3;
INT switchAt = buttonList.Count / 2 - 1; //减一,因为该列表开始于0,并上升到3,因此,1是切换点


否则
{
spriteBatch.Draw(mMenuPhoto,新的Rectangle(800,150,mMenuPhoto.Width,mMenuPhoto.Height),Color.White);
浮动X = 210;
浮子Y = 600 - (spriteFont.LineSpacing *(buttonList.Count)/ 2)+((spriteFont.LineSpacing + linePadding));
FLOAT MAX = 0;
浮动linesXSpace = 30; //给这个单独线路,以腾出空间|

的for(int i = 0; I< buttonList.Count;我++)
{
色=(我==选择)? Color.LightGray:Color.DarkSlateGray;
如果(MAX< spriteFont.MeasureString(buttonList [I])x)
{
最大= spriteFont.MeasureString(buttonList [I])×。
}

如果(ⅰ!=选择)
{
spriteBatch.DrawString(SpriteFont类,buttonList [Ⅰ],新Vector2(X,Y),颜色) ;
Y + = spriteFont.MeasureString(buttonList [I])Y。
如果(我== switchAt)
{
X + = MAX + linesXSpace;
Y = 600 - (spriteFont.LineSpacing *(buttonList.Count)/ 2)+((spriteFont.LineSpacing + linePadding));
}

}
,否则
{
spriteBatch.DrawString(SpriteFont类,buttonList [Ⅰ],新Vector2(X,Y),颜色);
spriteBatch.DrawString(spriteFont2,|,新Vector2(X - 20,Y)Color.DarkGreen);
Y + = spriteFont.MeasureString(buttonList [I])Y。
如果(我== switchAt)
{
X + = MAX + linesXSpace;
Y = 600 - (spriteFont.LineSpacing *(buttonList.Count)/ 2)+((spriteFont.LineSpacing + linePadding)*ⅰ);
}
}
}
}



P.S。我喜欢它到目前为止的样子^^的方式。


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
解决方案

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

这篇关于XNA - 如何改变绘制字符串列表的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 22:12
查看更多