ScreenManager.cs 8.8 KB

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