2
0

MenuScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. namespace CatapultGame
  14. {
  15. /// <summary>
  16. /// Base class for screens that contain a menu of options. The user can
  17. /// move up and down to select an entry, or cancel to back out of the screen.
  18. /// </summary>
  19. abstract class MenuScreen : GameScreen
  20. {
  21. // the number of pixels to pad above and below menu entries for touch input
  22. //const int menuEntryPadding = 35;
  23. const int menuEntryPadding = 10;
  24. List<MenuEntry> menuEntries = new List<MenuEntry>();
  25. int selectedEntry = 0;
  26. string menuTitle;
  27. /// <summary>
  28. /// Gets the list of menu entries, so derived classes can add
  29. /// or change the menu contents.
  30. /// </summary>
  31. protected IList<MenuEntry> MenuEntries
  32. {
  33. get { return menuEntries; }
  34. }
  35. /// <summary>
  36. /// Constructor.
  37. /// </summary>
  38. public MenuScreen(string menuTitle)
  39. {
  40. // menus generally only need Tap for menu selection
  41. EnabledGestures = GestureType.Tap;
  42. this.menuTitle = menuTitle;
  43. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  44. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  45. }
  46. /// <summary>
  47. /// Allows the screen to create the hit bounds for a particular menu entry.
  48. /// </summary>
  49. protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry)
  50. {
  51. // the hit bounds are the entire width of the screen, and the height of the entry
  52. // with some additional padding above and below.
  53. return new Rectangle(
  54. 0,
  55. (int)entry.Position.Y - menuEntryPadding,
  56. ScreenManager.BASE_BUFFER_WIDTH,
  57. entry.GetHeight(this) + (menuEntryPadding * 2));
  58. }
  59. /// <summary>
  60. /// Responds to user input, changing the selected entry and accepting
  61. /// or cancelling the menu.
  62. /// </summary>
  63. public override void HandleInput(InputState input)
  64. {
  65. // we cancel the current menu screen if the user presses the back button
  66. PlayerIndex player;
  67. if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
  68. {
  69. OnCancel(player);
  70. }
  71. if (input.IsMenuDown(ControllingPlayer))
  72. {
  73. if (selectedEntry < menuEntries.Count - 1)
  74. selectedEntry += 1;
  75. }
  76. if (input.IsMenuUp(ControllingPlayer))
  77. {
  78. if (selectedEntry > 0)
  79. selectedEntry -= 1;
  80. }
  81. if (input.IsMenuSelect(ControllingPlayer, out player))
  82. {
  83. menuEntries[selectedEntry].OnSelectEntry(player);
  84. }
  85. if (input.MouseGesture.HasFlag(MouseGestureType.LeftClick))
  86. {
  87. Point clickLocation = new Point((int)input.CurrentMousePosition.X, (int)input.CurrentMousePosition.Y);
  88. // iterate the entries to see if any were tapped
  89. for (int i = 0; i < menuEntries.Count; i++)
  90. {
  91. MenuEntry menuEntry = menuEntries[i];
  92. if (GetMenuEntryHitBounds(menuEntry).Contains(clickLocation))
  93. {
  94. // select the entry. since gestures are only available on Mobiles,
  95. // we can safely pass PlayerIndex.One to all entries since there is only
  96. // one player on Mobiles.
  97. OnSelectEntry(i, PlayerIndex.One);
  98. }
  99. }
  100. }
  101. if (input.MouseGesture.HasFlag(MouseGestureType.Move))
  102. {
  103. Point clickLocation = new Point((int)input.CurrentMousePosition.X, (int)input.CurrentMousePosition.Y);
  104. // iterate the entries to see if any were tapped
  105. for (int i = 0; i < menuEntries.Count; i++)
  106. {
  107. MenuEntry menuEntry = menuEntries[i];
  108. if (GetMenuEntryHitBounds(menuEntry).Contains(clickLocation))
  109. {
  110. // select the entry. since gestures are only available on Mobiles,
  111. // we can safely pass PlayerIndex.One to all entries since there is only
  112. // one player on Mobiles.
  113. //OnSelectEntry(i, PlayerIndex.One);
  114. selectedEntry = i;
  115. }
  116. }
  117. }
  118. // look for any taps that occurred and select any entries that were tapped
  119. foreach (GestureSample gesture in input.Gestures)
  120. {
  121. if (gesture.GestureType == GestureType.Tap)
  122. {
  123. // convert the position to a Point that we can test against a Rectangle
  124. Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
  125. // iterate the entries to see if any were tapped
  126. for (int i = 0; i < menuEntries.Count; i++)
  127. {
  128. MenuEntry menuEntry = menuEntries[i];
  129. if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
  130. {
  131. // select the entry. since gestures are only available on Mobiles,
  132. // we can safely pass PlayerIndex.One to all entries since there is only
  133. // one player on Mobiles.
  134. OnSelectEntry(i, PlayerIndex.One);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Handler for when the user has chosen a menu entry.
  142. /// </summary>
  143. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  144. {
  145. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  146. }
  147. /// <summary>
  148. /// Handler for when the user has cancelled the menu.
  149. /// </summary>
  150. protected virtual void OnCancel(PlayerIndex playerIndex)
  151. {
  152. ExitScreen();
  153. }
  154. /// <summary>
  155. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  156. /// </summary>
  157. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  158. {
  159. OnCancel(e.PlayerIndex);
  160. }
  161. /// <summary>
  162. /// Allows the screen the chance to position the menu entries. By default
  163. /// all menu entries are lined up in a vertical list, centered on the screen.
  164. /// </summary>
  165. protected virtual void UpdateMenuEntryLocations()
  166. {
  167. // Make the menu slide into place during transitions, using a
  168. // power curve to make things look more interesting (this makes
  169. // the movement slow down as it nears the end).
  170. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  171. // start at Y = 175; each X value is generated per entry
  172. Vector2 position = new Vector2(0f, 175f);
  173. // update each menu entry's location in turn
  174. for (int i = 0; i < menuEntries.Count; i++)
  175. {
  176. MenuEntry menuEntry = menuEntries[i];
  177. // each entry is to be centered horizontally
  178. position.X = ScreenManager.BASE_BUFFER_WIDTH / 2 - menuEntry.GetWidth(this) / 2;
  179. if (ScreenState == ScreenState.TransitionOn)
  180. position.X -= transitionOffset * 256;
  181. else
  182. position.X += transitionOffset * 512;
  183. // set the entry's position
  184. menuEntry.Position = position;
  185. // move down for the next entry the size of this entry plus our padding
  186. position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2);
  187. }
  188. }
  189. /// <summary>
  190. /// Updates the menu.
  191. /// </summary>
  192. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  193. bool coveredByOtherScreen)
  194. {
  195. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  196. // Update each nested MenuEntry object.
  197. for (int i = 0; i < menuEntries.Count; i++)
  198. {
  199. bool isSelected = IsActive && (i == selectedEntry);
  200. // if (!menuEntries[i].HasMouseEnteredAttached)
  201. // menuEntries [i].MouseEntered += MouseEntered;
  202. // if (!menuEntries[i].HasMouseClickedAttached)
  203. // menuEntries [i].MouseClicked += MouseClicked;
  204. menuEntries[i].Update(this, isSelected, gameTime);
  205. }
  206. }
  207. // void MouseClicked (object sender, MenuEntryEventArgs e)
  208. // {
  209. // e.MenuEntry.OnSelectEntry(PlayerIndex.One);
  210. // }
  211. //
  212. // void MouseEntered (object sender, MenuEntryEventArgs e)
  213. // {
  214. // //Console.WriteLine("Mouse Entered menu item " + e.MenuEntry.Text);
  215. // selectedEntry = menuEntries.IndexOf(e.MenuEntry);
  216. // }
  217. /// <summary>
  218. /// Draws the menu.
  219. /// </summary>
  220. public override void Draw(GameTime gameTime)
  221. {
  222. // make sure our entries are in the right place before we draw them
  223. UpdateMenuEntryLocations();
  224. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  225. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  226. SpriteFont font = ScreenManager.Font;
  227. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  228. // Draw each menu entry in turn.
  229. for (int i = 0; i < menuEntries.Count; i++)
  230. {
  231. MenuEntry menuEntry = menuEntries[i];
  232. bool isSelected = IsActive && (i == selectedEntry);
  233. menuEntry.Draw(this, isSelected, gameTime);
  234. }
  235. // Make the menu slide into place during transitions, using a
  236. // power curve to make things look more interesting (this makes
  237. // the movement slow down as it nears the end).
  238. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  239. // Draw the menu title centered on the screen
  240. Vector2 titlePosition = new Vector2(ScreenManager.BASE_BUFFER_WIDTH / 2, 80);
  241. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  242. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  243. float titleScale = 1.25f;
  244. titlePosition.Y -= transitionOffset * 100;
  245. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  246. titleOrigin, titleScale, SpriteEffects.None, 0);
  247. spriteBatch.End();
  248. }
  249. }
  250. }