GameScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //-----------------------------------------------------------------------------
  2. // GameScreen.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Input.Touch;
  10. using System.IO;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace GameStateManagement
  13. {
  14. /// <summary>
  15. /// Enum describes the screen transition state.
  16. /// </summary>
  17. public enum ScreenState
  18. {
  19. TransitionOn,
  20. Active,
  21. TransitionOff,
  22. Hidden,
  23. }
  24. /// <summary>
  25. /// A screen is a single layer that has update and draw logic, and which
  26. /// can be combined with other layers to build up a complex menu system.
  27. /// For instance the main menu, the options menu, the "are you sure you
  28. /// want to quit" message box, and the main game itself are all implemented
  29. /// as screens.
  30. /// </summary>
  31. public abstract class GameScreen
  32. {
  33. /// <summary>
  34. /// Normally when one screen is brought up over the top of another,
  35. /// the first screen will transition off to make room for the new
  36. /// one. This property indicates whether the screen is only a small
  37. /// popup, in which case screens underneath it do not need to bother
  38. /// transitioning off.
  39. /// </summary>
  40. public bool IsPopup
  41. {
  42. get { return isPopup; }
  43. protected set { isPopup = value; }
  44. }
  45. bool isPopup = false;
  46. /// <summary>
  47. /// Indicates how long the screen takes to
  48. /// transition on when it is activated.
  49. /// </summary>
  50. public TimeSpan TransitionOnTime
  51. {
  52. get { return transitionOnTime; }
  53. protected set { transitionOnTime = value; }
  54. }
  55. TimeSpan transitionOnTime = TimeSpan.Zero;
  56. /// <summary>
  57. /// Indicates how long the screen takes to
  58. /// transition off when it is deactivated.
  59. /// </summary>
  60. public TimeSpan TransitionOffTime
  61. {
  62. get { return transitionOffTime; }
  63. protected set { transitionOffTime = value; }
  64. }
  65. TimeSpan transitionOffTime = TimeSpan.Zero;
  66. /// <summary>
  67. /// Gets the current position of the screen transition, ranging
  68. /// from zero (fully active, no transition) to one (transitioned
  69. /// fully off to nothing).
  70. /// </summary>
  71. public float TransitionPosition
  72. {
  73. get { return transitionPosition; }
  74. protected set { transitionPosition = value; }
  75. }
  76. float transitionPosition = 1;
  77. /// <summary>
  78. /// Gets the current alpha of the screen transition, ranging
  79. /// from 1 (fully active, no transition) to 0 (transitioned
  80. /// fully off to nothing).
  81. /// </summary>
  82. public float TransitionAlpha
  83. {
  84. get { return 1f - TransitionPosition; }
  85. }
  86. /// <summary>
  87. /// Gets the current screen transition state.
  88. /// </summary>
  89. public ScreenState ScreenState
  90. {
  91. get { return screenState; }
  92. protected set { screenState = value; }
  93. }
  94. ScreenState screenState = ScreenState.TransitionOn;
  95. /// <summary>
  96. /// There are two possible reasons why a screen might be transitioning
  97. /// off. It could be temporarily going away to make room for another
  98. /// screen that is on top of it, or it could be going away for good.
  99. /// This property indicates whether the screen is exiting for real:
  100. /// if set, the screen will automatically remove itself as soon as the
  101. /// transition finishes.
  102. /// </summary>
  103. public bool IsExiting
  104. {
  105. get { return isExiting; }
  106. protected internal set { isExiting = value; }
  107. }
  108. bool isExiting = false;
  109. /// <summary>
  110. /// Checks whether this screen is active and can respond to user input.
  111. /// </summary>
  112. public bool IsActive
  113. {
  114. get
  115. {
  116. return !otherScreenHasFocus &&
  117. (screenState == ScreenState.TransitionOn ||
  118. screenState == ScreenState.Active);
  119. }
  120. }
  121. bool otherScreenHasFocus;
  122. /// <summary>
  123. /// Gets the manager that this screen belongs to.
  124. /// </summary>
  125. public ScreenManager ScreenManager
  126. {
  127. get { return screenManager; }
  128. internal set { screenManager = value; }
  129. }
  130. ScreenManager screenManager;
  131. /// <summary>
  132. /// Gets the index of the player who is currently controlling this screen,
  133. /// or null if it is accepting input from any player. This is used to lock
  134. /// the game to a specific player profile. The main menu responds to input
  135. /// from any connected gamepad, but whichever player makes a selection from
  136. /// this menu is given control over all subsequent screens, so other gamepads
  137. /// are inactive until the controlling player returns to the main menu.
  138. /// </summary>
  139. public PlayerIndex? ControllingPlayer
  140. {
  141. get { return controllingPlayer; }
  142. internal set { controllingPlayer = value; }
  143. }
  144. PlayerIndex? controllingPlayer;
  145. /// <summary>
  146. /// Gets the gestures the screen is interested in. Screens should be as specific
  147. /// as possible with gestures to increase the accuracy of the gesture engine.
  148. /// For example, most menus only need Tap or perhaps Tap and VerticalDrag to operate.
  149. /// These gestures are handled by the ScreenManager when screens change and
  150. /// all gestures are placed in the InputState passed to the HandleInput method.
  151. /// </summary>
  152. public GestureType EnabledGestures
  153. {
  154. get { return enabledGestures; }
  155. protected set
  156. {
  157. enabledGestures = value;
  158. // the screen manager handles this during screen changes, but
  159. // if this screen is active and the gesture types are changing,
  160. // we have to update the TouchPanel ourself.
  161. if (ScreenState == ScreenState.Active)
  162. {
  163. TouchPanel.EnabledGestures = value;
  164. }
  165. }
  166. }
  167. GestureType enabledGestures = GestureType.None;
  168. /// <summary>
  169. /// Gets whether or not this screen is serializable. If this is true,
  170. /// the screen will be recorded into the screen manager's state and
  171. /// its Serialize and Deserialize methods will be called as appropriate.
  172. /// If this is false, the screen will be ignored during serialization.
  173. /// By default, all screens are assumed to be serializable.
  174. /// </summary>
  175. public bool IsSerializable
  176. {
  177. get { return isSerializable; }
  178. protected set { isSerializable = value; }
  179. }
  180. bool isSerializable = true;
  181. /// <summary>
  182. /// Load graphics content for the screen.
  183. /// </summary>
  184. public virtual void LoadContent()
  185. {
  186. ScreenManager.ScalePresentationArea();
  187. }
  188. /// <summary>
  189. /// Unload content for the screen.
  190. /// </summary>
  191. public virtual void UnloadContent() { }
  192. /// <summary>
  193. /// Allows the screen to run logic, such as updating the transition position.
  194. /// Unlike HandleInput, this method is called regardless of whether the screen
  195. /// is active, hidden, or in the middle of a transition.
  196. /// </summary>
  197. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  198. bool coveredByOtherScreen)
  199. {
  200. this.otherScreenHasFocus = otherScreenHasFocus;
  201. if (isExiting)
  202. {
  203. // If the screen is going away to die, it should transition off.
  204. screenState = ScreenState.TransitionOff;
  205. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  206. {
  207. // When the transition finishes, remove the screen.
  208. ScreenManager.RemoveScreen(this);
  209. }
  210. }
  211. else if (coveredByOtherScreen)
  212. {
  213. // If the screen is covered by another, it should transition off.
  214. if (UpdateTransition(gameTime, transitionOffTime, 1))
  215. {
  216. // Still busy transitioning.
  217. screenState = ScreenState.TransitionOff;
  218. }
  219. else
  220. {
  221. // Transition finished!
  222. screenState = ScreenState.Hidden;
  223. }
  224. }
  225. else
  226. {
  227. // Otherwise the screen should transition on and become active.
  228. if (UpdateTransition(gameTime, transitionOnTime, -1))
  229. {
  230. // Still busy transitioning.
  231. screenState = ScreenState.TransitionOn;
  232. }
  233. else
  234. {
  235. // Transition finished!
  236. screenState = ScreenState.Active;
  237. }
  238. }
  239. // Check if the back buffer size has changed (e.g., window resize).
  240. if (ScreenManager.BackbufferHeight != ScreenManager.GraphicsDevice.PresentationParameters.BackBufferHeight
  241. || ScreenManager.BackbufferWidth != ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth)
  242. {
  243. // Adjust the presentation area to match the new back buffer size.
  244. ScreenManager.ScalePresentationArea();
  245. }
  246. }
  247. /// <summary>
  248. /// Helper for updating the screen transition position.
  249. /// </summary>
  250. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  251. {
  252. // How much should we move by?
  253. float transitionDelta;
  254. if (time == TimeSpan.Zero)
  255. transitionDelta = 1;
  256. else
  257. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  258. time.TotalMilliseconds);
  259. // Update the transition position.
  260. transitionPosition += transitionDelta * direction;
  261. // Did we reach the end of the transition?
  262. if (((direction < 0) && (transitionPosition <= 0)) ||
  263. ((direction > 0) && (transitionPosition >= 1)))
  264. {
  265. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  266. return false;
  267. }
  268. // Otherwise we are still busy transitioning.
  269. return true;
  270. }
  271. /// <summary>
  272. /// Allows the screen to handle user input. Unlike Update, this method
  273. /// is only called when the screen is active, and not when some other
  274. /// screen has taken the focus.
  275. /// </summary>
  276. public virtual void HandleInput(InputState input) { }
  277. /// <summary>
  278. /// This is called when the screen should draw itself.
  279. /// </summary>
  280. public virtual void Draw(GameTime gameTime) { }
  281. /// <summary>
  282. /// Tells the screen to serialize its state into the given stream.
  283. /// </summary>
  284. public virtual void Serialize(Stream stream) { }
  285. /// <summary>
  286. /// Tells the screen to deserialize its state from the given stream.
  287. /// </summary>
  288. public virtual void Deserialize(Stream stream) { }
  289. /// <summary>
  290. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  291. /// instantly kills the screen, this method respects the transition timings
  292. /// and will give the screen a chance to gradually transition off.
  293. /// </summary>
  294. public void ExitScreen()
  295. {
  296. if (TransitionOffTime == TimeSpan.Zero)
  297. {
  298. // If the screen has a zero transition time, remove it immediately.
  299. ScreenManager.RemoveScreen(this);
  300. }
  301. else
  302. {
  303. // Otherwise flag that it should transition off and then exit.
  304. isExiting = true;
  305. }
  306. }
  307. /// <summary>
  308. /// A helper method which loads assets using the screen manager's
  309. /// associated game content loader.
  310. /// </summary>
  311. /// <typeparam name="T">Type of asset.</typeparam>
  312. /// <param name="assetName">Asset name, relative to the loader root
  313. /// directory, and not including the .xnb extension.</param>
  314. /// <returns></returns>
  315. public T Load<T>(string assetName)
  316. {
  317. return ScreenManager.Game.Content.Load<T>(assetName);
  318. }
  319. }
  320. }