ScreenManager.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ScreenManager.cs
  4. //
  5. // Microsoft 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.Diagnostics;
  12. using System.Collections.Generic;
  13. #if IPHONE
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Graphics;
  17. #else
  18. using Microsoft.Xna.Framework;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.Graphics;
  21. #endif
  22. #endregion
  23. namespace Microsoft.Xna.Samples.GameStateManagement
  24. {
  25. /// <summary>
  26. /// The screen manager is a component which manages one or more GameScreen
  27. /// instances. It maintains a stack of screens, calls their Update and Draw
  28. /// methods at the appropriate times, and automatically routes input to the
  29. /// topmost active screen.
  30. /// </summary>
  31. public class ScreenManager : DrawableGameComponent
  32. {
  33. #region Fields
  34. List<GameScreen> screens = new List<GameScreen>();
  35. List<GameScreen> screensToUpdate = new List<GameScreen>();
  36. InputState input = new InputState();
  37. SpriteBatch spriteBatch;
  38. SpriteFont font;
  39. Texture2D blankTexture;
  40. bool isInitialized;
  41. bool traceEnabled;
  42. #endregion
  43. #region Properties
  44. /// <summary>
  45. /// A default SpriteBatch shared by all the screens. This saves
  46. /// each screen having to bother creating their own local instance.
  47. /// </summary>
  48. public SpriteBatch SpriteBatch
  49. {
  50. get { return spriteBatch; }
  51. }
  52. /// <summary>
  53. /// A default font shared by all the screens. This saves
  54. /// each screen having to bother loading their own local copy.
  55. /// </summary>
  56. public SpriteFont Font
  57. {
  58. get { return font; }
  59. }
  60. /// <summary>
  61. /// If true, the manager prints out a list of all the screens
  62. /// each time it is updated. This can be useful for making sure
  63. /// everything is being added and removed at the right times.
  64. /// </summary>
  65. public bool TraceEnabled
  66. {
  67. get { return traceEnabled; }
  68. set { traceEnabled = value; }
  69. }
  70. #endregion
  71. #region Initialization
  72. /// <summary>
  73. /// Constructs a new screen manager component.
  74. /// </summary>
  75. public ScreenManager(Game game)
  76. : base(game)
  77. {
  78. }
  79. /// <summary>
  80. /// Initializes the screen manager component.
  81. /// </summary>
  82. public override void Initialize()
  83. {
  84. base.Initialize();
  85. isInitialized = true;
  86. }
  87. /// <summary>
  88. /// Load your graphics content.
  89. /// </summary>
  90. protected override void LoadContent()
  91. {
  92. // Load content belonging to the screen manager.
  93. ContentManager content = Game.Content;
  94. spriteBatch = new SpriteBatch(GraphicsDevice);
  95. font = content.Load<SpriteFont>("menufont");
  96. blankTexture = content.Load<Texture2D>("blank");
  97. // Tell each of the screens to load their content.
  98. foreach (GameScreen screen in screens)
  99. {
  100. screen.LoadContent();
  101. }
  102. }
  103. /// <summary>
  104. /// Unload your graphics content.
  105. /// </summary>
  106. protected override void UnloadContent()
  107. {
  108. // Tell each of the screens to unload their content.
  109. foreach (GameScreen screen in screens)
  110. {
  111. screen.UnloadContent();
  112. }
  113. }
  114. #endregion
  115. #region Update and Draw
  116. /// <summary>
  117. /// Allows each screen to run logic.
  118. /// </summary>
  119. public override void Update(GameTime gameTime)
  120. {
  121. // Read the keyboard and gamepad.
  122. input.Update();
  123. // Make a copy of the master screen list, to avoid confusion if
  124. // the process of updating one screen adds or removes others.
  125. screensToUpdate.Clear();
  126. foreach (GameScreen screen in screens)
  127. screensToUpdate.Add(screen);
  128. bool otherScreenHasFocus = !Game.IsActive;
  129. bool coveredByOtherScreen = false;
  130. // Loop as long as there are screens waiting to be updated.
  131. while (screensToUpdate.Count > 0)
  132. {
  133. // Pop the topmost screen off the waiting list.
  134. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
  135. screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
  136. // Update the screen.
  137. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  138. if (screen.ScreenState == ScreenState.TransitionOn ||
  139. screen.ScreenState == ScreenState.Active)
  140. {
  141. // If this is the first active screen we came across,
  142. // give it a chance to handle input.
  143. if (!otherScreenHasFocus)
  144. {
  145. screen.HandleInput(input);
  146. otherScreenHasFocus = true;
  147. }
  148. // If this is an active non-popup, inform any subsequent
  149. // screens that they are covered by it.
  150. if (!screen.IsPopup)
  151. coveredByOtherScreen = true;
  152. }
  153. }
  154. // Print debug trace?
  155. if (traceEnabled)
  156. TraceScreens();
  157. }
  158. /// <summary>
  159. /// Prints a list of all the screens, for debugging.
  160. /// </summary>
  161. void TraceScreens()
  162. {
  163. List<string> screenNames = new List<string>();
  164. foreach (GameScreen screen in screens)
  165. screenNames.Add(screen.GetType().Name);
  166. Console.WriteLine(string.Join(", ", screenNames.ToArray()));
  167. }
  168. /// <summary>
  169. /// Tells each screen to draw itself.
  170. /// </summary>
  171. public override void Draw(GameTime gameTime)
  172. {
  173. foreach (GameScreen screen in screens)
  174. {
  175. if (screen.ScreenState == ScreenState.Hidden)
  176. continue;
  177. screen.Draw(gameTime);
  178. }
  179. }
  180. #endregion
  181. #region Public Methods
  182. /// <summary>
  183. /// Adds a new screen to the screen manager.
  184. /// </summary>
  185. public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
  186. {
  187. screen.ControllingPlayer = controllingPlayer;
  188. screen.ScreenManager = this;
  189. screen.IsExiting = false;
  190. // If we have a graphics device, tell the screen to load content.
  191. if (isInitialized)
  192. {
  193. screen.LoadContent();
  194. }
  195. screens.Add(screen);
  196. }
  197. /// <summary>
  198. /// Removes a screen from the screen manager. You should normally
  199. /// use GameScreen.ExitScreen instead of calling this directly, so
  200. /// the screen can gradually transition off rather than just being
  201. /// instantly removed.
  202. /// </summary>
  203. public void RemoveScreen(GameScreen screen)
  204. {
  205. // If we have a graphics device, tell the screen to unload content.
  206. if (isInitialized)
  207. {
  208. screen.UnloadContent();
  209. }
  210. screens.Remove(screen);
  211. screensToUpdate.Remove(screen);
  212. }
  213. /// <summary>
  214. /// Expose an array holding all the screens. We return a copy rather
  215. /// than the real master list, because screens should only ever be added
  216. /// or removed using the AddScreen and RemoveScreen methods.
  217. /// </summary>
  218. public GameScreen[] GetScreens()
  219. {
  220. return screens.ToArray();
  221. }
  222. /// <summary>
  223. /// Helper draws a translucent black fullscreen sprite, used for fading
  224. /// screens in and out, and for darkening the background behind popups.
  225. /// </summary>
  226. public void FadeBackBufferToBlack(int alpha)
  227. {
  228. Viewport viewport = GraphicsDevice.Viewport;
  229. spriteBatch.Begin();
  230. spriteBatch.Draw(blankTexture,
  231. new Rectangle(0, 0, viewport.Width, viewport.Height),
  232. new Color(0, 0, 0, (byte)alpha));
  233. spriteBatch.End();
  234. }
  235. #endregion
  236. }
  237. }