GameScreen.cs 9.8 KB

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