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