GameScreen.cs 9.2 KB

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