MenuScreen.cs 14 KB

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