c# - XNA - Drawing Strings on Menus -


two questions on drawing list of strings on xna in menus. first how left align text instead of center align it? second how show line before whichever 1 selected? here code have far;

    public void draw(spritebatch spritebatch, gametime gametime)     {         color color;         int linepadding = 3;          if (gametime.totalgametime.totalseconds <= 3)         {             spritebatch.draw(mthequantumbros2, new rectangle(300, 150, mthequantumbros2.width, mthequantumbros2.height), color.white);         }         else         {             (int = 0; < buttonlist.count; i++)             {                 color = (i == selected) ? color.gold : color.lawngreen;                 spritebatch.drawstring(spritefont, buttonlist[i], new vector2((screenwidth / 2) - (spritefont.measurestring(buttonlist[i]).x / 2), (screenheight / 2) - (spritefont.linespacing * (buttonlist.count) / 2) + ((spritefont.linespacing + linepadding) * i)), color);             }         }     } 

hmm.. think center aligning better, in code need change draw string this:

for (int = 0; < buttonlist.count; i++) {      color = (i == selected) ? color.lawngreen : color.gold;      spritebatch.drawstring(spritefont, buttonlist[i], new vector2((screenwidth / 2) /*- (spritefont.measurestring(buttonlist[i]).x / 2)*/, (screenheight / 2) - (spritefont.linespacing * (buttonlist.count) / 2) + ((spritefont.linespacing + linepadding) * i)), color); } 

you can see commented out. taking out, no longer moving string left half of width.

as second part, need check if value selected, doing determine menu item color. if selected, add "|" front , end of string:

for (int = 0; < buttonlist.count; i++) {      color = (i == selected) ? color.lawngreen : color.gold;      if(i != selected)      {          spritebatch.drawstring(spritefont, buttonlist[i], new vector2((screenwidth / 2) /*- (spritefont.measurestring(buttonlist[i]).x / 2)*/, (screenheight / 2) - (spritefont.linespacing * (buttonlist.count) / 2) + ((spritefont.linespacing + linepadding) * i)), color);      }      else      {          spritebatch.drawstring(spritefont,"|" + buttonlist[i] + "|", new vector2((screenwidth / 2) - (int)spritefont.measurestring("|").x, (screenheight / 2) - (spritefont.linespacing * (buttonlist.count) / 2) + ((spritefont.linespacing + linepadding) * i)), color);      } } 

now, since adding "|" beginning of string, effect position. subtracting measured width of "|", string recentered.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -