GameScreen.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PlayerIndexEventArgs.cs
  4. //
  5. // XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Input.Touch;
  12. namespace FarseerPhysics.SamplesFramework
  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. private GestureType _enabledGestures = GestureType.None;
  34. private bool _otherScreenHasFocus;
  35. public GameScreen()
  36. {
  37. ScreenState = ScreenState.TransitionOn;
  38. TransitionPosition = 1;
  39. TransitionOffTime = TimeSpan.Zero;
  40. TransitionOnTime = TimeSpan.Zero;
  41. HasCursor = false;
  42. HasVirtualStick = false;
  43. }
  44. public bool HasCursor { get; set; }
  45. public bool HasVirtualStick { get; set; }
  46. /// <summary>
  47. /// Normally when one screen is brought up over the top of another,
  48. /// the first screen will transition off to make room for the new
  49. /// one. This property indicates whether the screen is only a small
  50. /// popup, in which case screens underneath it do not need to bother
  51. /// transitioning off.
  52. /// </summary>
  53. public bool IsPopup { get; protected set; }
  54. /// <summary>
  55. /// Indicates how long the screen takes to
  56. /// transition on when it is activated.
  57. /// </summary>
  58. public TimeSpan TransitionOnTime { get; protected set; }
  59. /// <summary>
  60. /// Indicates how long the screen takes to
  61. /// transition off when it is deactivated.
  62. /// </summary>
  63. public TimeSpan TransitionOffTime { get; protected set; }
  64. /// <summary>
  65. /// Gets the current position of the screen transition, ranging
  66. /// from zero (fully active, no transition) to one (transitioned
  67. /// fully off to nothing).
  68. /// </summary>
  69. public float TransitionPosition { get; protected set; }
  70. /// <summary>
  71. /// Gets the current alpha of the screen transition, ranging
  72. /// from 1 (fully active, no transition) to 0 (transitioned
  73. /// fully off to nothing).
  74. /// </summary>
  75. public float TransitionAlpha
  76. {
  77. get { return 1f - TransitionPosition; }
  78. }
  79. /// <summary>
  80. /// Gets the current screen transition state.
  81. /// </summary>
  82. public ScreenState ScreenState { get; protected set; }
  83. /// <summary>
  84. /// There are two possible reasons why a screen might be transitioning
  85. /// off. It could be temporarily going away to make room for another
  86. /// screen that is on top of it, or it could be going away for good.
  87. /// This property indicates whether the screen is exiting for real:
  88. /// if set, the screen will automatically remove itself as soon as the
  89. /// transition finishes.
  90. /// </summary>
  91. public bool IsExiting { get; protected internal set; }
  92. /// <summary>
  93. /// Checks whether this screen is active and can respond to user input.
  94. /// </summary>
  95. public bool IsActive
  96. {
  97. get
  98. {
  99. return !_otherScreenHasFocus &&
  100. (ScreenState == ScreenState.TransitionOn ||
  101. ScreenState == ScreenState.Active);
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the manager that this screen belongs to.
  106. /// </summary>
  107. public ScreenManager ScreenManager { get; internal set; }
  108. /// <summary>
  109. /// Gets the gestures the screen is interested in. Screens should be as specific
  110. /// as possible with gestures to increase the accuracy of the gesture engine.
  111. /// For example, most menus only need Tap or perhaps Tap and VerticalDrag to operate.
  112. /// These gestures are handled by the ScreenManager when screens change and
  113. /// all gestures are placed in the InputState passed to the HandleInput method.
  114. /// </summary>
  115. public GestureType EnabledGestures
  116. {
  117. get { return _enabledGestures; }
  118. protected set
  119. {
  120. _enabledGestures = value;
  121. // the screen manager handles this during screen changes, but
  122. // if this screen is active and the gesture types are changing,
  123. // we have to update the TouchPanel ourself.
  124. if (ScreenState == ScreenState.Active)
  125. {
  126. TouchPanel.EnabledGestures = value;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Load graphics content for the screen.
  132. /// </summary>
  133. public virtual void LoadContent()
  134. {
  135. }
  136. /// <summary>
  137. /// Unload content for the screen.
  138. /// </summary>
  139. public virtual void UnloadContent()
  140. {
  141. }
  142. /// <summary>
  143. /// Allows the screen to run logic, such as updating the transition position.
  144. /// Unlike HandleInput, this method is called regardless of whether the screen
  145. /// is active, hidden, or in the middle of a transition.
  146. /// </summary>
  147. public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
  148. bool coveredByOtherScreen)
  149. {
  150. _otherScreenHasFocus = otherScreenHasFocus;
  151. if (IsExiting)
  152. {
  153. // If the screen is going away to die, it should transition off.
  154. ScreenState = ScreenState.TransitionOff;
  155. if (!UpdateTransition(gameTime, TransitionOffTime, 1))
  156. {
  157. // When the transition finishes, remove the screen.
  158. ScreenManager.RemoveScreen(this);
  159. }
  160. }
  161. else if (coveredByOtherScreen)
  162. {
  163. // If the screen is covered by another, it should transition off.
  164. if (UpdateTransition(gameTime, TransitionOffTime, 1))
  165. {
  166. // Still busy transitioning.
  167. ScreenState = ScreenState.TransitionOff;
  168. }
  169. else
  170. {
  171. // Transition finished!
  172. ScreenState = ScreenState.Hidden;
  173. }
  174. }
  175. else
  176. {
  177. // Otherwise the screen should transition on and become active.
  178. if (UpdateTransition(gameTime, TransitionOnTime, -1))
  179. {
  180. // Still busy transitioning.
  181. ScreenState = ScreenState.TransitionOn;
  182. }
  183. else
  184. {
  185. // Transition finished!
  186. ScreenState = ScreenState.Active;
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Helper for updating the screen transition position.
  192. /// </summary>
  193. private bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
  194. {
  195. // How much should we move by?
  196. float transitionDelta;
  197. if (time == TimeSpan.Zero)
  198. {
  199. transitionDelta = 1f;
  200. }
  201. else
  202. {
  203. transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
  204. time.TotalMilliseconds);
  205. }
  206. // Update the transition position.
  207. TransitionPosition += transitionDelta * direction;
  208. // Did we reach the end of the transition?
  209. if (((direction < 0) && (TransitionPosition <= 0)) ||
  210. ((direction > 0) && (TransitionPosition >= 1)))
  211. {
  212. TransitionPosition = MathHelper.Clamp(TransitionPosition, 0, 1);
  213. return false;
  214. }
  215. // Otherwise we are still busy transitioning.
  216. return true;
  217. }
  218. /// <summary>
  219. /// Allows the screen to handle user input. Unlike Update, this method
  220. /// is only called when the screen is active, and not when some other
  221. /// screen has taken the focus.
  222. /// </summary>
  223. public virtual void HandleInput(InputHelper input, GameTime gameTime)
  224. {
  225. }
  226. /// <summary>
  227. /// This is called when the screen should draw itself.
  228. /// </summary>
  229. public virtual void Draw(GameTime gameTime)
  230. {
  231. }
  232. /// <summary>
  233. /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
  234. /// instantly kills the screen, this method respects the transition timings
  235. /// and will give the screen a chance to gradually transition off.
  236. /// </summary>
  237. public void ExitScreen()
  238. {
  239. if (TransitionOffTime == TimeSpan.Zero)
  240. {
  241. // If the screen has a zero transition time, remove it immediately.
  242. ScreenManager.RemoveScreen(this);
  243. }
  244. else
  245. {
  246. // Otherwise flag that it should transition off and then exit.
  247. IsExiting = true;
  248. }
  249. }
  250. }
  251. }