MenuScreen.cs 9.0 KB

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