GameScreen.cs 13 KB

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