MenuScreen.cs 11 KB

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