MenuScreen.cs 13 KB

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