GameScreen.cs 9.3 KB

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