MenuScreen.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuScreen.cs
  4. //
  5. // XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input.Touch;
  15. using Microsoft.Xna.Framework.Input;
  16. using GameStateManagement;
  17. #endregion
  18. namespace GameStateManagementSample
  19. {
  20. /// <summary>
  21. /// Base class for screens that contain a menu of options. The user can
  22. /// move up and down to select an entry, or cancel to back out of the screen.
  23. /// </summary>
  24. abstract class MenuScreen : GameScreen
  25. {
  26. #region Fields
  27. List<MenuEntry> menuEntries = new List<MenuEntry>();
  28. int selectedEntry = 0;
  29. string menuTitle;
  30. InputAction menuUp;
  31. InputAction menuDown;
  32. InputAction menuSelect;
  33. InputAction menuCancel;
  34. #endregion
  35. #region Properties
  36. /// <summary>
  37. /// Gets the list of menu entries, so derived classes can add
  38. /// or change the menu contents.
  39. /// </summary>
  40. protected IList<MenuEntry> MenuEntries
  41. {
  42. get { return menuEntries; }
  43. }
  44. #endregion
  45. #region Initialization
  46. /// <summary>
  47. /// Constructor.
  48. /// </summary>
  49. public MenuScreen(string menuTitle)
  50. {
  51. this.menuTitle = menuTitle;
  52. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  53. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  54. menuUp = new InputAction(
  55. new Buttons[] { Buttons.DPadUp, Buttons.LeftThumbstickUp },
  56. new Keys[] { Keys.Up },
  57. true);
  58. menuDown = new InputAction(
  59. new Buttons[] { Buttons.DPadDown, Buttons.LeftThumbstickDown },
  60. new Keys[] { Keys.Down },
  61. true);
  62. menuSelect = new InputAction(
  63. new Buttons[] { Buttons.A, Buttons.Start },
  64. new Keys[] { Keys.Enter, Keys.Space },
  65. true);
  66. menuCancel = new InputAction(
  67. new Buttons[] { Buttons.B, Buttons.Back },
  68. new Keys[] { Keys.Escape },
  69. true);
  70. }
  71. #endregion
  72. #region Handle Input
  73. /// <summary>
  74. /// Responds to user input, changing the selected entry and accepting
  75. /// or cancelling the menu.
  76. /// </summary>
  77. public override void HandleInput(GameTime gameTime, InputState input)
  78. {
  79. // For input tests we pass in our ControllingPlayer, which may
  80. // either be null (to accept input from any player) or a specific index.
  81. // If we pass a null controlling player, the InputState helper returns to
  82. // us which player actually provided the input. We pass that through to
  83. // OnSelectEntry and OnCancel, so they can tell which player triggered them.
  84. PlayerIndex playerIndex;
  85. // Move to the previous menu entry?
  86. if (menuUp.Evaluate(input, ControllingPlayer, out playerIndex))
  87. {
  88. selectedEntry--;
  89. if (selectedEntry < 0)
  90. selectedEntry = menuEntries.Count - 1;
  91. }
  92. // Move to the next menu entry?
  93. if (menuDown.Evaluate(input, ControllingPlayer, out playerIndex))
  94. {
  95. selectedEntry++;
  96. if (selectedEntry >= menuEntries.Count)
  97. selectedEntry = 0;
  98. }
  99. if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex))
  100. {
  101. OnSelectEntry(selectedEntry, playerIndex);
  102. }
  103. else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex))
  104. {
  105. OnCancel(playerIndex);
  106. }
  107. }
  108. /// <summary>
  109. /// Handler for when the user has chosen a menu entry.
  110. /// </summary>
  111. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  112. {
  113. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  114. }
  115. /// <summary>
  116. /// Handler for when the user has cancelled the menu.
  117. /// </summary>
  118. protected virtual void OnCancel(PlayerIndex playerIndex)
  119. {
  120. ExitScreen();
  121. }
  122. /// <summary>
  123. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  124. /// </summary>
  125. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  126. {
  127. OnCancel(e.PlayerIndex);
  128. }
  129. #endregion
  130. #region Update and Draw
  131. /// <summary>
  132. /// Allows the screen the chance to position the menu entries. By default
  133. /// all menu entries are lined up in a vertical list, centered on the screen.
  134. /// </summary>
  135. protected virtual void UpdateMenuEntryLocations()
  136. {
  137. // Make the menu slide into place during transitions, using a
  138. // power curve to make things look more interesting (this makes
  139. // the movement slow down as it nears the end).
  140. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  141. // start at Y = 175; each X value is generated per entry
  142. Vector2 position = new Vector2(0f, 175f);
  143. // update each menu entry's location in turn
  144. for (int i = 0; i < menuEntries.Count; i++)
  145. {
  146. MenuEntry menuEntry = menuEntries[i];
  147. // each entry is to be centered horizontally
  148. position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
  149. if (ScreenState == ScreenState.TransitionOn)
  150. position.X -= transitionOffset * 256;
  151. else
  152. position.X += transitionOffset * 512;
  153. // set the entry's position
  154. menuEntry.Position = position;
  155. // move down for the next entry the size of this entry
  156. position.Y += menuEntry.GetHeight(this);
  157. }
  158. }
  159. /// <summary>
  160. /// Updates the menu.
  161. /// </summary>
  162. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  163. bool coveredByOtherScreen)
  164. {
  165. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  166. // Update each nested MenuEntry object.
  167. for (int i = 0; i < menuEntries.Count; i++)
  168. {
  169. bool isSelected = IsActive && (i == selectedEntry);
  170. menuEntries[i].Update(this, isSelected, gameTime);
  171. }
  172. }
  173. /// <summary>
  174. /// Draws the menu.
  175. /// </summary>
  176. public override void Draw(GameTime gameTime)
  177. {
  178. // make sure our entries are in the right place before we draw them
  179. UpdateMenuEntryLocations();
  180. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  181. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  182. SpriteFont font = ScreenManager.Font;
  183. spriteBatch.Begin();
  184. // Draw each menu entry in turn.
  185. for (int i = 0; i < menuEntries.Count; i++)
  186. {
  187. MenuEntry menuEntry = menuEntries[i];
  188. bool isSelected = IsActive && (i == selectedEntry);
  189. menuEntry.Draw(this, isSelected, gameTime);
  190. }
  191. // Make the menu slide into place during transitions, using a
  192. // power curve to make things look more interesting (this makes
  193. // the movement slow down as it nears the end).
  194. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  195. // Draw the menu title centered on the screen
  196. Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
  197. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  198. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  199. float titleScale = 1.25f;
  200. titlePosition.Y -= transitionOffset * 100;
  201. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  202. titleOrigin, titleScale, SpriteEffects.None, 0);
  203. spriteBatch.End();
  204. }
  205. #endregion
  206. }
  207. }