GameScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. using Microsoft.Xna.Framework.Graphics;
  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. #if WINDOWS_PHONE || IOS || ANDROID
  151. /// <summary>
  152. /// Gets the gestures the screen is interested in. Screens should be as specific
  153. /// as possible with gestures to increase the accuracy of the gesture engine.
  154. /// For example, most menus only need Tap or perhaps Tap and VerticalDrag to operate.
  155. /// These gestures are handled by the ScreenManager when screens change and
  156. /// all gestures are placed in the InputState passed to the HandleInput method.
  157. /// </summary>
  158. public GestureType EnabledGestures
  159. {
  160. get { return enabledGestures; }
  161. protected set
  162. {
  163. enabledGestures = value;
  164. // the screen manager handles this during screen changes, but
  165. // if this screen is active and the gesture types are changing,
  166. // we have to update the TouchPanel ourself.
  167. if (ScreenState == ScreenState.Active)
  168. {
  169. TouchPanel.EnabledGestures = value;
  170. }
  171. }
  172. }
  173. GestureType enabledGestures = GestureType.None;
  174. #endif
  175. /// <summary>
  176. /// Gets whether or not this screen is serializable. If this is true,
  177. /// the screen will be recorded into the screen manager's state and
  178. /// its Serialize and Deserialize methods will be called as appropriate.
  179. /// If this is false, the screen will be ignored during serialization.
  180. /// By default, all screens are assumed to be serializable.
  181. /// </summary>
  182. public bool IsSerializable
  183. {
  184. get { return isSerializable; }
  185. protected set { isSerializable = value; }
  186. }
  187. bool isSerializable = true;
  188. #endregion
  189. #region Initialization
  190. /// <summary>
  191. /// Load graphics content for the screen.
  192. /// </summary>
  193. public virtual void LoadContent() { }
  194. /// <summary>
  195. /// Unload content for the screen.
  196. /// </summary>
  197. public virtual void UnloadContent() { }
  198. #endregion
  199. #region Update and Draw
  200. /// <summary>
  201. /// Allows the screen to run logic, such as updating the transition position.
  202. /// Unlike HandleInput, this method is called regardless of whether the screen
  203. /// is active, hidden, or in the middle of a transition.
  204. /// </summary>
  205. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  206. bool coveredByOtherScreen)
  207. {
  208. this.otherScreenHasFocus = otherScreenHasFocus;
  209. if (isExiting)
  210. {
  211. // If the screen is going away to die, it should transition off.
  212. screenState = ScreenState.TransitionOff;
  213. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  214. {
  215. // When the transition finishes, remove the screen.
  216. ScreenManager.RemoveScreen(this);
  217. }
  218. }
  219. else if (coveredByOtherScreen)
  220. {
  221. // If the screen is covered by another, it should transition off.
  222. if (UpdateTransition(gameTime, transitionOffTime, 1))
  223. {
  224. // Still busy transitioning.
  225. screenState = ScreenState.TransitionOff;
  226. }
  227. else
  228. {
  229. // Transition finished!
  230. screenState = ScreenState.Hidden;
  231. }
  232. }
  233. else
  234. {
  235. // Otherwise the screen should transition on and become active.
  236. if (UpdateTransition(gameTime, transitionOnTime, -1))
  237. {
  238. // Still busy transitioning.
  239. screenState = ScreenState.TransitionOn;
  240. }
  241. else
  242. {
  243. // Transition finished!
  244. screenState = ScreenState.Active;
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// Helper for updating the screen transition position.
  250. /// </summary>
  251. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  252. {
  253. // How much should we move by?
  254. float transitionDelta;
  255. if (time == TimeSpan.Zero)
  256. transitionDelta = 1;
  257. else
  258. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  259. time.TotalMilliseconds);
  260. // Update the transition position.
  261. transitionPosition += transitionDelta * direction;
  262. // Did we reach the end of the transition?
  263. if (((direction < 0) && (transitionPosition <= 0)) ||
  264. ((direction > 0) && (transitionPosition >= 1)))
  265. {
  266. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  267. return false;
  268. }
  269. // Otherwise we are still busy transitioning.
  270. return true;
  271. }
  272. /// <summary>
  273. /// Allows the screen to handle user input. Unlike Update, this method
  274. /// is only called when the screen is active, and not when some other
  275. /// screen has taken the focus.
  276. /// </summary>
  277. public virtual void HandleInput(InputState input) { }
  278. /// <summary>
  279. /// This is called when the screen should draw itself.
  280. /// </summary>
  281. public virtual void Draw(GameTime gameTime) { }
  282. #endregion
  283. #region Public Methods
  284. /// <summary>
  285. /// Tells the screen to serialize its state into the given stream.
  286. /// </summary>
  287. public virtual void Serialize(Stream stream) { }
  288. /// <summary>
  289. /// Tells the screen to deserialize its state from the given stream.
  290. /// </summary>
  291. public virtual void Deserialize(Stream stream) { }
  292. /// <summary>
  293. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  294. /// instantly kills the screen, this method respects the transition timings
  295. /// and will give the screen a chance to gradually transition off.
  296. /// </summary>
  297. public void ExitScreen()
  298. {
  299. if (TransitionOffTime == TimeSpan.Zero)
  300. {
  301. // If the screen has a zero transition time, remove it immediately.
  302. ScreenManager.RemoveScreen(this);
  303. }
  304. else
  305. {
  306. // Otherwise flag that it should transition off and then exit.
  307. isExiting = true;
  308. }
  309. }
  310. #endregion
  311. #region Helper Methods
  312. /// <summary>
  313. /// A helper method which loads assets using the screen manager's
  314. /// associated game content loader.
  315. /// </summary>
  316. /// <typeparam name="T">Type of asset.</typeparam>
  317. /// <param name="assetName">Asset name, relative to the loader root
  318. /// directory, and not including the .xnb extension.</param>
  319. /// <returns></returns>
  320. public T Load<T>(string assetName)
  321. {
  322. return ScreenManager.Game.Content.Load<T>(assetName);
  323. }
  324. #endregion
  325. }
  326. }