ScreenManager.cs 8.9 KB

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