GameScreen.cs 13 KB

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