Sunday, October 7, 2012

5: Cyclone Game Engine's Menu System



     I wanted to create a 2D menu system which allowed us to easily add new menus and specify their items. The menus allow the user to scroll through the different items and menus using the controller/keyboard. I also wanted to be able to create some smooth transition effects when the user browses from one menu to another. This menu system is based off of Riemer Grootjans 2D menu interface sample from his book XNA 3.0 Game Programming Recipes and I upgraded it to XNA 4.0. I omitted the menu effect from the book however. I greatly extended the menu system by adding splash screens, a credits screen and the gameplay screen with further gamepad support. As far as the models in the scene, I am fixing the meshes so that they are exported better.


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.

public MenuWindow ProcessInput(KeyboardState lastKeybState, KeyboardState currentKeybState, GamePadState lastGamePadState, GamePadState currentGamePadState)
        {
                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