2
0

GameScreen.cs 10 KB

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