123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #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;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- #endregion
- namespace RolePlaying
- {
- /// <summary>
- /// 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.
- /// </summary>
- /// <remarks>
- /// Similar to a class found in the Game State Management sample on the
- /// XNA Creators Club Online website (http://creators.xna.com).
- /// </remarks>
- abstract class MenuScreen : GameScreen
- {
- #region Fields
- List<MenuEntry> menuEntries = new List<MenuEntry>();
- protected int selectedEntry = 0;
- #endregion
- #region Properties
- /// <summary>
- /// Gets the list of menu entries, so derived classes can add
- /// or change the menu contents.
- /// </summary>
- protected IList<MenuEntry> MenuEntries
- {
- get { return menuEntries; }
- }
- protected MenuEntry SelectedMenuEntry
- {
- get
- {
- if ((selectedEntry < 0) || (selectedEntry >= menuEntries.Count))
- {
- return null;
- }
- return menuEntries[selectedEntry];
- }
- }
- #endregion
-
- #region Initialization
- /// <summary>
- /// Constructor.
- /// </summary>
- public MenuScreen()
- {
- TransitionOnTime = TimeSpan.FromSeconds(0.5);
- TransitionOffTime = TimeSpan.FromSeconds(0.5);
- }
- #endregion
- #region Handle Input
- /// <summary>
- /// Responds to user input, changing the selected entry and accepting
- /// or cancelling the menu.
- /// </summary>
- public override void HandleInput()
- {
- int oldSelectedEntry = selectedEntry;
- // Move to the previous menu entry?
- if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
- {
- selectedEntry--;
- if (selectedEntry < 0)
- selectedEntry = menuEntries.Count - 1;
- }
- // Move to the next menu entry?
- if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
- {
- selectedEntry++;
- if (selectedEntry >= menuEntries.Count)
- selectedEntry = 0;
- }
- // Accept or cancel the menu?
- if (InputManager.IsActionTriggered(InputManager.Action.Ok))
- {
- AudioManager.PlayCue("Continue");
- OnSelectEntry(selectedEntry);
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.Back) ||
- InputManager.IsActionTriggered(InputManager.Action.ExitGame))
- {
- OnCancel();
- }
- else if (selectedEntry != oldSelectedEntry)
- {
- AudioManager.PlayCue("MenuMove");
- }
- }
- /// <summary>
- /// Handler for when the user has chosen a menu entry.
- /// </summary>
- protected virtual void OnSelectEntry(int entryIndex)
- {
- menuEntries[selectedEntry].OnSelectEntry();
- }
- /// <summary>
- /// Handler for when the user has cancelled the menu.
- /// </summary>
- protected virtual void OnCancel()
- {
- ExitScreen();
- }
- /// <summary>
- /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
- /// </summary>
- protected void OnCancel(object sender, EventArgs e)
- {
- OnCancel();
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// Updates the menu.
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// Draws the menu.
- /// </summary>
- public override void Draw(GameTime gameTime)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- 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, isSelected, gameTime);
- }
- spriteBatch.End();
- }
- #endregion
- }
- }
|