MenuScreen.cs 13 KB

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