MenuScreen.cs 11 KB

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