GameScreen.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. }
  173. /// <summary>
  174. /// Unload content for the screen.
  175. /// </summary>
  176. public virtual void UnloadContent()
  177. {
  178. }
  179. /// <summary>
  180. /// Allows the screen to run logic, such as updating the transition position.
  181. /// Unlike HandleInput, this method is called regardless of whether the screen
  182. /// is active, hidden, or in the middle of a transition.
  183. /// </summary>
  184. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  185. bool coveredByOtherScreen)
  186. {
  187. this.otherScreenHasFocus = otherScreenHasFocus;
  188. if (isExiting)
  189. {
  190. // If the screen is going away to die, it should transition off.
  191. screenState = ScreenState.TransitionOff;
  192. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  193. {
  194. // When the transition finishes, remove the screen.
  195. ScreenManager.RemoveScreen(this);
  196. }
  197. }
  198. else if (coveredByOtherScreen)
  199. {
  200. // If the screen is covered by another, it should transition off.
  201. if (UpdateTransition(gameTime, transitionOffTime, 1))
  202. {
  203. // Still busy transitioning.
  204. screenState = ScreenState.TransitionOff;
  205. }
  206. else
  207. {
  208. // Transition finished!
  209. screenState = ScreenState.Hidden;
  210. }
  211. }
  212. else
  213. {
  214. // Otherwise the screen should transition on and become active.
  215. if (UpdateTransition(gameTime, transitionOnTime, -1))
  216. {
  217. // Still busy transitioning.
  218. screenState = ScreenState.TransitionOn;
  219. }
  220. else
  221. {
  222. // Transition finished!
  223. screenState = ScreenState.Active;
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Helper for updating the screen transition position.
  229. /// </summary>
  230. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  231. {
  232. // How much should we move by?
  233. float transitionDelta;
  234. if (time == TimeSpan.Zero)
  235. transitionDelta = 1;
  236. else
  237. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  238. time.TotalMilliseconds);
  239. // Update the transition position.
  240. transitionPosition += transitionDelta * direction;
  241. // Did we reach the end of the transition?
  242. if (((direction < 0) && (transitionPosition <= 0)) ||
  243. ((direction > 0) && (transitionPosition >= 1)))
  244. {
  245. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  246. return false;
  247. }
  248. // Otherwise we are still busy transitioning.
  249. return true;
  250. }
  251. /// <summary>
  252. /// Allows the screen to handle user input. Unlike Update, this method
  253. /// is only called when the screen is active, and not when some other
  254. /// screen has taken the focus.
  255. /// </summary>
  256. public virtual void HandleInput(InputState input)
  257. {
  258. }
  259. /// <summary>
  260. /// This is called when the screen should draw itself.
  261. /// </summary>
  262. public virtual void Draw(GameTime gameTime)
  263. {
  264. }
  265. /// <summary>
  266. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  267. /// instantly kills the screen, this method respects the transition timings
  268. /// and will give the screen a chance to gradually transition off.
  269. /// </summary>
  270. public void ExitScreen()
  271. {
  272. if (TransitionOffTime == TimeSpan.Zero)
  273. {
  274. // If the screen has a zero transition time, remove it immediately.
  275. ScreenManager.RemoveScreen(this);
  276. }
  277. else
  278. {
  279. // Otherwise flag that it should transition off and then exit.
  280. isExiting = true;
  281. }
  282. }
  283. }
  284. }