Extend the MenuWindow Class with Xbox 360 Controller Support
So the MenuWindow Class has been extended to allow the user to navigate through the menus with the Xbox 360 controller using the Left-Thumbstick and the the D-Pad. Below is the source code to the controls in the MenuWindow Class.
{
if (lastKeybState.IsKeyUp(Keys.Down) && currentKeybState.IsKeyDown(Keys.Down))
selectedItem++;
// New
if (currentGamePadState.DPad.Down == ButtonState.Pressed && lastGamePadState.DPad.Down == ButtonState.Released || currentGamePadState.IsButtonDown(Buttons.LeftThumbstickDown) && lastGamePadState.IsButtonUp(Buttons.LeftThumbstickDown))
{
selectedItem++;
buttonClick.Play();
if (selectedItem < 0)
selectedItem = itemList.Count - 1;
if (selectedItem >= itemList.Count)
selectedItem = 0;
}
if (lastKeybState.IsKeyUp(Keys.Up) && currentKeybState.IsKeyDown(Keys.Up))
selectedItem--;
// New
if (currentGamePadState.DPad.Up == ButtonState.Pressed && lastGamePadState.DPad.Up == ButtonState.Released || currentGamePadState.IsButtonDown(Buttons.LeftThumbstickUp) && lastGamePadState.IsButtonUp(Buttons.LeftThumbstickUp))
{
selectedItem--;
buttonClick.Play();
if (selectedItem < 0)
selectedItem = itemList.Count - 1;
if (selectedItem >= itemList.Count)
selectedItem = 0;
}
if ((lastKeybState.IsKeyUp(Keys.Enter) && currentKeybState.IsKeyDown(Keys.Enter)) || ((lastGamePadState.Buttons.A == ButtonState.Released) && (currentGamePadState.Buttons.A == ButtonState.Pressed)))
{
itemSelected.Play();
windowState = WindowState.Ending;
return itemList[selectedItem].itemLink;
}
if (lastKeybState.IsKeyDown(Keys.Escape))
{
windowState = WindowState.Inactive;
return null;
}
else
return this;
}
Increase Font Size of Menu Selection
I also increased the font size when an item is selected and it switches back to its previous size when not selected inside the Draw Method of the MenuWindow Class.
for (int itemID = 0; itemID < itemList.Count; itemID++)
{
Vector2 itemPostition = new Vector2(horPosition, verPosition);
Color itemColor = Color.White;
verPosition += 40;
if (itemID == selectedItem)
{
itemColor = Color.Yellow;
// increase font size
spriteBatch.DrawString(spriteFont, itemList[itemID].itemText, itemPostition, itemColor, 0, Vector2.Zero, 1.2f, SpriteEffects.None, 0);
}
else
{
itemColor = Color.White;
// decrease font size back to normal
spriteBatch.DrawString(spriteFont, itemList[itemID].itemText, itemPostition, itemColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
}
}
No comments:
Post a Comment