2
0

MenuScreen.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 NetworkStateManagement
  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. List<MenuEntry> menuEntries = new List<MenuEntry>();
  27. int selectedEntry = 0;
  28. string menuTitle;
  29. #endregion
  30. #region Properties
  31. /// <summary>
  32. /// Gets the list of menu entries, so derived classes can add
  33. /// or change the menu contents.
  34. /// </summary>
  35. protected IList<MenuEntry> MenuEntries
  36. {
  37. get { return menuEntries; }
  38. }
  39. #endregion
  40. #region Initialization
  41. /// <summary>
  42. /// Constructor.
  43. /// </summary>
  44. public MenuScreen(string menuTitle)
  45. {
  46. this.menuTitle = menuTitle;
  47. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  48. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  49. }
  50. #endregion
  51. #region Handle Input
  52. /// <summary>
  53. /// Responds to user input, changing the selected entry and accepting
  54. /// or cancelling the menu.
  55. /// </summary>
  56. public override void HandleInput(InputState input)
  57. {
  58. // Move to the previous menu entry?
  59. if (input.IsMenuUp(ControllingPlayer))
  60. {
  61. selectedEntry--;
  62. if (selectedEntry < 0)
  63. selectedEntry = menuEntries.Count - 1;
  64. }
  65. // Move to the next menu entry?
  66. if (input.IsMenuDown(ControllingPlayer))
  67. {
  68. selectedEntry++;
  69. if (selectedEntry >= menuEntries.Count)
  70. selectedEntry = 0;
  71. }
  72. // Accept or cancel the menu? We pass in our ControllingPlayer, which may
  73. // either be null (to accept input from any player) or a specific index.
  74. // If we pass a null controlling player, the InputState helper returns to
  75. // us which player actually provided the input. We pass that through to
  76. // OnSelectEntry and OnCancel, so they can tell which player triggered them.
  77. PlayerIndex playerIndex;
  78. if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
  79. {
  80. OnSelectEntry(selectedEntry, playerIndex);
  81. }
  82. else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
  83. {
  84. OnCancel(playerIndex);
  85. }
  86. }
  87. /// <summary>
  88. /// Handler for when the user has chosen a menu entry.
  89. /// </summary>
  90. protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
  91. {
  92. menuEntries[entryIndex].OnSelectEntry(playerIndex);
  93. }
  94. /// <summary>
  95. /// Handler for when the user has cancelled the menu.
  96. /// </summary>
  97. protected virtual void OnCancel(PlayerIndex playerIndex)
  98. {
  99. ExitScreen();
  100. }
  101. /// <summary>
  102. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  103. /// </summary>
  104. protected void OnCancel(object sender, PlayerIndexEventArgs e)
  105. {
  106. OnCancel(e.PlayerIndex);
  107. }
  108. #endregion
  109. #region Update and Draw
  110. /// <summary>
  111. /// Allows the screen the chance to position the menu entries. By default
  112. /// all menu entries are lined up in a vertical list, centered on the screen.
  113. /// </summary>
  114. protected virtual void UpdateMenuEntryLocations()
  115. {
  116. // Make the menu slide into place during transitions, using a
  117. // power curve to make things look more interesting (this makes
  118. // the movement slow down as it nears the end).
  119. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  120. // start at Y = 175; each X value is generated per entry
  121. Vector2 position = new Vector2(0f, 175f);
  122. // update each menu entry's location in turn
  123. for (int i = 0; i < menuEntries.Count; i++)
  124. {
  125. MenuEntry menuEntry = menuEntries[i];
  126. // each entry is to be centered horizontally
  127. position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
  128. if (ScreenState == ScreenState.TransitionOn)
  129. position.X -= transitionOffset * 256;
  130. else
  131. position.X += transitionOffset * 512;
  132. // set the entry's position
  133. menuEntry.Position = position;
  134. // move down for the next entry the size of this entry
  135. position.Y += menuEntry.GetHeight(this);
  136. }
  137. }
  138. /// <summary>
  139. /// Updates the menu.
  140. /// </summary>
  141. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  142. bool coveredByOtherScreen)
  143. {
  144. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  145. // Update each nested MenuEntry object.
  146. for (int i = 0; i < menuEntries.Count; i++)
  147. {
  148. bool isSelected = IsActive && (i == selectedEntry);
  149. menuEntries[i].Update(this, isSelected, gameTime);
  150. }
  151. }
  152. /// <summary>
  153. /// Draws the menu.
  154. /// </summary>
  155. public override void Draw(GameTime gameTime)
  156. {
  157. // make sure our entries are in the right place before we draw them
  158. UpdateMenuEntryLocations();
  159. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  160. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  161. SpriteFont font = ScreenManager.Font;
  162. spriteBatch.Begin();
  163. // Draw each menu entry in turn.
  164. for (int i = 0; i < menuEntries.Count; i++)
  165. {
  166. MenuEntry menuEntry = menuEntries[i];
  167. bool isSelected = IsActive && (i == selectedEntry);
  168. menuEntry.Draw(this, isSelected, gameTime);
  169. }
  170. // Make the menu slide into place during transitions, using a
  171. // power curve to make things look more interesting (this makes
  172. // the movement slow down as it nears the end).
  173. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  174. // Draw the menu title centered on the screen
  175. Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
  176. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  177. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  178. float titleScale = 1.25f;
  179. titlePosition.Y -= transitionOffset * 100;
  180. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  181. titleOrigin, titleScale, SpriteEffects.None, 0);
  182. spriteBatch.End();
  183. }
  184. #endregion
  185. }
  186. }