GameScreen.cs 12 KB

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