ScreenManagerComponent.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input.Touch;
  6. namespace FarseerPhysics.SamplesFramework
  7. {
  8. /// <summary>
  9. /// The screen manager is a component which manages one or more GameScreen
  10. /// instances. It maintains a stack of screens, calls their Update and Draw
  11. /// methods at the appropriate times, and automatically routes input to the
  12. /// topmost active screen.
  13. /// </summary>
  14. public class ScreenManager : DrawableGameComponent
  15. {
  16. private AssetCreator _assetCreator;
  17. private ContentManager _contentManager;
  18. private InputHelper _input;
  19. private bool _isInitialized;
  20. private LineBatch _lineBatch;
  21. private List<GameScreen> _screens;
  22. private List<GameScreen> _screensToUpdate;
  23. private SpriteBatch _spriteBatch;
  24. /// <summary>
  25. /// Contains all the fonts avaliable for use.
  26. /// </summary>
  27. private SpriteFonts _spriteFonts;
  28. private List<RenderTarget2D> _transitions;
  29. /// <summary>
  30. /// Constructs a new screen manager component.
  31. /// </summary>
  32. public ScreenManager(Game game)
  33. : base(game)
  34. {
  35. // we must set EnabledGestures before we can query for them, but
  36. // we don't assume the game wants to read them.
  37. TouchPanel.EnabledGestures = GestureType.None;
  38. _contentManager = game.Content;
  39. _contentManager.RootDirectory = "Content";
  40. _input = new InputHelper(this);
  41. _screens = new List<GameScreen>();
  42. _screensToUpdate = new List<GameScreen>();
  43. _transitions = new List<RenderTarget2D>();
  44. }
  45. /// <summary>
  46. /// A default SpriteBatch shared by all the screens. This saves
  47. /// each screen having to bother creating their own local instance.
  48. /// </summary>
  49. public SpriteBatch SpriteBatch
  50. {
  51. get { return _spriteBatch; }
  52. }
  53. public LineBatch LineBatch
  54. {
  55. get { return _lineBatch; }
  56. }
  57. public ContentManager Content
  58. {
  59. get { return _contentManager; }
  60. }
  61. public SpriteFonts Fonts
  62. {
  63. get { return _spriteFonts; }
  64. }
  65. public AssetCreator Assets
  66. {
  67. get { return _assetCreator; }
  68. }
  69. /// <summary>
  70. /// Initializes the screen manager component.
  71. /// </summary>
  72. public override void Initialize()
  73. {
  74. _spriteFonts = new SpriteFonts(_contentManager);
  75. base.Initialize();
  76. _isInitialized = true;
  77. }
  78. /// <summary>
  79. /// Load your graphics content.
  80. /// </summary>
  81. protected override void LoadContent()
  82. {
  83. _spriteBatch = new SpriteBatch(GraphicsDevice);
  84. _lineBatch = new LineBatch(GraphicsDevice);
  85. _assetCreator = new AssetCreator(GraphicsDevice);
  86. _assetCreator.LoadContent(_contentManager);
  87. _input.LoadContent();
  88. // Tell each of the screens to load their content.
  89. foreach (GameScreen screen in _screens)
  90. {
  91. screen.LoadContent();
  92. }
  93. }
  94. /// <summary>
  95. /// Unload your graphics content.
  96. /// </summary>
  97. protected override void UnloadContent()
  98. {
  99. // Tell each of the screens to unload their content.
  100. foreach (GameScreen screen in _screens)
  101. {
  102. screen.UnloadContent();
  103. }
  104. }
  105. /// <summary>
  106. /// Allows each screen to run logic.
  107. /// </summary>
  108. public override void Update(GameTime gameTime)
  109. {
  110. // Read the keyboard and gamepad.
  111. _input.Update(gameTime);
  112. // Make a copy of the master screen list, to avoid confusion if
  113. // the process of updating one screen adds or removes others.
  114. _screensToUpdate.Clear();
  115. foreach (GameScreen screen in _screens)
  116. {
  117. _screensToUpdate.Add(screen);
  118. }
  119. bool otherScreenHasFocus = !Game.IsActive;
  120. bool coveredByOtherScreen = false;
  121. // Loop as long as there are screens waiting to be updated.
  122. while (_screensToUpdate.Count > 0)
  123. {
  124. // Pop the topmost screen off the waiting list.
  125. GameScreen screen = _screensToUpdate[_screensToUpdate.Count - 1];
  126. _screensToUpdate.RemoveAt(_screensToUpdate.Count - 1);
  127. // Update the screen.
  128. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  129. if (screen.ScreenState == ScreenState.TransitionOn ||
  130. screen.ScreenState == ScreenState.Active)
  131. {
  132. // If this is the first active screen we came across,
  133. // give it a chance to handle input.
  134. if (!otherScreenHasFocus)
  135. {
  136. _input.ShowCursor = screen.HasCursor;
  137. _input.EnableVirtualStick = screen.HasVirtualStick;
  138. screen.HandleInput(_input, gameTime);
  139. otherScreenHasFocus = true;
  140. }
  141. // If this is an active non-popup, inform any subsequent
  142. // screens that they are covered by it.
  143. if (!screen.IsPopup)
  144. {
  145. coveredByOtherScreen = true;
  146. }
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Tells each screen to draw itself.
  152. /// </summary>
  153. public override void Draw(GameTime gameTime)
  154. {
  155. int transitionCount = 0;
  156. foreach (GameScreen screen in _screens)
  157. {
  158. if (screen.ScreenState == ScreenState.TransitionOn ||
  159. screen.ScreenState == ScreenState.TransitionOff)
  160. {
  161. ++transitionCount;
  162. if (_transitions.Count < transitionCount)
  163. {
  164. PresentationParameters _pp = GraphicsDevice.PresentationParameters;
  165. _transitions.Add(new RenderTarget2D(GraphicsDevice, _pp.BackBufferWidth, _pp.BackBufferHeight,
  166. false,
  167. SurfaceFormat.Color, _pp.DepthStencilFormat,
  168. _pp.MultiSampleCount,
  169. RenderTargetUsage.DiscardContents));
  170. }
  171. GraphicsDevice.SetRenderTarget(_transitions[transitionCount - 1]);
  172. GraphicsDevice.Clear(Color.Transparent);
  173. screen.Draw(gameTime);
  174. GraphicsDevice.SetRenderTarget(null);
  175. }
  176. }
  177. GraphicsDevice.Clear(Color.Black);
  178. transitionCount = 0;
  179. foreach (GameScreen screen in _screens)
  180. {
  181. if (screen.ScreenState == ScreenState.Hidden)
  182. {
  183. continue;
  184. }
  185. if (screen.ScreenState == ScreenState.TransitionOn ||
  186. screen.ScreenState == ScreenState.TransitionOff)
  187. {
  188. _spriteBatch.Begin(0, BlendState.AlphaBlend);
  189. _spriteBatch.Draw(_transitions[transitionCount], Vector2.Zero, Color.White * screen.TransitionAlpha);
  190. _spriteBatch.End();
  191. ++transitionCount;
  192. }
  193. else
  194. {
  195. screen.Draw(gameTime);
  196. }
  197. }
  198. _input.Draw();
  199. }
  200. /// <summary>
  201. /// Adds a new screen to the screen manager.
  202. /// </summary>
  203. public void AddScreen(GameScreen screen)
  204. {
  205. screen.ScreenManager = this;
  206. screen.IsExiting = false;
  207. // If we have a graphics device, tell the screen to load content.
  208. if (_isInitialized)
  209. {
  210. screen.LoadContent();
  211. }
  212. _screens.Add(screen);
  213. // update the TouchPanel to respond to gestures this screen is interested in
  214. TouchPanel.EnabledGestures = screen.EnabledGestures;
  215. }
  216. /// <summary>
  217. /// Removes a screen from the screen manager. You should normally
  218. /// use GameScreen.ExitScreen instead of calling this directly, so
  219. /// the screen can gradually transition off rather than just being
  220. /// instantly removed.
  221. /// </summary>
  222. public void RemoveScreen(GameScreen screen)
  223. {
  224. // If we have a graphics device, tell the screen to unload content.
  225. if (_isInitialized)
  226. {
  227. screen.UnloadContent();
  228. }
  229. _screens.Remove(screen);
  230. _screensToUpdate.Remove(screen);
  231. // if there is a screen still in the manager, update TouchPanel
  232. // to respond to gestures that screen is interested in.
  233. if (_screens.Count > 0)
  234. {
  235. TouchPanel.EnabledGestures = _screens[_screens.Count - 1].EnabledGestures;
  236. }
  237. }
  238. /// <summary>
  239. /// Expose an array holding all the screens. We return a copy rather
  240. /// than the real master list, because screens should only ever be added
  241. /// or removed using the AddScreen and RemoveScreen methods.
  242. /// </summary>
  243. public GameScreen[] GetScreens()
  244. {
  245. return _screens.ToArray();
  246. }
  247. }
  248. }