GameScreen.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 System.IO;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. namespace UserInterfaceSample
  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. #region Properties
  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 1 (fully active, no transition) to 0 (transitioned
  80. /// fully off to nothing).
  81. /// </summary>
  82. public float TransitionAlpha
  83. {
  84. get { return 1f - TransitionPosition; }
  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 internal 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. /// Gets the index of the player who is currently controlling this screen,
  133. /// or null if it is accepting input from any player. This is used to lock
  134. /// the game to a specific player profile. The main menu responds to input
  135. /// from any connected gamepad, but whichever player makes a selection from
  136. /// this menu is given control over all subsequent screens, so other gamepads
  137. /// are inactive until the controlling player returns to the main menu.
  138. /// </summary>
  139. public PlayerIndex? ControllingPlayer
  140. {
  141. get { return controllingPlayer; }
  142. internal set { controllingPlayer = value; }
  143. }
  144. PlayerIndex? controllingPlayer;
  145. /// <summary>
  146. /// Gets the gestures the screen is interested in. Screens should be as specific
  147. /// as possible with gestures to increase the accuracy of the gesture engine.
  148. /// For example, most menus only need Tap or perhaps Tap and VerticalDrag to operate.
  149. /// These gestures are handled by the ScreenManager when screens change and
  150. /// all gestures are placed in the InputState passed to the HandleInput method.
  151. /// </summary>
  152. public GestureType EnabledGestures
  153. {
  154. get { return enabledGestures; }
  155. protected set
  156. {
  157. enabledGestures = value;
  158. // the screen manager handles this during screen changes, but
  159. // if this screen is active and the gesture types are changing,
  160. // we have to update the TouchPanel ourself.
  161. if (ScreenState == ScreenState.Active)
  162. {
  163. TouchPanel.EnabledGestures = value;
  164. }
  165. }
  166. }
  167. GestureType enabledGestures = GestureType.None;
  168. /// <summary>
  169. /// Gets whether or not this screen is serializable. If this is true,
  170. /// the screen will be recorded into the screen manager's state and
  171. /// its Serialize and Deserialize methods will be called as appropriate.
  172. /// If this is false, the screen will be ignored during serialization.
  173. /// By default, all screens are assumed to be serializable.
  174. /// </summary>
  175. public bool IsSerializable
  176. {
  177. get { return isSerializable; }
  178. protected set { isSerializable = value; }
  179. }
  180. bool isSerializable = true;
  181. #endregion
  182. #region Initialization
  183. /// <summary>
  184. /// Load graphics content for the screen.
  185. /// </summary>
  186. public virtual void LoadContent() { }
  187. /// <summary>
  188. /// Unload content for the screen.
  189. /// </summary>
  190. public virtual void UnloadContent() { }
  191. #endregion
  192. #region Update and Draw
  193. /// <summary>
  194. /// Allows the screen to run logic, such as updating the transition position.
  195. /// Unlike HandleInput, this method is called regardless of whether the screen
  196. /// is active, hidden, or in the middle of a transition.
  197. /// </summary>
  198. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  199. bool coveredByOtherScreen)
  200. {
  201. this.otherScreenHasFocus = otherScreenHasFocus;
  202. if (isExiting)
  203. {
  204. // If the screen is going away to die, it should transition off.
  205. screenState = ScreenState.TransitionOff;
  206. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  207. {
  208. // When the transition finishes, remove the screen.
  209. ScreenManager.RemoveScreen(this);
  210. }
  211. }
  212. else if (coveredByOtherScreen)
  213. {
  214. // If the screen is covered by another, it should transition off.
  215. if (UpdateTransition(gameTime, transitionOffTime, 1))
  216. {
  217. // Still busy transitioning.
  218. screenState = ScreenState.TransitionOff;
  219. }
  220. else
  221. {
  222. // Transition finished!
  223. screenState = ScreenState.Hidden;
  224. }
  225. }
  226. else
  227. {
  228. // Otherwise the screen should transition on and become active.
  229. if (UpdateTransition(gameTime, transitionOnTime, -1))
  230. {
  231. // Still busy transitioning.
  232. screenState = ScreenState.TransitionOn;
  233. }
  234. else
  235. {
  236. // Transition finished!
  237. screenState = ScreenState.Active;
  238. }
  239. }
  240. }
  241. /// <summary>
  242. /// Helper for updating the screen transition position.
  243. /// </summary>
  244. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  245. {
  246. // How much should we move by?
  247. float transitionDelta;
  248. if (time == TimeSpan.Zero)
  249. transitionDelta = 1;
  250. else
  251. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  252. time.TotalMilliseconds);
  253. // Update the transition position.
  254. transitionPosition += transitionDelta * direction;
  255. // Did we reach the end of the transition?
  256. if (((direction < 0) && (transitionPosition <= 0)) ||
  257. ((direction > 0) && (transitionPosition >= 1)))
  258. {
  259. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  260. return false;
  261. }
  262. // Otherwise we are still busy transitioning.
  263. return true;
  264. }
  265. /// <summary>
  266. /// Allows the screen to handle user input. Unlike Update, this method
  267. /// is only called when the screen is active, and not when some other
  268. /// screen has taken the focus.
  269. /// </summary>
  270. public virtual void HandleInput(InputState input) { }
  271. /// <summary>
  272. /// This is called when the screen should draw itself.
  273. /// </summary>
  274. public virtual void Draw(GameTime gameTime) { }
  275. #endregion
  276. #region Public Methods
  277. /// <summary>
  278. /// Tells the screen to serialize its state into the given stream.
  279. /// </summary>
  280. public virtual void Serialize(Stream stream) { }
  281. /// <summary>
  282. /// Tells the screen to deserialize its state from the given stream.
  283. /// </summary>
  284. public virtual void Deserialize(Stream stream) { }
  285. /// <summary>
  286. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  287. /// instantly kills the screen, this method respects the transition timings
  288. /// and will give the screen a chance to gradually transition off.
  289. /// </summary>
  290. public void ExitScreen()
  291. {
  292. if (TransitionOffTime == TimeSpan.Zero)
  293. {
  294. // If the screen has a zero transition time, remove it immediately.
  295. ScreenManager.RemoveScreen(this);
  296. }
  297. else
  298. {
  299. // Otherwise flag that it should transition off and then exit.
  300. isExiting = true;
  301. }
  302. }
  303. #endregion
  304. }
  305. }