MenuScreen.cs 8.6 KB

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