GameScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. using Microsoft.Xna.Framework.Graphics;
  12. using CardsFramework;
  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. /// Load graphics content for the screen.
  184. /// </summary>
  185. public virtual void LoadContent()
  186. {
  187. ScreenManager.ScalePresentationArea();
  188. }
  189. /// <summary>
  190. /// Unload content for the screen.
  191. /// </summary>
  192. public virtual void UnloadContent() { }
  193. /// <summary>
  194. /// Allows the screen to run logic, such as updating the transition position.
  195. /// Unlike HandleInput, this method is called regardless of whether the screen
  196. /// is active, hidden, or in the middle of a transition.
  197. /// </summary>
  198. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  199. bool coveredByOtherScreen)
  200. {
  201. this.otherScreenHasFocus = otherScreenHasFocus;
  202. if (isExiting)
  203. {
  204. // If the screen is going away to die, it should transition off.
  205. screenState = ScreenState.TransitionOff;
  206. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  207. {
  208. // When the transition finishes, remove the screen.
  209. ScreenManager.RemoveScreen(this);
  210. }
  211. }
  212. else if (coveredByOtherScreen)
  213. {
  214. // If the screen is covered by another, it should transition off.
  215. if (UpdateTransition(gameTime, transitionOffTime, 1))
  216. {
  217. // Still busy transitioning.
  218. screenState = ScreenState.TransitionOff;
  219. }
  220. else
  221. {
  222. // Transition finished!
  223. screenState = ScreenState.Hidden;
  224. }
  225. }
  226. else
  227. {
  228. // Otherwise the screen should transition on and become active.
  229. if (UpdateTransition(gameTime, transitionOnTime, -1))
  230. {
  231. // Still busy transitioning.
  232. screenState = ScreenState.TransitionOn;
  233. }
  234. else
  235. {
  236. // Transition finished!
  237. screenState = ScreenState.Active;
  238. }
  239. }
  240. // Check if the back buffer size has changed (e.g., window resize).
  241. if (ScreenManager.BackbufferHeight != ScreenManager.GraphicsDevice.PresentationParameters.BackBufferHeight
  242. || ScreenManager.BackbufferWidth != ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth)
  243. {
  244. // Adjust the presentation area to match the new back buffer size.
  245. ScreenManager.ScalePresentationArea();
  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. /// <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. /// <summary>
  309. /// A helper method which loads assets using the screen manager's
  310. /// associated game content loader.
  311. /// </summary>
  312. /// <typeparam name="T">Type of asset.</typeparam>
  313. /// <param name="assetName">Asset name, relative to the loader root
  314. /// directory, and not including the .xnb extension.</param>
  315. /// <returns></returns>
  316. public T Load<T>(string assetName)
  317. {
  318. return ScreenManager.Game.Content.Load<T>(assetName);
  319. }
  320. }
  321. }