MenuScreen.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #endregion
  17. namespace GameStateManagement
  18. {
  19. /// <summary>
  20. /// Base class for screens that contain a menu of options. The user can
  21. /// move up and down to select an entry, or cancel to back out of the screen.
  22. /// </summary>
  23. abstract class MenuScreen : GameScreen
  24. {
  25. #region Fields
  26. // the number of pixels to pad above and below menu entries for touch input
  27. const int menuEntryPadding = 23;
  28. List<MenuEntry> menuEntries = new List<MenuEntry>();
  29. int selectedEntry = 0;
  30. string menuTitle;
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// Gets the list of menu entries, so derived classes can add
  35. /// or change the menu contents.
  36. /// </summary>
  37. protected IList<MenuEntry> MenuEntries
  38. {
  39. get { return menuEntries; }
  40. }
  41. #endregion
  42. #region Initialization
  43. /// <summary>
  44. /// Constructor.
  45. /// </summary>
  46. public MenuScreen(string menuTitle)
  47. {
  48. // menus generally only need Tap for menu selection
  49. EnabledGestures = GestureType.Tap;
  50. this.menuTitle = menuTitle;
  51. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  52. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  53. }
  54. #endregion
  55. #region Handle Input
  56. /// <summary>
  57. /// Allows the screen to create the hit bounds for a particular menu entry.
  58. /// </summary>
  59. protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry)
  60. {
  61. // the hit bounds are the entire width of the screen, and the height of the entry
  62. // with some additional padding above and below.
  63. return new Rectangle(
  64. 0,
  65. (int)entry.Position.Y - menuEntryPadding,
  66. ScreenManager.GraphicsDevice.Viewport.Width,
  67. entry.GetHeight(this) + (menuEntryPadding * 2));
  68. }
  69. /// <summary>
  70. /// Responds to user input, changing the selected entry and accepting
  71. /// or cancelling the menu.
  72. /// </summary>
  73. public override void HandleInput(InputState input)
  74. {
  75. // we cancel the current menu screen if the user presses the back button
  76. PlayerIndex player;
  77. if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
  78. {
  79. OnCancel(player);
  80. }
  81. // look for any taps that occurred and select any entries that were tapped
  82. foreach (GestureSample gesture in input.Gestures)
  83. {
  84. if (gesture.GestureType == GestureType.Tap)
  85. {
  86. // convert the position to a Point that we can test against a Rectangle
  87. Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
  88. // iterate the entries to see if any were tapped
  89. for (int i = 0; i < menuEntries.Count; i++)
  90. {
  91. MenuEntry menuEntry = menuEntries[i];
  92. if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
  93. {
  94. // select the entry. since gestures are only available on Windows Phone,
  95. // we can safely pass PlayerIndex.One to all entries since there is only
  96. // one player on Windows Phone.
  97. OnSelectEntry(i, PlayerIndex.One);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Handler for when the user has chosen a menu entry.
  105. /// </summary>
  106. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  107. {
  108. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  109. }
  110. /// <summary>
  111. /// Handler for when the user has cancelled the menu.
  112. /// </summary>
  113. protected virtual void OnCancel(PlayerIndex playerIndex)
  114. {
  115. ExitScreen();
  116. }
  117. /// <summary>
  118. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  119. /// </summary>
  120. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  121. {
  122. OnCancel(e.PlayerIndex);
  123. }
  124. #endregion
  125. #region Update and Draw
  126. /// <summary>
  127. /// Allows the screen the chance to position the menu entries. By default
  128. /// all menu entries are lined up in a vertical list, centered on the screen.
  129. /// </summary>
  130. protected virtual void UpdateMenuEntryLocations()
  131. {
  132. // Make the menu slide into place during transitions, using a
  133. // power curve to make things look more interesting (this makes
  134. // the movement slow down as it nears the end).
  135. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  136. // start at Y = 475; each X value is generated per entry
  137. Vector2 position = new Vector2(0f, 316f);
  138. // update each menu entry's location in turn
  139. for (int i = 0; i < menuEntries.Count; i++)
  140. {
  141. MenuEntry menuEntry = menuEntries[i];
  142. // each entry is to be centered horizontally
  143. position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
  144. if (ScreenState == ScreenState.TransitionOn)
  145. position.X -= transitionOffset * 256;
  146. else
  147. position.X += transitionOffset * 512;
  148. // set the entry's position
  149. menuEntry.Position = position;
  150. // move down for the next entry the size of this entry plus our padding
  151. position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2);
  152. }
  153. }
  154. /// <summary>
  155. /// Updates the menu.
  156. /// </summary>
  157. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  158. bool coveredByOtherScreen)
  159. {
  160. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  161. // Update each nested MenuEntry object.
  162. for (int i = 0; i < menuEntries.Count; i++)
  163. {
  164. bool isSelected = IsActive && (i == selectedEntry);
  165. menuEntries[i].Update(this, isSelected, gameTime);
  166. }
  167. }
  168. /// <summary>
  169. /// Draws the menu.
  170. /// </summary>
  171. public override void Draw(GameTime gameTime)
  172. {
  173. // make sure our entries are in the right place before we draw them
  174. UpdateMenuEntryLocations();
  175. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  176. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  177. SpriteFont font = ScreenManager.Font;
  178. spriteBatch.Begin();
  179. // Draw each menu entry in turn.
  180. for (int i = 0; i < menuEntries.Count; i++)
  181. {
  182. MenuEntry menuEntry = menuEntries[i];
  183. bool isSelected = IsActive && (i == selectedEntry);
  184. menuEntry.Draw(this, isSelected, gameTime);
  185. }
  186. // Make the menu slide into place during transitions, using a
  187. // power curve to make things look more interesting (this makes
  188. // the movement slow down as it nears the end).
  189. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  190. // Draw the menu title centered on the screen
  191. Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 375);
  192. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  193. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  194. float titleScale = 1.25f;
  195. titlePosition.Y -= transitionOffset * 100;
  196. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  197. titleOrigin, titleScale, SpriteEffects.None, 0);
  198. spriteBatch.End();
  199. }
  200. #endregion
  201. }
  202. }