MenuScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. 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. // 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 (UIUtility.IsDesktop)
  83. {
  84. // Handle keyboard input
  85. if (input.IsMenuUp(ControllingPlayer))
  86. {
  87. selectedEntry--;
  88. if (selectedEntry < 0)
  89. selectedEntry = menuEntries.Count - 1;
  90. }
  91. else if (input.IsMenuDown(ControllingPlayer))
  92. {
  93. selectedEntry++;
  94. if (selectedEntry >= menuEntries.Count)
  95. selectedEntry = 0;
  96. }
  97. else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
  98. input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
  99. {
  100. OnSelectEntry(selectedEntry, player);
  101. }
  102. // Handle mouse input using InputState
  103. if (input.CurrentMouseState.LeftButton == ButtonState.Released)
  104. {
  105. if (isMouseDown)
  106. {
  107. isMouseDown = false;
  108. Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.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 (input.CurrentMouseState.LeftButton == ButtonState.Pressed)
  120. {
  121. isMouseDown = true;
  122. Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);
  123. for (int i = 0; i < menuEntries.Count; i++)
  124. {
  125. MenuEntry menuEntry = menuEntries[i];
  126. if (menuEntry.Destination.Contains(clickLocation))
  127. selectedEntry = i;
  128. }
  129. }
  130. }
  131. else if (UIUtility.IsMobile)
  132. {
  133. // Handle touch input
  134. foreach (GestureSample gesture in input.Gestures)
  135. {
  136. if (gesture.GestureType == GestureType.Tap)
  137. {
  138. Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
  139. for (int i = 0; i < menuEntries.Count; i++)
  140. {
  141. MenuEntry menuEntry = menuEntries[i];
  142. if (menuEntry.Destination.Contains(tapLocation))
  143. {
  144. OnSelectEntry(i, PlayerIndex.One);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. else
  151. {
  152. // Handle gamepad input
  153. if (input.IsMenuUp(ControllingPlayer))
  154. {
  155. selectedEntry--;
  156. if (selectedEntry < 0)
  157. selectedEntry = menuEntries.Count - 1;
  158. }
  159. else if (input.IsMenuDown(ControllingPlayer))
  160. {
  161. selectedEntry++;
  162. if (selectedEntry >= menuEntries.Count)
  163. selectedEntry = 0;
  164. }
  165. else if (input.IsNewButtonPress(Buttons.A, ControllingPlayer, out player))
  166. {
  167. OnSelectEntry(selectedEntry, player);
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Handler for when the user has chosen a menu entry.
  173. /// </summary>
  174. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  175. {
  176. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  177. }
  178. /// <summary>
  179. /// Handler for when the user has cancelled the menu.
  180. /// </summary>
  181. protected virtual void OnCancel(PlayerIndex playerIndex)
  182. {
  183. ExitScreen();
  184. }
  185. /// <summary>
  186. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  187. /// </summary>
  188. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  189. {
  190. OnCancel(e.PlayerIndex);
  191. }
  192. public override void LoadContent()
  193. {
  194. bounds = ScreenManager.SafeArea;
  195. base.LoadContent();
  196. }
  197. /// <summary>
  198. /// Allows the screen the chance to position the menu entries. By default
  199. /// all menu entries are lined up in a vertical list, centered on the screen.
  200. /// </summary>
  201. protected virtual void UpdateMenuEntryLocations()
  202. {
  203. // Make the menu slide into place during transitions, using a
  204. // power curve to make things look more interesting (this makes
  205. // the movement slow down as it nears the end).
  206. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  207. // start at Y = 475; each X value is generated per entry
  208. Vector2 position = new Vector2(0f,
  209. ScreenManager.Game.Window.ClientBounds.Height / 2 -
  210. (menuEntries[0].GetHeight(this) + (menuEntryPadding * 2) * menuEntries.Count));
  211. // update each menu entry's location in turn
  212. for (int i = 0; i < menuEntries.Count; i++)
  213. {
  214. MenuEntry menuEntry = menuEntries[i];
  215. // each entry is to be centered horizontally
  216. position.X = ScreenManager.BASE_BUFFER_WIDTH / 2 - menuEntry.GetWidth(this) / 2;
  217. if (ScreenState == ScreenState.TransitionOn)
  218. position.X -= transitionOffset * 256;
  219. else
  220. position.X += transitionOffset * 512;
  221. // set the entry's position
  222. //menuEntry.Position = position;
  223. // move down for the next entry the size of this entry plus our padding
  224. position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2);
  225. }
  226. }
  227. /// <summary>
  228. /// Updates the menu.
  229. /// </summary>
  230. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  231. bool coveredByOtherScreen)
  232. {
  233. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  234. // Update each nested MenuEntry object.
  235. for (int i = 0; i < menuEntries.Count; i++)
  236. {
  237. bool isSelected = IsActive && (i == selectedEntry);
  238. UpdateMenuEntryDestination();
  239. menuEntries[i].Update(this, isSelected, gameTime);
  240. }
  241. }
  242. /// <summary>
  243. /// Draws the menu.
  244. /// </summary>
  245. public override void Draw(GameTime gameTime)
  246. {
  247. // make sure our entries are in the right place before we draw them
  248. //UpdateMenuEntryLocations();
  249. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  250. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  251. SpriteFont font = ScreenManager.Font;
  252. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  253. // Draw each menu entry in turn.
  254. for (int i = 0; i < menuEntries.Count; i++)
  255. {
  256. MenuEntry menuEntry = menuEntries[i];
  257. bool isSelected = IsActive && (i == selectedEntry);
  258. menuEntry.Draw(this, isSelected, gameTime);
  259. }
  260. // Make the menu slide into place during transitions, using a
  261. // power curve to make things look more interesting (this makes
  262. // the movement slow down as it nears the end).
  263. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  264. // Draw the menu title in the top third of the screen
  265. Vector2 titlePosition = new Vector2(
  266. graphics.Viewport.Width / 2,
  267. ScreenManager.SafeArea.Top + 80); // Position in top third instead of center
  268. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  269. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  270. float titleScale = 1.25f;
  271. titlePosition.Y -= transitionOffset * 100;
  272. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  273. titleOrigin, titleScale, SpriteEffects.None, 0);
  274. spriteBatch.End();
  275. }
  276. public void UpdateMenuEntryDestination()
  277. {
  278. Rectangle bounds = ScreenManager.SafeArea;
  279. Rectangle textureSize = ScreenManager.ButtonBackground.Bounds;
  280. int xStep = bounds.Width / (menuEntries.Count + 2);
  281. int maxWidth = 0;
  282. for (int i = 0; i < menuEntries.Count; i++)
  283. {
  284. int width = menuEntries[i].GetWidth(this);
  285. if (width > maxWidth)
  286. {
  287. maxWidth = width;
  288. }
  289. }
  290. maxWidth += 20;
  291. for (int i = 0; i < menuEntries.Count; i++)
  292. {
  293. menuEntries[i].Destination =
  294. new Rectangle(
  295. bounds.Left + (xStep - textureSize.Width) / 2 + (i + 1) * xStep,
  296. bounds.Bottom - textureSize.Height * 2, maxWidth, 50);
  297. }
  298. }
  299. }
  300. }