GameScreen.cs 9.3 KB

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