MenuScreen.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 HoneycombRush
  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 = 35;
  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. return entry.Bounds;
  62. }
  63. /// <summary>
  64. /// Responds to user input, changing the selected entry and accepting
  65. /// or cancelling the menu.
  66. /// </summary>
  67. public override void HandleInput(GameTime gameTime, InputState input)
  68. {
  69. // we cancel the current menu screen if the user presses the back button
  70. PlayerIndex player;
  71. if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
  72. {
  73. OnCancel(player);
  74. }
  75. // Take care of Keyboard input
  76. if (input.IsMenuUp(ControllingPlayer))
  77. {
  78. selectedEntry--;
  79. if (selectedEntry < 0)
  80. selectedEntry = menuEntries.Count - 1;
  81. }
  82. else if (input.IsMenuDown(ControllingPlayer))
  83. {
  84. selectedEntry++;
  85. if (selectedEntry >= menuEntries.Count)
  86. selectedEntry = 0;
  87. }
  88. else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
  89. input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
  90. {
  91. OnSelectEntry(selectedEntry, player);
  92. }
  93. // look for any taps that occurred and select any entries that were tapped
  94. foreach (GestureSample gesture in input.Gestures)
  95. {
  96. if (gesture.GestureType == GestureType.Tap)
  97. {
  98. // convert the position to a Point that we can test against a Rectangle
  99. Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
  100. // iterate the entries to see if any were tapped
  101. for (int i = 0; i < menuEntries.Count; i++)
  102. {
  103. MenuEntry menuEntry = menuEntries[i];
  104. if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
  105. {
  106. // Select the entry. since gestures are only available on Windows Phone,
  107. // we can safely pass PlayerIndex.One to all entries since there is only
  108. // one player on Windows Phone.
  109. OnSelectEntry(i, PlayerIndex.One);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Handler for when the user has chosen a menu entry.
  117. /// </summary>
  118. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  119. {
  120. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  121. }
  122. /// <summary>
  123. /// Handler for when the user has cancelled the menu.
  124. /// </summary>
  125. protected virtual void OnCancel(PlayerIndex playerIndex)
  126. {
  127. ExitScreen();
  128. }
  129. /// <summary>
  130. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  131. /// </summary>
  132. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  133. {
  134. OnCancel(e.PlayerIndex);
  135. }
  136. #endregion
  137. #region Update and Draw
  138. /// <summary>
  139. /// Allows the screen the chance to position the menu entries. By default
  140. /// all menu entries are lined up in a vertical list, centered on the screen.
  141. /// </summary>
  142. protected virtual void UpdateMenuEntryLocations()
  143. {
  144. // Make the menu slide into place during transitions, using a
  145. // power curve to make things look more interesting (this makes
  146. // the movement slow down as it nears the end).
  147. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  148. // start at Y = 475; each X value is generated per entry
  149. Vector2 position = new Vector2(0f,
  150. ScreenManager.Game.Window.ClientBounds.Height /2 -
  151. (menuEntries[0].GetHeight(this) +(menuEntryPadding * 2) * menuEntries.Count));
  152. // update each menu entry's location in turn
  153. for (int i = 0; i < menuEntries.Count; i++)
  154. {
  155. MenuEntry menuEntry = menuEntries[i];
  156. // each entry is to be centered horizontally
  157. position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
  158. if (ScreenState == ScreenState.TransitionOn)
  159. position.X -= transitionOffset * 256;
  160. else
  161. position.X += transitionOffset * 512;
  162. // set the entry's position
  163. menuEntry.Position = position;
  164. // move down for the next entry the size of this entry plus our padding
  165. position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2);
  166. }
  167. }
  168. /// <summary>
  169. /// Updates the menu.
  170. /// </summary>
  171. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  172. bool coveredByOtherScreen)
  173. {
  174. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  175. // Update each nested MenuEntry object.
  176. for (int i = 0; i < menuEntries.Count; i++)
  177. {
  178. bool isSelected = IsActive && (i == selectedEntry);
  179. menuEntries[i].Update(this, isSelected, gameTime);
  180. }
  181. }
  182. /// <summary>
  183. /// Draws the menu.
  184. /// </summary>
  185. public override void Draw(GameTime gameTime)
  186. {
  187. // make sure our entries are in the right place before we draw them
  188. //UpdateMenuEntryLocations();
  189. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  190. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  191. SpriteFont font = ScreenManager.Font;
  192. spriteBatch.Begin();
  193. // Draw each menu entry in turn.
  194. for (int i = 0; i < menuEntries.Count; i++)
  195. {
  196. MenuEntry menuEntry = menuEntries[i];
  197. bool isSelected = IsActive && (i == selectedEntry);
  198. menuEntry.Draw(this, isSelected, gameTime);
  199. }
  200. // Make the menu slide into place during transitions, using a
  201. // power curve to make things look more interesting (this makes
  202. // the movement slow down as it nears the end).
  203. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  204. // Draw the menu title centered on the screen
  205. Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 375);
  206. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  207. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  208. float titleScale = 1.25f;
  209. titlePosition.Y -= transitionOffset * 100;
  210. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  211. titleOrigin, titleScale, SpriteEffects.None, 0);
  212. spriteBatch.End();
  213. }
  214. #endregion
  215. }
  216. }