c# - Mouse hovering over menu options -


i having trouble mouse hovering on menu. trying highlight various menu options when mouse on them not sure doing wrong. right not cycles through them fast when move mouse on them. appreciated. problem either in update function or measuremenu function. way have keyboard part working fine.

using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; namespace blocks { /// <summary> /// game component implements iupdateable. /// </summary> public class menucomponents : microsoft.xna.framework.drawablegamecomponent { string[] menuitems; int selectedindex; color normal = color.white; color hilite = color.yellow; keyboardstate keyboardstate; keyboardstate oldkeyboardstate; spritebatch spritebatch; spritefont spritefont; vector2 position,size; float width = 0f; float height = 0f; mousestate mousestate; rectangle area; public int selectedindex { { return selectedindex; } set { selectedindex = value; if (selectedindex < 0) selectedindex = 0; if (selectedindex >= menuitems.length) selectedindex = menuitems.length - 1; } } public menucomponents(game game, spritebatch spritebatch, spritefont spritefont, string[] menuitems) : base(game) { // todo: construct child components here this.spritebatch = spritebatch; this.spritefont = spritefont; this.menuitems = menuitems; measuremenu(); } private void measuremenu() { height = 0; width = 0; foreach (string item in menuitems) { size = spritefont.measurestring(item); if (size.x > width) width = size.x; height += spritefont.linespacing + 5; } position = new vector2((game.window.clientbounds.width - width) / 2, ((game.window.clientbounds.height - height) / 2) + 50); int positionx = (int) position.x; int positiony = (int) position.y; int areawidth = (int) width; int areaheight = (int) height; //for (int = 0; < area.length; i++) //{ area = new rectangle(positionx, positiony, areawidth, areaheight); //} } /// <summary> /// allows game component perform initialization needs before starting /// run. can query required services , load content. /// </summary> public override void initialize() { // todo: add initialization code here base.initialize(); } private bool checkkey(keys thekey) { return keyboardstate.iskeyup(thekey) && oldkeyboardstate.iskeydown(thekey); } /// <summary> /// allows game component update itself. /// </summary> /// <param name="gametime">provides snapshot of timing values.</param> public override void update(gametime gametime) { // todo: add update code here keyboardstate = keyboard.getstate(); mousestate = mouse.getstate(); point mouselocation = new point(mousestate.x, mousestate.y); if (checkkey(keys.down)) { selectedindex++; if (selectedindex == menuitems.length) selectedindex = 0; } if (checkkey(keys.up)) { selectedindex--; if (selectedindex < 0) selectedindex = menuitems.length - 1; } if (area.contains(mouselocation)) { selectedindex++; if (selectedindex == menuitems.length) selectedindex = 0; if (selectedindex < 0) selectedindex = menuitems.length - 1; } base.update(gametime); oldkeyboardstate = keyboardstate; } public override void draw(gametime gametime) { base.draw(gametime); vector2 location = position; color tint; (int = 0; < menuitems.length; i++) { if (i == selectedindex) tint = hilite; else tint = normal; spritebatch.drawstring(spritefont, menuitems[i], location, tint); location.y += spritefont.linespacing + 5; } } } } 

if (area.contains(mouselocation)) { selectedindex++; if (selectedindex == menuitems.length) selectedindex = 0; if (selectedindex < 0) selectedindex = menuitems.length - 1; } 

that code makes selectedindex 1 more if hovering on of rectangles. can explain cycling through each one.

you need check rectangle each menu item, , set selectedindex appropriately.

pseudo-code:

for (int = 0; i<= menuitems.count;i++) { rectangle rect = new rectangle(position.x,position.y + (i * heightofmenuitemandoffset),width,height);

mouse.getstate(); point mouselocation = new point(mousestate.x, mousestate.y); if (area.contains(mouselocation) { selectedindex = i; }

}

something along lines of that.


Comments