ScreenManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 System.Text;
  14. namespace RolePlaying
  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. /// <remarks>
  23. /// Similar to a class found in the Game State Management sample on the
  24. /// XNA Creators Club Online website (http://creators.xna.com).
  25. /// </remarks>
  26. public class ScreenManager : DrawableGameComponent
  27. {
  28. List<GameScreen> screens = new List<GameScreen>();
  29. List<GameScreen> screensToUpdate = new List<GameScreen>();
  30. SpriteBatch spriteBatch;
  31. bool isInitialized;
  32. bool traceEnabled;
  33. /// <summary>
  34. /// A default SpriteBatch shared by all the screens. This saves
  35. /// each screen having to bother creating their own local instance.
  36. /// </summary>
  37. public SpriteBatch SpriteBatch
  38. {
  39. get { return spriteBatch; }
  40. }
  41. /// <summary>
  42. /// If true, the manager prints out a list of all the screens
  43. /// each time it is updated. This can be useful for making sure
  44. /// everything is being added and removed at the right times.
  45. /// </summary>
  46. public bool TraceEnabled
  47. {
  48. get { return traceEnabled; }
  49. set { traceEnabled = value; }
  50. }
  51. /// <summary>
  52. /// Constructs a new screen manager component.
  53. /// </summary>
  54. public ScreenManager(Game game)
  55. : base(game)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes the screen manager component.
  60. /// </summary>
  61. public override void Initialize()
  62. {
  63. base.Initialize();
  64. isInitialized = true;
  65. }
  66. /// <summary>
  67. /// Load your graphics content.
  68. /// </summary>
  69. protected override void LoadContent()
  70. {
  71. // Load content belonging to the screen manager.
  72. ContentManager content = Game.Content;
  73. spriteBatch = new SpriteBatch(GraphicsDevice);
  74. // Tell each of the screens to load their content.
  75. foreach (GameScreen screen in screens)
  76. {
  77. screen.LoadContent();
  78. }
  79. }
  80. /// <summary>
  81. /// Unload your graphics content.
  82. /// </summary>
  83. protected override void UnloadContent()
  84. {
  85. // Tell each of the screens to unload their content.
  86. foreach (GameScreen screen in screens)
  87. {
  88. screen.UnloadContent();
  89. }
  90. }
  91. /// <summary>
  92. /// Allows each screen to run logic.
  93. /// </summary>
  94. public override void Update(GameTime gameTime)
  95. {
  96. // Make a copy of the master screen list, to avoid confusion if
  97. // the process of updating one screen adds or removes others.
  98. screensToUpdate.Clear();
  99. foreach (GameScreen screen in screens)
  100. screensToUpdate.Add(screen);
  101. bool otherScreenHasFocus = !Game.IsActive;
  102. bool coveredByOtherScreen = false;
  103. // Loop as long as there are screens waiting to be updated.
  104. while (screensToUpdate.Count > 0)
  105. {
  106. // Pop the topmost screen off the waiting list.
  107. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
  108. screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
  109. // Update the screen.
  110. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  111. if (screen.ScreenState == ScreenState.TransitionOn ||
  112. screen.ScreenState == ScreenState.Active)
  113. {
  114. // If this is the first active screen we came across,
  115. // give it a chance to handle input.
  116. if (!otherScreenHasFocus)
  117. {
  118. screen.HandleInput();
  119. otherScreenHasFocus = true;
  120. }
  121. // If this is an active non-popup, inform any subsequent
  122. // screens that they are covered by it.
  123. if (!screen.IsPopup)
  124. coveredByOtherScreen = true;
  125. }
  126. }
  127. // Print debug trace?
  128. if (traceEnabled)
  129. TraceScreens();
  130. }
  131. /// <summary>
  132. /// Prints a list of all the screens, for debugging.
  133. /// </summary>
  134. void TraceScreens()
  135. {
  136. List<string> screenNames = new List<string>();
  137. foreach (GameScreen screen in screens)
  138. screenNames.Add(screen.GetType().Name);
  139. #if WINDOWS
  140. Trace.WriteLine(string.Join(", ", screenNames.ToArray()));
  141. #endif
  142. }
  143. /// <summary>
  144. /// Tells each screen to draw itself.
  145. /// </summary>
  146. public override void Draw(GameTime gameTime)
  147. {
  148. foreach (GameScreen screen in screens)
  149. {
  150. if (screen.ScreenState == ScreenState.Hidden)
  151. continue;
  152. screen.Draw(gameTime);
  153. }
  154. }
  155. /// <summary>
  156. /// Adds a new screen to the screen manager.
  157. /// </summary>
  158. public void AddScreen(GameScreen screen)
  159. {
  160. screen.ScreenManager = this;
  161. screen.IsExiting = false;
  162. // If we have a graphics device, tell the screen to load content.
  163. if (isInitialized)
  164. {
  165. screen.LoadContent();
  166. }
  167. screens.Add(screen);
  168. }
  169. /// <summary>
  170. /// Removes a screen from the screen manager. You should normally
  171. /// use GameScreen.ExitScreen instead of calling this directly, so
  172. /// the screen can gradually transition off rather than just being
  173. /// instantly removed.
  174. /// </summary>
  175. public void RemoveScreen(GameScreen screen)
  176. {
  177. // If we have a graphics device, tell the screen to unload content.
  178. if (isInitialized)
  179. {
  180. screen.UnloadContent();
  181. }
  182. screens.Remove(screen);
  183. screensToUpdate.Remove(screen);
  184. }
  185. /// <summary>
  186. /// Expose an array holding all the screens. We return a copy rather
  187. /// than the real master list, because screens should only ever be added
  188. /// or removed using the AddScreen and RemoveScreen methods.
  189. /// </summary>
  190. public GameScreen[] GetScreens()
  191. {
  192. return screens.ToArray();
  193. }
  194. }
  195. }