MenuScreen.cs 9.5 KB

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