このように見えるように、スプライトバッチで文字列のリストを描画するにはどうすればよいですか。
Item1 Item2
Item3 Item4
アイテム 1 アイテム 2 アイテム 3 アイテム 4 の代わりに、または次の代わりに:
Item1
Item2
Item3
Item4
このように見えるように、スプライトバッチで文字列のリストを描画するにはどうすればよいですか。
Item1 Item2
Item3 Item4
アイテム 1 アイテム 2 アイテム 3 アイテム 4 の代わりに、または次の代わりに:
Item1
Item2
Item3
Item4
このようなものは、後で変更するのが少し簡単になります。
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;
}
}
これで、ストリングを中央に配置するおおよその位置を推測できます.... または、並べて配置されるストリングの長さを見つけ、それらの間にあるスペースの合計量を測定し、その他のトリッキーなことができますどの弦をどこに置くべきかを把握するためのもの... 言い換えれば、それらを完全に中央に配置することは苦痛であり、おそらく私が知る限り、その価値よりもはるかに面倒です.
リファクタリングされたコード:
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);
}
}
}
}
PS 今のところ見た目が気に入っています ^^.