GameScreen.cs 9.8 KB

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