2
0

GameScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #endregion
  173. #region Initialization
  174. /// <summary>
  175. /// Load graphics content for the screen.
  176. /// </summary>
  177. public virtual void LoadContent() { }
  178. /// <summary>
  179. /// Unload content for the screen.
  180. /// </summary>
  181. public virtual void UnloadContent() { }
  182. #endregion
  183. #region Update and Draw
  184. /// <summary>
  185. /// Allows the screen to run logic, such as updating the transition position.
  186. /// Unlike HandleInput, this method is called regardless of whether the screen
  187. /// is active, hidden, or in the middle of a transition.
  188. /// </summary>
  189. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  190. bool coveredByOtherScreen)
  191. {
  192. this.otherScreenHasFocus = otherScreenHasFocus;
  193. if (isExiting)
  194. {
  195. // If the screen is going away to die, it should transition off.
  196. screenState = ScreenState.TransitionOff;
  197. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  198. {
  199. // When the transition finishes, remove the screen.
  200. ScreenManager.RemoveScreen(this);
  201. }
  202. }
  203. else if (coveredByOtherScreen)
  204. {
  205. // If the screen is covered by another, it should transition off.
  206. if (UpdateTransition(gameTime, transitionOffTime, 1))
  207. {
  208. // Still busy transitioning.
  209. screenState = ScreenState.TransitionOff;
  210. }
  211. else
  212. {
  213. // Transition finished!
  214. screenState = ScreenState.Hidden;
  215. }
  216. }
  217. else
  218. {
  219. // Otherwise the screen should transition on and become active.
  220. if (UpdateTransition(gameTime, transitionOnTime, -1))
  221. {
  222. // Still busy transitioning.
  223. screenState = ScreenState.TransitionOn;
  224. }
  225. else
  226. {
  227. // Transition finished!
  228. screenState = ScreenState.Active;
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// Helper for updating the screen transition position.
  234. /// </summary>
  235. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  236. {
  237. // How much should we move by?
  238. float transitionDelta;
  239. if (time == TimeSpan.Zero)
  240. transitionDelta = 1;
  241. else
  242. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  243. time.TotalMilliseconds);
  244. // Update the transition position.
  245. transitionPosition += transitionDelta * direction;
  246. // Did we reach the end of the transition?
  247. if (((direction < 0) && (transitionPosition <= 0)) ||
  248. ((direction > 0) && (transitionPosition >= 1)))
  249. {
  250. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  251. return false;
  252. }
  253. // Otherwise we are still busy transitioning.
  254. return true;
  255. }
  256. /// <summary>
  257. /// Allows the screen to handle user input. Unlike Update, this method
  258. /// is only called when the screen is active, and not when some other
  259. /// screen has taken the focus.
  260. /// </summary>
  261. public virtual void HandleInput(InputState input) { }
  262. /// <summary>
  263. /// This is called when the screen should draw itself.
  264. /// </summary>
  265. public virtual void Draw(GameTime gameTime) { }
  266. #endregion
  267. #region Public Methods
  268. /// <summary>
  269. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  270. /// instantly kills the screen, this method respects the transition timings
  271. /// and will give the screen a chance to gradually transition off.
  272. /// </summary>
  273. public void ExitScreen()
  274. {
  275. if (TransitionOffTime == TimeSpan.Zero)
  276. {
  277. // If the screen has a zero transition time, remove it immediately.
  278. ScreenManager.RemoveScreen(this);
  279. }
  280. else
  281. {
  282. // Otherwise flag that it should transition off and then exit.
  283. isExiting = true;
  284. }
  285. }
  286. #endregion
  287. }
  288. }