MenuScreen.cs 13 KB

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