GameScreen.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /// <summary>
  136. /// Unload content for the screen.
  137. /// </summary>
  138. public virtual void UnloadContent() { }
  139. /// <summary>
  140. /// Allows the screen to run logic, such as updating the transition position.
  141. /// Unlike HandleInput, this method is called regardless of whether the screen
  142. /// is active, hidden, or in the middle of a transition.
  143. /// </summary>
  144. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  145. bool coveredByOtherScreen)
  146. {
  147. this.otherScreenHasFocus = otherScreenHasFocus;
  148. if (isExiting)
  149. {
  150. // If the screen is going away to die, it should transition off.
  151. screenState = ScreenState.TransitionOff;
  152. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  153. {
  154. // When the transition finishes, remove the screen.
  155. ScreenManager.RemoveScreen(this);
  156. isExiting = false;
  157. }
  158. }
  159. else if (coveredByOtherScreen)
  160. {
  161. // If the screen is covered by another, it should transition off.
  162. if (UpdateTransition(gameTime, transitionOffTime, 1))
  163. {
  164. // Still busy transitioning.
  165. screenState = ScreenState.TransitionOff;
  166. }
  167. else
  168. {
  169. // Transition finished!
  170. screenState = ScreenState.Hidden;
  171. }
  172. }
  173. else
  174. {
  175. // Otherwise the screen should transition on and become active.
  176. if (UpdateTransition(gameTime, transitionOnTime, -1))
  177. {
  178. // Still busy transitioning.
  179. screenState = ScreenState.TransitionOn;
  180. }
  181. else
  182. {
  183. // Transition finished!
  184. screenState = ScreenState.Active;
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// Helper for updating the screen transition position.
  190. /// </summary>
  191. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  192. {
  193. // How much should we move by?
  194. float transitionDelta;
  195. if (time == TimeSpan.Zero)
  196. transitionDelta = 1;
  197. else
  198. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  199. time.TotalMilliseconds);
  200. // Update the transition position.
  201. transitionPosition += transitionDelta * direction;
  202. // Did we reach the end of the transition?
  203. if ((transitionPosition <= 0) || (transitionPosition >= 1))
  204. {
  205. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  206. return false;
  207. }
  208. // Otherwise we are still busy transitioning.
  209. return true;
  210. }
  211. /// <summary>
  212. /// Allows the screen to handle user input. Unlike Update, this method
  213. /// is only called when the screen is active, and not when some other
  214. /// screen has taken the focus.
  215. /// </summary>
  216. public virtual void HandleInput(InputState input) { }
  217. /// <summary>
  218. /// Screen-specific update to gamer rich presence.
  219. /// </summary>
  220. public virtual void UpdatePresence() { }
  221. /// <summary>
  222. /// This is called when the screen should draw itself.
  223. /// </summary>
  224. public abstract void Draw(GameTime gameTime);
  225. /// <summary>
  226. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  227. /// instantly kills the screen, this method respects the transition timings
  228. /// and will give the screen a chance to gradually transition off.
  229. /// </summary>
  230. public virtual void ExitScreen()
  231. {
  232. if (TransitionOffTime == TimeSpan.Zero)
  233. {
  234. // If the screen has a zero transition time, remove it immediately.
  235. ScreenManager.RemoveScreen(this);
  236. }
  237. else
  238. {
  239. // Otherwise flag that it should transition off and then exit.
  240. isExiting = true;
  241. }
  242. }
  243. }
  244. }