2
0

ScreenManager.cs 7.8 KB

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