GameScreenManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameScreenManager.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 RobotGameData.Resource;
  17. using RobotGameData.GameObject;
  18. #endregion
  19. namespace RobotGameData.Screen
  20. {
  21. /// <summary>
  22. /// The screen manager is a component which manages one or more GameScreen
  23. /// instances. It maintains a stack of screens, calls their Update and Draw
  24. /// methods at the appropriate times, and automatically routes input to the
  25. /// topmost active screen.
  26. /// </summary>
  27. public class GameScreenManager : DrawableGameComponent
  28. {
  29. #region Fields
  30. List<GameScreen> screens = new List<GameScreen>();
  31. List<GameScreen> screensToUpdate = new List<GameScreen>();
  32. GameScreenInput[] screenInput = new GameScreenInput[4];
  33. GameScreen currentScreen = null;
  34. GameSceneNode scene2DFadeRoot = null;
  35. GameSprite2D fadeSprite = null;
  36. Sprite2DObject fadeObject = null;
  37. bool traceEnabled;
  38. #endregion
  39. #region Properties
  40. /// <summary>
  41. /// Expose access to our Game instance (this is protected in the
  42. /// default GameComponent, but we want to make it public).
  43. /// </summary>
  44. new public Game Game
  45. {
  46. get { return base.Game; }
  47. }
  48. /// <summary>
  49. /// A content manager used to load data that is shared between multiple
  50. /// screens. This is never unloaded, so if a screen requires a large amount
  51. /// of temporary data, it should create a local content manager instead.
  52. /// </summary>
  53. public static ContentManager Content
  54. {
  55. get { return FrameworkCore.ContentManager; }
  56. }
  57. /// <summary>
  58. /// A default SpriteBatch shared by all the screens. This saves
  59. /// each screen having to bother creating their own local instance.
  60. /// </summary>
  61. public static SpriteBatch SpriteBatch
  62. {
  63. get { return FrameworkCore.RenderContext.SpriteBatch; }
  64. }
  65. /// <summary>
  66. /// If true, the manager prints out a list of all the screens
  67. /// each time it is updated. This can be useful for making sure
  68. /// everything is being added and removed at the right times.
  69. /// </summary>
  70. public bool TraceEnabled
  71. {
  72. get { return traceEnabled; }
  73. set { traceEnabled = value; }
  74. }
  75. public GameScreen CurrentScreen
  76. {
  77. get { return currentScreen; }
  78. }
  79. public int InputCount
  80. {
  81. get { return screenInput.Length; }
  82. }
  83. public GameScreenInput SingleInput
  84. {
  85. get { return screenInput[0]; }
  86. }
  87. public GameScreenInput[] ScreenInput
  88. {
  89. get { return screenInput; }
  90. }
  91. #endregion
  92. /// <summary>
  93. /// Constructs a new screen manager component.
  94. /// </summary>
  95. public GameScreenManager(Game game)
  96. : base(game)
  97. {
  98. scene2DFadeRoot = FrameworkCore.Scene2DFadeLayer;
  99. fadeSprite = new GameSprite2D();
  100. scene2DFadeRoot.AddChild(fadeSprite);
  101. // Controller
  102. screenInput[0] = new GameScreenInput(PlayerIndex.One);
  103. screenInput[1] = new GameScreenInput(PlayerIndex.Two);
  104. screenInput[2] = new GameScreenInput(PlayerIndex.Three);
  105. screenInput[3] = new GameScreenInput(PlayerIndex.Four);
  106. }
  107. public override void Initialize()
  108. {
  109. base.Initialize();
  110. }
  111. /// <summary>
  112. /// Load your graphics content.
  113. /// </summary>
  114. protected override void LoadContent()
  115. {
  116. // Load content belonging to the screen manager.
  117. fadeSprite.Create(1, "blank");
  118. fadeObject = fadeSprite.AddSprite(0, "Screen fade");
  119. fadeObject.ScreenSize =
  120. new Vector2(FrameworkCore.ViewWidth, FrameworkCore.ViewHeight);
  121. fadeObject.Visible = false;
  122. // Tell each of the screens to load their content.
  123. foreach (GameScreen screen in screens)
  124. {
  125. screen.LoadContent();
  126. }
  127. }
  128. public void OnSize(Rectangle newRect)
  129. {
  130. if (fadeObject != null)
  131. fadeObject.ScreenSize = new Vector2(newRect.Width, newRect.Height);
  132. foreach (GameScreen screen in screens)
  133. {
  134. screen.OnSize(newRect);
  135. }
  136. }
  137. /// <summary>
  138. /// Unload your graphics content.
  139. /// </summary>
  140. protected override void UnloadContent()
  141. {
  142. // Unload content belonging to the screen manager.
  143. scene2DFadeRoot.RemoveAllChild(true);
  144. // Tell each of the screens to unload their content.
  145. foreach (GameScreen screen in screens)
  146. {
  147. screen.UnloadContent();
  148. }
  149. }
  150. #region Update and Draw
  151. /// <summary>
  152. /// Allows each screen to run logic.
  153. /// </summary>
  154. public override void Update(GameTime gameTime)
  155. {
  156. // Make a copy of the master screen list, to avoid confusion if
  157. // the process of updating one screen adds or removes others.
  158. screensToUpdate.Clear();
  159. foreach (GameScreen screen in screens)
  160. screensToUpdate.Add(screen);
  161. bool otherScreenHasFocus = !Game.IsActive;
  162. bool coveredByOtherScreen = false;
  163. // Loop as long as there are screens waiting to be updated.
  164. while (screensToUpdate.Count > 0)
  165. {
  166. // Pop the topmost screen off the waiting list.
  167. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
  168. screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
  169. // Update the screen.
  170. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  171. if (screen.ScreenState == ScreenState.TransitionOn ||
  172. screen.ScreenState == ScreenState.Active)
  173. {
  174. // If this is the first active screen we came across,
  175. // give it a chance to handle input.
  176. if (!otherScreenHasFocus)
  177. {
  178. screen.HandleInput(gameTime);
  179. otherScreenHasFocus = true;
  180. }
  181. // If this is an active non-popup, inform any subsequent
  182. // screens that they are covered by it.
  183. if (!screen.IsPopup)
  184. coveredByOtherScreen = true;
  185. }
  186. }
  187. // Print debug trace?
  188. if (traceEnabled)
  189. TraceScreens();
  190. }
  191. /// <summary>
  192. /// Prints a list of all the screens, for debugging.
  193. /// </summary>
  194. void TraceScreens()
  195. {
  196. List<string> screenNames = new List<string>();
  197. foreach (GameScreen screen in screens)
  198. screenNames.Add(screen.GetType().Name);
  199. Trace.WriteLine(string.Join(", ", screenNames.ToArray()));
  200. }
  201. /// <summary>
  202. /// Tells each screen to draw itself.
  203. /// </summary>
  204. public override void Draw(GameTime gameTime)
  205. {
  206. foreach (GameScreen screen in screens)
  207. {
  208. if (screen.ScreenState == ScreenState.Hidden)
  209. continue;
  210. screen.Draw(gameTime);
  211. }
  212. }
  213. #endregion
  214. /// <summary>
  215. /// Adds a new screen to the screen manager.
  216. /// </summary>
  217. public void AddScreen(GameScreen screen, bool callLoadContent)
  218. {
  219. screen.GameScreenManager = this;
  220. // If we have a graphics device, tell the screen to load content.
  221. if (GraphicsDevice != null && callLoadContent)
  222. {
  223. screen.LoadContent();
  224. }
  225. screen.InitializeScreen();
  226. screens.Add(screen);
  227. currentScreen = screen;
  228. for (int i = 0; i < screenInput.Length; i++)
  229. screenInput[i].Reset();
  230. }
  231. /// <summary>
  232. /// Removes a screen from the screen manager. You should normally
  233. /// use GameScreen.ExitScreen instead of calling this directly, so
  234. /// the screen can gradually transition off rather than just being
  235. /// instantly removed.
  236. /// </summary>
  237. public void RemoveScreen(GameScreen screen)
  238. {
  239. screen.FinalizeScreen();
  240. // If we have a graphics device, tell the screen to unload content.
  241. if (GraphicsDevice != null)
  242. {
  243. screen.UnloadContent();
  244. }
  245. if (currentScreen == screen)
  246. currentScreen = null;
  247. screens.Remove(screen);
  248. screensToUpdate.Remove(screen);
  249. }
  250. /// <summary>
  251. /// Expose an array holding all the screens. We return a copy rather
  252. /// than the real master list, because screens should only ever be added
  253. /// or removed using the AddScreen and RemoveScreen methods.
  254. /// </summary>
  255. public GameScreen[] GetScreens()
  256. {
  257. return screens.ToArray();
  258. }
  259. /// <summary>
  260. /// Helper draws a translucent black fullscreen sprite, used for fading
  261. /// screens in and out, and for darkening the background behind popups.
  262. /// </summary>
  263. public void FadeBackBufferToBlack(int alpha)
  264. {
  265. if (alpha > 0)
  266. {
  267. fadeObject.Visible = true;
  268. fadeObject.Color = new Color(0, 0, 0, (byte)alpha);
  269. }
  270. else
  271. {
  272. fadeObject.Visible = false;
  273. }
  274. }
  275. }
  276. }