GameScreen.cs 9.8 KB

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