#region File Description //----------------------------------------------------------------------------- // MenuScreen.cs // // XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; #if IPHONE using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #else using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #endif #endregion namespace Microsoft.Xna.Samples.GameStateManagement { /// /// Base class for screens that contain a menu of options. The user can /// move up and down to select an entry, or cancel to back out of the screen. /// abstract class MenuScreen : GameScreen { #region Fields List menuEntries = new List(); int selectedEntry = 0; string menuTitle; #endregion #region Properties /// /// Gets the list of menu entries, so derived classes can add /// or change the menu contents. /// protected IList MenuEntries { get { return menuEntries; } } #endregion #region Initialization /// /// Constructor. /// public MenuScreen(string menuTitle) { this.menuTitle = menuTitle; TransitionOnTime = TimeSpan.FromSeconds(0.5); TransitionOffTime = TimeSpan.FromSeconds(0.5); } #endregion #region Handle Input /// /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// public override void HandleInput(InputState input) { // Move to the previous menu entry? if (input.IsMenuUp(ControllingPlayer)) { selectedEntry--; if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; } // Move to the next menu entry? if (input.IsMenuDown(ControllingPlayer)) { selectedEntry++; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; } // Accept or cancel the menu? We pass in our ControllingPlayer, which may // either be null (to accept input from any player) or a specific index. // If we pass a null controlling player, the InputState helper returns to // us which player actually provided the input. We pass that through to // OnSelectEntry and OnCancel, so they can tell which player triggered them. PlayerIndex playerIndex; if (input.IsMenuSelect(ControllingPlayer, out playerIndex)) { OnSelectEntry(selectedEntry, playerIndex); } else if (input.IsMenuCancel(ControllingPlayer, out playerIndex)) { OnCancel(playerIndex); } } /// /// Handler for when the user has chosen a menu entry. /// protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex) { menuEntries[selectedEntry].OnSelectEntry(playerIndex); } /// /// Handler for when the user has cancelled the menu. /// protected virtual void OnCancel(PlayerIndex playerIndex) { ExitScreen(); } /// /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler. /// protected void OnCancel(object sender, PlayerIndexEventArgs e) { OnCancel(e.PlayerIndex); } #endregion #region Update and Draw /// /// Updates the menu. /// public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); // Update each nested MenuEntry object. for (int i = 0; i < menuEntries.Count; i++) { bool isSelected = IsActive && (i == selectedEntry); menuEntries[i].Update(this, isSelected, gameTime); } } /// /// Draws the menu. /// public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; SpriteFont font = ScreenManager.Font; Vector2 position = new Vector2(80, 140); // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(TransitionPosition, 2); if (ScreenState == ScreenState.TransitionOn) position.X -= transitionOffset * 256; else position.X += transitionOffset * 512; spriteBatch.Begin(); // Draw each menu entry in turn. for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; bool isSelected = IsActive && (i == selectedEntry); menuEntry.Draw(this, position, isSelected, gameTime); position.Y += menuEntry.GetHeight(this); } // Draw the menu title. Vector2 titlePosition = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2, 80); Vector2 titleOrigin = font.MeasureString(menuTitle) / 2; Color titleColor = new Color(192, 192, 192, TransitionAlpha); float titleScale = 1.25f; titlePosition.Y -= transitionOffset * 100; spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0, titleOrigin, titleScale, SpriteEffects.None, 0); spriteBatch.End(); } #endregion } }