2
0

ScreenManager.cs 9.5 KB

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