GameScreen.cs 13 KB

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