GameScreen.cs 9.3 KB

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