2
0

MenuScreen.cs 6.5 KB

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