LoadingScreen.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LoadingScreen.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 NetRumble
  15. {
  16. /// <summary>
  17. /// The loading screen coordinates transitions between the menu system and the
  18. /// game itself. Normally one screen will transition off at the same time as
  19. /// the next screen is transitioning on, but for larger transitions that can
  20. /// take a longer time to load their data, we want the menu system to be entirely
  21. /// gone before we start loading the game. This is done as follows:
  22. ///
  23. /// - Tell all the existing screens to transition off.
  24. /// - Activate a loading screen, which will transition on at the same time.
  25. /// - The loading screen watches the state of the previous screens.
  26. /// - When it sees they have finished transitioning off, it activates the real
  27. /// next screen, which may take a long time to load its data. The loading
  28. /// screen will be the only thing displayed while this load is taking place.
  29. /// </summary>
  30. /// <remarks>
  31. /// This public class is similar to one in the GameStateManagement sample.
  32. /// </remarks>
  33. public class LoadingScreen : GameScreen
  34. {
  35. #region Fields
  36. bool loadingIsSlow;
  37. bool otherScreensAreGone;
  38. EventHandler<EventArgs> loadNextScreen;
  39. #endregion
  40. #region Initialization
  41. /// <summary>
  42. /// The constructor is private: loading screens should
  43. /// be activated via the static Load method instead.
  44. /// </summary>
  45. private LoadingScreen()
  46. {
  47. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  48. }
  49. /// <summary>
  50. /// Activates the loading screen.
  51. /// </summary>
  52. public static void Load(ScreenManager screenManager,
  53. EventHandler<EventArgs> loadNextScreen,
  54. bool loadingIsSlow)
  55. {
  56. // Tell all the current screens to transition off.
  57. foreach (GameScreen screen in screenManager.GetScreens())
  58. screen.ExitScreen();
  59. // Create and activate the loading screen.
  60. LoadingScreen loadingScreen = new LoadingScreen();
  61. loadingScreen.loadingIsSlow = loadingIsSlow;
  62. loadingScreen.loadNextScreen = loadNextScreen;
  63. screenManager.AddScreen(loadingScreen);
  64. }
  65. #endregion
  66. #region Update and Draw
  67. /// <summary>
  68. /// Updates the loading screen.
  69. /// </summary>
  70. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  71. bool coveredByOtherScreen)
  72. {
  73. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  74. // If all the previous screens have finished transitioning
  75. // off, it is time to actually perform the load.
  76. if (otherScreensAreGone)
  77. {
  78. ScreenManager.RemoveScreen(this);
  79. loadNextScreen(this, EventArgs.Empty);
  80. }
  81. }
  82. /// <summary>
  83. /// Draws the loading screen.
  84. /// </summary>
  85. public override void Draw(GameTime gameTime)
  86. {
  87. // If we are the only active screen, that means all the previous screens
  88. // must have finished transitioning off. We check for this in the Draw
  89. // method, rather than in Update, because it isn't enough just for the
  90. // screens to be gone: in order for the transition to look good we must
  91. // have actually drawn a frame without them before we perform the load.
  92. if ((ScreenState == ScreenState.Active) &&
  93. (ScreenManager.GetScreens().Length == 1))
  94. {
  95. otherScreensAreGone = true;
  96. }
  97. // The gameplay screen takes a while to load, so we display a loading
  98. // message while that is going on, but the menus load very quickly, and
  99. // it would look silly if we flashed this up for just a fraction of a
  100. // second while returning from the game to the menus. This parameter
  101. // tells us how long the loading is going to take, so we know whether
  102. // to bother drawing the message.
  103. if (loadingIsSlow)
  104. {
  105. const string message = "Loading...";
  106. // Center the text in the viewport.
  107. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  108. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  109. Vector2 textSize = ScreenManager.Font.MeasureString(message);
  110. Vector2 textPosition = (viewportSize - textSize) / 2;
  111. Color color = new Color(255, 255, 255, TransitionAlpha);
  112. // Draw the text.
  113. ScreenManager.SpriteBatch.Begin();
  114. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, message,
  115. textPosition, color);
  116. ScreenManager.SpriteBatch.End();
  117. }
  118. }
  119. #endregion
  120. }
  121. }