MenuScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. using CardsFramework;
  14. namespace GameStateManagement
  15. {
  16. /// <summary>
  17. /// Base class for screens that contain a menu of options. The user can
  18. /// move up and down to select an entry, or cancel to back out of the screen.
  19. /// </summary>
  20. abstract class MenuScreen : GameScreen
  21. {
  22. // the number of pixels to pad above and below menu entries for touch input
  23. const int menuEntryPadding = 35;
  24. List<MenuEntry> menuEntries = new List<MenuEntry>();
  25. int selectedEntry = 0;
  26. string menuTitle;
  27. Rectangle bounds;
  28. private bool isMouseDown = false;
  29. /// <summary>
  30. /// Gets whether the mouse button is currently pressed down.
  31. /// Used by MenuEntry to determine if it should show the pressed texture.
  32. /// </summary>
  33. public bool IsMouseDown
  34. {
  35. get { return isMouseDown; }
  36. }
  37. /// <summary>
  38. /// Gets the list of menu entries, so derived classes can add
  39. /// or change the menu contents.
  40. /// </summary>
  41. protected IList<MenuEntry> MenuEntries
  42. {
  43. get { return menuEntries; }
  44. }
  45. /// <summary>
  46. /// Constructor.
  47. /// </summary>
  48. public MenuScreen(string menuTitle)
  49. {
  50. // menus generally only need Tap for menu selection
  51. EnabledGestures = GestureType.Tap;
  52. this.menuTitle = menuTitle;
  53. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  54. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  55. }
  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.Destination.Y - menuEntryPadding,
  66. (int)ScreenManager.BaseScreenSize.X,
  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 inputState)
  74. {
  75. // Cancel the current menu screen if the user presses the back button
  76. PlayerIndex player;
  77. if (inputState.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
  78. {
  79. OnCancel(player);
  80. }
  81. if (UIUtility.IsDesktop)
  82. {
  83. // Handle keyboard input
  84. if (inputState.IsMenuUp(ControllingPlayer))
  85. {
  86. selectedEntry--;
  87. if (selectedEntry < 0)
  88. selectedEntry = menuEntries.Count - 1;
  89. }
  90. else if (inputState.IsMenuDown(ControllingPlayer))
  91. {
  92. selectedEntry++;
  93. if (selectedEntry >= menuEntries.Count)
  94. selectedEntry = 0;
  95. }
  96. else if (inputState.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
  97. inputState.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
  98. {
  99. OnSelectEntry(selectedEntry, player);
  100. }
  101. // Handle mouse input using transformed coordinates
  102. if (inputState.CurrentMouseState.LeftButton == ButtonState.Released)
  103. {
  104. if (isMouseDown)
  105. {
  106. isMouseDown = false;
  107. // Use transformed cursor location for proper scaling/letterboxing
  108. Point clickLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
  109. for (int i = 0; i < menuEntries.Count; i++)
  110. {
  111. MenuEntry menuEntry = menuEntries[i];
  112. if (menuEntry.Destination.Contains(clickLocation))
  113. {
  114. OnSelectEntry(i, PlayerIndex.One);
  115. }
  116. }
  117. }
  118. }
  119. else if (inputState.CurrentMouseState.LeftButton == ButtonState.Pressed)
  120. {
  121. isMouseDown = true;
  122. // Use transformed cursor location for proper scaling/letterboxing
  123. Point clickLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
  124. for (int i = 0; i < menuEntries.Count; i++)
  125. {
  126. MenuEntry menuEntry = menuEntries[i];
  127. if (menuEntry.Destination.Contains(clickLocation))
  128. selectedEntry = i;
  129. }
  130. }
  131. }
  132. else if (UIUtility.IsMobile)
  133. {
  134. // Handle touch input with transformed coordinates
  135. foreach (GestureSample gesture in inputState.Gestures)
  136. {
  137. if (gesture.GestureType == GestureType.Tap)
  138. {
  139. // Use transformed cursor location (InputState already transforms gesture position)
  140. Point tapLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
  141. for (int i = 0; i < menuEntries.Count; i++)
  142. {
  143. MenuEntry menuEntry = menuEntries[i];
  144. if (menuEntry.Destination.Contains(tapLocation))
  145. {
  146. OnSelectEntry(i, PlayerIndex.One);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. else
  153. {
  154. // Handle gamepad input
  155. if (inputState.IsMenuUp(ControllingPlayer))
  156. {
  157. selectedEntry--;
  158. if (selectedEntry < 0)
  159. selectedEntry = menuEntries.Count - 1;
  160. }
  161. else if (inputState.IsMenuDown(ControllingPlayer))
  162. {
  163. selectedEntry++;
  164. if (selectedEntry >= menuEntries.Count)
  165. selectedEntry = 0;
  166. }
  167. else if (inputState.IsNewButtonPress(Buttons.A, ControllingPlayer, out player))
  168. {
  169. OnSelectEntry(selectedEntry, player);
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Handler for when the user has chosen a menu entry.
  175. /// </summary>
  176. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  177. {
  178. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  179. }
  180. /// <summary>
  181. /// Handler for when the user has cancelled the menu.
  182. /// </summary>
  183. protected virtual void OnCancel(PlayerIndex playerIndex)
  184. {
  185. ExitScreen();
  186. }
  187. /// <summary>
  188. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  189. /// </summary>
  190. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  191. {
  192. OnCancel(e.PlayerIndex);
  193. }
  194. public override void LoadContent()
  195. {
  196. bounds = ScreenManager.SafeArea;
  197. base.LoadContent();
  198. }
  199. /// <summary>
  200. /// Allows the screen the chance to position the menu entries. By default
  201. /// all menu entries are lined up in a vertical list, centered on the screen.
  202. /// </summary>
  203. protected virtual void UpdateMenuEntryLocations()
  204. {
  205. // Make the menu slide into place during transitions, using a
  206. // power curve to make things look more interesting (this makes
  207. // the movement slow down as it nears the end).
  208. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  209. // start at Y = 475; each X value is generated per entry
  210. Vector2 position = new Vector2(0f,
  211. ScreenManager.Game.Window.ClientBounds.Height / 2 -
  212. (menuEntries[0].GetHeight(this) + (menuEntryPadding * 2) * menuEntries.Count));
  213. // update each menu entry's location in turn
  214. for (int i = 0; i < menuEntries.Count; i++)
  215. {
  216. MenuEntry menuEntry = menuEntries[i];
  217. // each entry is to be centered horizontally
  218. position.X = ScreenManager.BASE_BUFFER_WIDTH / 2 - menuEntry.GetWidth(this) / 2;
  219. if (ScreenState == ScreenState.TransitionOn)
  220. position.X -= transitionOffset * 256;
  221. else
  222. position.X += transitionOffset * 512;
  223. // set the entry's position
  224. //menuEntry.Position = position;
  225. // move down for the next entry the size of this entry plus our padding
  226. position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2);
  227. }
  228. }
  229. /// <summary>
  230. /// Updates the menu.
  231. /// </summary>
  232. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  233. bool coveredByOtherScreen)
  234. {
  235. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  236. // Update each nested MenuEntry object.
  237. for (int i = 0; i < menuEntries.Count; i++)
  238. {
  239. bool isSelected = IsActive && (i == selectedEntry);
  240. UpdateMenuEntryDestination();
  241. menuEntries[i].Update(this, isSelected, gameTime);
  242. }
  243. }
  244. /// <summary>
  245. /// Draws the menu.
  246. /// </summary>
  247. public override void Draw(GameTime gameTime)
  248. {
  249. // make sure our entries are in the right place before we draw them
  250. //UpdateMenuEntryLocations();
  251. GraphicsDevice graphicsDevice = ScreenManager.GraphicsDevice;
  252. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  253. SpriteFont font = ScreenManager.Font;
  254. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  255. // Draw each menu entry in turn.
  256. for (int i = 0; i < menuEntries.Count; i++)
  257. {
  258. MenuEntry menuEntry = menuEntries[i];
  259. bool isSelected = IsActive && (i == selectedEntry);
  260. menuEntry.Draw(this, isSelected, gameTime);
  261. }
  262. // Make the menu slide into place during transitions, using a
  263. // power curve to make things look more interesting (this makes
  264. // the movement slow down as it nears the end).
  265. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  266. // Draw the menu title in the top third of the screen
  267. Vector2 titlePosition = new Vector2(
  268. ScreenManager.BaseScreenSize.X / 2,
  269. ScreenManager.SafeArea.Top + 80); // Position in top third instead of center
  270. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  271. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  272. float titleScale = 1.25f;
  273. titlePosition.Y -= transitionOffset * 100;
  274. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  275. titleOrigin, titleScale, SpriteEffects.None, 0);
  276. spriteBatch.End();
  277. }
  278. public void UpdateMenuEntryDestination()
  279. {
  280. Rectangle bounds = ScreenManager.SafeArea;
  281. Rectangle textureSize = ScreenManager.ButtonBackground.Bounds;
  282. int xStep = bounds.Width / (menuEntries.Count + 2);
  283. int maxWidth = 0;
  284. for (int i = 0; i < menuEntries.Count; i++)
  285. {
  286. int width = menuEntries[i].GetWidth(this);
  287. if (width > maxWidth)
  288. {
  289. maxWidth = width;
  290. }
  291. }
  292. maxWidth += 20;
  293. for (int i = 0; i < menuEntries.Count; i++)
  294. {
  295. menuEntries[i].Destination =
  296. new Rectangle(
  297. bounds.Left + (xStep - textureSize.Width) / 2 + (i + 1) * xStep,
  298. bounds.Bottom - textureSize.Height * 2, maxWidth, 50);
  299. }
  300. }
  301. }
  302. }