GameScreen.cs 12 KB

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