GameScreen.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.Graphics;
  13. #endregion
  14. namespace RobotGameData.Screen
  15. {
  16. /// <summary>
  17. /// Enum describes the screen transition state.
  18. /// </summary>
  19. public enum ScreenState
  20. {
  21. TransitionOn,
  22. Active,
  23. TransitionOff,
  24. Hidden,
  25. Finished,
  26. }
  27. /// <summary>
  28. /// A screen is a single layer that has update and draw logic, and which
  29. /// can be combined with other layers to build up a complex menu system.
  30. /// For instance the main menu, the options menu, the "are you sure you
  31. /// want to quit" message box, and the main game itself are all implemented
  32. /// as screens.
  33. /// </summary>
  34. public abstract class GameScreen
  35. {
  36. #region Fields
  37. GameScreen nextScreen = null;
  38. #endregion
  39. #region Properties
  40. public GameScreen NextScreen
  41. {
  42. get { return nextScreen; }
  43. set { nextScreen = value; }
  44. }
  45. /// <summary>
  46. /// Normally when one screen is brought up over the top of another,
  47. /// the first screen will transition off to make room for the new
  48. /// one. This property indicates whether the screen is only a small
  49. /// popup, in which case screens underneath it do not need to bother
  50. /// transitioning off.
  51. /// </summary>
  52. public bool IsPopup
  53. {
  54. get { return isPopup; }
  55. protected set { isPopup = value; }
  56. }
  57. bool isPopup = false;
  58. /// <summary>
  59. /// Indicates how long the screen takes to
  60. /// transition on when it is activated.
  61. /// </summary>
  62. public TimeSpan TransitionOnTime
  63. {
  64. get { return transitionOnTime; }
  65. protected set { transitionOnTime = value; }
  66. }
  67. TimeSpan transitionOnTime = TimeSpan.Zero;
  68. /// <summary>
  69. /// Indicates how long the screen takes to
  70. /// transition off when it is deactivated.
  71. /// </summary>
  72. public TimeSpan TransitionOffTime
  73. {
  74. get { return transitionOffTime; }
  75. protected set { transitionOffTime = value; }
  76. }
  77. TimeSpan transitionOffTime = TimeSpan.Zero;
  78. /// <summary>
  79. /// Gets the current position of the screen transition, ranging
  80. /// from zero (fully active, no transition) to one (transitioned
  81. /// fully off to nothing).
  82. /// </summary>
  83. public float TransitionPosition
  84. {
  85. get { return transitionPosition; }
  86. protected set { transitionPosition = value; }
  87. }
  88. float transitionPosition = 1;
  89. /// <summary>
  90. /// Gets the current alpha of the screen transition, ranging
  91. /// from 255 (fully active, no transition) to 0 (transitioned
  92. /// fully off to nothing).
  93. /// </summary>
  94. public byte TransitionAlpha
  95. {
  96. get { return (byte)(255 - TransitionPosition * 255); }
  97. }
  98. /// <summary>
  99. /// Gets the current screen transition state.
  100. /// </summary>
  101. public ScreenState ScreenState
  102. {
  103. get { return screenState; }
  104. protected set { screenState = value; }
  105. }
  106. ScreenState screenState = ScreenState.TransitionOn;
  107. /// <summary>
  108. /// There are two possible reasons why a screen might be transitioning
  109. /// off. It could be temporarily going away to make room for another
  110. /// screen that is on top of it, or it could be going away for good.
  111. /// This property indicates whether the screen is exiting for real:
  112. /// if set, the screen will automatically remove itself as soon as the
  113. /// transition finishes.
  114. /// </summary>
  115. public bool IsExiting
  116. {
  117. get { return isExiting; }
  118. protected set { isExiting = value; }
  119. }
  120. bool isExiting = false;
  121. /// <summary>
  122. /// Checks whether this screen is active and can respond to user input.
  123. /// </summary>
  124. public bool IsActive
  125. {
  126. get
  127. {
  128. return !otherScreenHasFocus &&
  129. (screenState == ScreenState.TransitionOn ||
  130. screenState == ScreenState.Active);
  131. }
  132. }
  133. bool otherScreenHasFocus;
  134. /// <summary>
  135. /// Gets the manager that this screen belongs to.
  136. /// </summary>
  137. public GameScreenManager GameScreenManager
  138. {
  139. get { return gameScreenManager; }
  140. internal set { gameScreenManager = value; }
  141. }
  142. GameScreenManager gameScreenManager;
  143. #endregion
  144. #region Initialization
  145. /// <summary>
  146. /// Initialize for the screen.
  147. /// called after finished LoadGraphicsContent function of GameScreen.
  148. /// </summary>
  149. public virtual void InitializeScreen() { }
  150. /// <summary>
  151. /// Finalize for the screen.
  152. /// called before jump to next GameScreen.
  153. /// </summary>
  154. public virtual void FinalizeScreen() { }
  155. /// <summary>
  156. /// Load graphics content for the screen.
  157. /// </summary>
  158. public virtual void LoadContent() { }
  159. /// <summary>
  160. /// Unload content for the screen.
  161. /// </summary>
  162. public virtual void UnloadContent() { }
  163. /// <summary>
  164. /// calling when screen size has changed
  165. /// </summary>
  166. /// <param name="viewRect">new view area</param>
  167. public virtual void OnSize(Rectangle newRect) { }
  168. #endregion
  169. #region Update and Draw
  170. /// <summary>
  171. /// Allows the screen to run logic, such as updating the transition position.
  172. /// Unlike HandleInput, this method is called regardless of whether the screen
  173. /// is active, hidden, or in the middle of a transition.
  174. /// </summary>
  175. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  176. bool coveredByOtherScreen)
  177. {
  178. this.otherScreenHasFocus = otherScreenHasFocus;
  179. if (isExiting)
  180. {
  181. // If the screen is going away to die, it should transition off.
  182. screenState = ScreenState.TransitionOff;
  183. if (!UpdateTransition(gameTime, transitionOffTime, 1))
  184. {
  185. // When the transition finishes, remove the screen.
  186. gameScreenManager.RemoveScreen(this);
  187. isExiting = false;
  188. screenState = ScreenState.Finished;
  189. }
  190. }
  191. else if (coveredByOtherScreen)
  192. {
  193. // If the screen is covered by another, it should transition off.
  194. if (UpdateTransition(gameTime, transitionOffTime, 1))
  195. {
  196. // Still busy transitioning.
  197. screenState = ScreenState.TransitionOff;
  198. }
  199. else
  200. {
  201. // Transition finished!
  202. screenState = ScreenState.Hidden;
  203. }
  204. }
  205. else
  206. {
  207. // Otherwise the screen should transition on and become active.
  208. if (UpdateTransition(gameTime, transitionOnTime, -1))
  209. {
  210. // Still busy transitioning.
  211. screenState = ScreenState.TransitionOn;
  212. }
  213. else
  214. {
  215. // Transition finished!
  216. screenState = ScreenState.Active;
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// Helper for updating the screen transition position.
  222. /// </summary>
  223. bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  224. {
  225. // How much should we move by?
  226. float transitionDelta;
  227. if (time == TimeSpan.Zero)
  228. transitionDelta = 1;
  229. else
  230. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  231. time.TotalMilliseconds);
  232. // Update the transition position.
  233. transitionPosition += transitionDelta * direction;
  234. // Did we reach the end of the transition?
  235. if ((transitionPosition <= 0) || (transitionPosition >= 1))
  236. {
  237. transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
  238. return false;
  239. }
  240. // Otherwise we are still busy transitioning.
  241. return true;
  242. }
  243. /// <summary>
  244. /// Allows the screen to handle user input. Unlike Update, this method
  245. /// is only called when the screen is active, and not when some other
  246. /// screen has taken the focus.
  247. /// </summary>
  248. public virtual void HandleInput(GameTime gameTime) { }
  249. /// <summary>
  250. /// This is called when the screen should draw itself.
  251. /// </summary>
  252. public abstract void Draw(GameTime gameTime);
  253. #endregion
  254. /// <summary>
  255. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  256. /// instantly kills the screen, this method respects the transition timings
  257. /// and will give the screen a chance to gradually transition off.
  258. /// </summary>
  259. public void ExitScreen()
  260. {
  261. if (TransitionOffTime == TimeSpan.Zero)
  262. {
  263. // If the screen has a zero transition time, remove it immediately.
  264. GameScreenManager.RemoveScreen(this);
  265. }
  266. else
  267. {
  268. // Otherwise flag that it should transition off and then exit.
  269. isExiting = true;
  270. }
  271. }
  272. public void OnSize()
  273. {
  274. Viewport viewport = RobotGameData.Render.Viewer.CurrentViewport;
  275. OnSize(new Rectangle(viewport.X, viewport.Y, viewport.Width,
  276. viewport.Height));
  277. }
  278. }
  279. }