2
0

MenuScreen.cs 11 KB

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