ScreenManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 Microsoft.Xna.Framework.GamerServices;
  17. #endregion
  18. namespace NetRumble
  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. /// This public class is similar to one in the GameStateManagement sample.
  28. /// </remarks>
  29. public class ScreenManager : DrawableGameComponent
  30. {
  31. #region Fields
  32. List<GameScreen> screens = new List<GameScreen>();
  33. List<GameScreen> screensToUpdate = new List<GameScreen>();
  34. List<GameScreen> screensToDraw = new List<GameScreen>();
  35. InputState input = new InputState();
  36. IGraphicsDeviceService graphicsDeviceService;
  37. public SignedInGamer invited;
  38. ContentManager content;
  39. SpriteBatch spriteBatch;
  40. SpriteFont font;
  41. Texture2D blankTexture;
  42. Rectangle titleSafeArea;
  43. bool traceEnabled;
  44. #endregion
  45. #region Properties
  46. /// <summary>
  47. /// Expose access to our Game instance (this is protected in the
  48. /// default GameComponent, but we want to make it public).
  49. /// </summary>
  50. new public Game Game
  51. {
  52. get { return base.Game; }
  53. }
  54. /// <summary>
  55. /// Expose access to our graphics device (this is protected in the
  56. /// default DrawableGameComponent, but we want to make it public).
  57. /// </summary>
  58. new public GraphicsDevice GraphicsDevice
  59. {
  60. get { return base.GraphicsDevice; }
  61. }
  62. /// <summary>
  63. /// A content manager used to load data that is shared between multiple
  64. /// screens. This is never unloaded, so if a screen requires a large amount
  65. /// of temporary data, it should create a local content manager instead.
  66. /// </summary>
  67. public ContentManager Content
  68. {
  69. get { return content; }
  70. }
  71. /// <summary>
  72. /// A default SpriteBatch shared by all the screens. This saves
  73. /// each screen having to bother creating their own local instance.
  74. /// </summary>
  75. public SpriteBatch SpriteBatch
  76. {
  77. get { return spriteBatch; }
  78. }
  79. /// <summary>
  80. /// A default font shared by all the screens. This saves
  81. /// each screen having to bother loading their own local copy.
  82. /// </summary>
  83. public SpriteFont Font
  84. {
  85. get { return font; }
  86. }
  87. /// <summary>
  88. /// If true, the manager prints out a list of all the screens
  89. /// each time it is updated. This can be useful for making sure
  90. /// everything is being added and removed at the right times.
  91. /// </summary>
  92. public bool TraceEnabled
  93. {
  94. get { return traceEnabled; }
  95. set { traceEnabled = value; }
  96. }
  97. /// <summary>
  98. /// The title-safe area for the menus.
  99. /// </summary>
  100. public Rectangle TitleSafeArea
  101. {
  102. get { return titleSafeArea; }
  103. }
  104. #endregion
  105. #region Initialization
  106. /// <summary>
  107. /// Constructs a new screen manager component.
  108. /// </summary>
  109. public ScreenManager(Game game)
  110. : base(game)
  111. {
  112. content = new ContentManager(game.Services, "Content");
  113. graphicsDeviceService = (IGraphicsDeviceService)game.Services.GetService(
  114. typeof(IGraphicsDeviceService));
  115. if (graphicsDeviceService == null)
  116. throw new InvalidOperationException("No graphics device service.");
  117. invited = null;
  118. }
  119. /// <summary>
  120. /// Load your graphics content.
  121. /// </summary>
  122. protected override void LoadContent()
  123. {
  124. // Load content belonging to the screen manager.
  125. spriteBatch = new SpriteBatch(GraphicsDevice);
  126. font = content.Load<SpriteFont>("Fonts/MenuFont");
  127. blankTexture = content.Load<Texture2D>("Textures/blank");
  128. // Tell each of the screens to load their content.
  129. foreach (GameScreen screen in screens)
  130. {
  131. screen.LoadContent();
  132. }
  133. // update the title-safe area
  134. titleSafeArea = new Rectangle(
  135. (int)Math.Floor(GraphicsDevice.Viewport.X +
  136. GraphicsDevice.Viewport.Width * 0.05f),
  137. (int)Math.Floor(GraphicsDevice.Viewport.Y +
  138. GraphicsDevice.Viewport.Height * 0.05f),
  139. (int)Math.Floor(GraphicsDevice.Viewport.Width * 0.9f),
  140. (int)Math.Floor(GraphicsDevice.Viewport.Height * 0.9f));
  141. }
  142. /// <summary>
  143. /// Unload your graphics content.
  144. /// </summary>
  145. protected override void UnloadContent()
  146. {
  147. // Unload content belonging to the screen manager.
  148. content.Unload();
  149. // Tell each of the screens to unload their content.
  150. foreach (GameScreen screen in screens)
  151. {
  152. screen.UnloadContent();
  153. }
  154. }
  155. #endregion
  156. #region Update and Draw
  157. /// <summary>
  158. /// Allows each screen to run logic.
  159. /// </summary>
  160. public override void Update(GameTime gameTime)
  161. {
  162. // Read the keyboard and gamepad.
  163. input.Update();
  164. // Make a copy of the master screen list, to avoid confusion if
  165. // the process of updating one screen adds or removes others
  166. // (or it happens on another thread)
  167. screensToUpdate.Clear();
  168. foreach (GameScreen screen in screens)
  169. screensToUpdate.Add(screen);
  170. bool otherScreenHasFocus = !Game.IsActive;
  171. bool coveredByOtherScreen = false;
  172. // Loop as long as there are screens waiting to be updated.
  173. while (screensToUpdate.Count > 0)
  174. {
  175. // Pop the topmost screen off the waiting list.
  176. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
  177. screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
  178. // Update the screen.
  179. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  180. if (screen.ScreenState == ScreenState.TransitionOn ||
  181. screen.ScreenState == ScreenState.Active)
  182. {
  183. // If this is the first active screen we came across,
  184. // give it a chance to handle input and update presence.
  185. if (!otherScreenHasFocus)
  186. {
  187. screen.HandleInput(input);
  188. screen.UpdatePresence(); // presence support
  189. otherScreenHasFocus = true;
  190. }
  191. // If this is an active non-popup, inform any subsequent
  192. // screens that they are covered by it.
  193. if (!screen.IsPopup)
  194. coveredByOtherScreen = true;
  195. }
  196. }
  197. // Print debug trace?
  198. if (traceEnabled)
  199. TraceScreens();
  200. }
  201. /// <summary>
  202. /// Prints a list of all the screens, for debugging.
  203. /// </summary>
  204. void TraceScreens()
  205. {
  206. List<string> screenNames = new List<string>();
  207. foreach (GameScreen screen in screens)
  208. screenNames.Add(screen.GetType().Name);
  209. Debug.WriteLine(string.Join(", ", screenNames.ToArray()));
  210. }
  211. /// <summary>
  212. /// Tells each screen to draw itself.
  213. /// </summary>
  214. public override void Draw(GameTime gameTime)
  215. {
  216. // Make a copy of the master screen list, to avoid confusion if
  217. // the process of drawing one screen adds or removes others
  218. // (or it happens on another thread
  219. screensToDraw.Clear();
  220. foreach (GameScreen screen in screens)
  221. screensToDraw.Add(screen);
  222. foreach (GameScreen screen in screensToDraw)
  223. {
  224. if (screen.ScreenState == ScreenState.Hidden)
  225. continue;
  226. screen.Draw(gameTime);
  227. }
  228. }
  229. /// <summary>
  230. /// Draw an empty rectangle of the given size and color.
  231. /// </summary>
  232. /// <param name="rectangle">The destination rectangle.</param>
  233. /// <param name="color">The color of the rectangle.</param>
  234. public void DrawRectangle(Rectangle rectangle, Color color)
  235. {
  236. //SpriteBatch.Begin();
  237. // We changed this to be Opaque
  238. spriteBatch.Begin(0,BlendState.Opaque, null, null, null);
  239. SpriteBatch.Draw(blankTexture, rectangle, color);
  240. SpriteBatch.End();
  241. }
  242. #endregion
  243. #region Public Methods
  244. /// <summary>
  245. /// Adds a new screen to the screen manager.
  246. /// </summary>
  247. public void AddScreen(GameScreen screen)
  248. {
  249. screen.ScreenManager = this;
  250. // If we have a graphics device, tell the screen to load content.
  251. if ((graphicsDeviceService != null) &&
  252. (graphicsDeviceService.GraphicsDevice != null))
  253. {
  254. screen.LoadContent();
  255. }
  256. screens.Add(screen);
  257. }
  258. /// <summary>
  259. /// Removes a screen from the screen manager. You should normally
  260. /// use GameScreen.ExitScreen instead of calling this directly, so
  261. /// the screen can gradually transition off rather than just being
  262. /// instantly removed.
  263. /// </summary>
  264. public void RemoveScreen(GameScreen screen)
  265. {
  266. // If we have a graphics device, tell the screen to unload content.
  267. if ((graphicsDeviceService != null) &&
  268. (graphicsDeviceService.GraphicsDevice != null))
  269. {
  270. screen.UnloadContent();
  271. }
  272. screens.Remove(screen);
  273. screensToUpdate.Remove(screen);
  274. }
  275. /// <summary>
  276. /// Expose an array holding all the screens. We return a copy rather
  277. /// than the real master list, because screens should only ever be added
  278. /// or removed using the AddScreen and RemoveScreen methods.
  279. /// </summary>
  280. public GameScreen[] GetScreens()
  281. {
  282. return screens.ToArray();
  283. }
  284. /// <summary>
  285. /// Helper draws a translucent black fullscreen sprite, used for fading
  286. /// screens in and out, and for darkening the background behind popups.
  287. /// </summary>
  288. public void FadeBackBufferToBlack(int alpha)
  289. {
  290. Viewport viewport = GraphicsDevice.Viewport;
  291. spriteBatch.Begin();
  292. spriteBatch.Draw(blankTexture,
  293. new Rectangle(0, 0, viewport.Width, viewport.Height),
  294. new Color(0, 0, 0, (byte)alpha));
  295. spriteBatch.End();
  296. }
  297. #endregion
  298. }
  299. }