NetworkBusyScreen.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // NetworkBusyScreen.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.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace NetworkStateManagement
  16. {
  17. /// <summary>
  18. /// When an asynchronous network operation (for instance searching for or joining a
  19. /// session) is in progress, we want to display some sort of busy indicator to let
  20. /// the user know the game hasn't just locked up. We also want to make sure they
  21. /// can't pick some other menu option before the current operation has finished.
  22. /// This screen takes care of both requirements in a single stroke. It monitors
  23. /// the IAsyncResult returned by an asynchronous network call, displaying a busy
  24. /// indicator for as long as the call is still in progress. When it notices the
  25. /// IAsyncResult has completed, it raises an event to let the game know it should
  26. /// proceed to the next step, after which the busy screen automatically goes away.
  27. /// Because this screen is on top of all others for as long as the asynchronous
  28. /// operation is in progress, it automatically takes over all user input,
  29. /// preventing any other menu entries being selected until the operation completes.
  30. /// </summary>
  31. class NetworkBusyScreen : GameScreen
  32. {
  33. #region Fields
  34. IAsyncResult asyncResult;
  35. Texture2D gradientTexture;
  36. Texture2D catTexture;
  37. #endregion
  38. #region Events
  39. public event EventHandler<OperationCompletedEventArgs> OperationCompleted;
  40. #endregion
  41. #region Initialization
  42. /// <summary>
  43. /// Constructs a network busy screen for the specified asynchronous operation.
  44. /// </summary>
  45. public NetworkBusyScreen (IAsyncResult asyncResult)
  46. {
  47. this.asyncResult = asyncResult;
  48. IsPopup = true;
  49. TransitionOnTime = TimeSpan.FromSeconds (0.1);
  50. TransitionOffTime = TimeSpan.FromSeconds (0.2);
  51. }
  52. /// <summary>
  53. /// Loads graphics content for this screen. This uses the shared ContentManager
  54. /// provided by the Game class, so the content will remain loaded forever.
  55. /// Whenever a subsequent NetworkBusyScreen tries to load this same content,
  56. /// it will just get back another reference to the already loaded data.
  57. /// </summary>
  58. public override void LoadContent ()
  59. {
  60. ContentManager content = ScreenManager.Game.Content;
  61. gradientTexture = content.Load<Texture2D> ("gradient");
  62. catTexture = content.Load<Texture2D> ("cat");
  63. }
  64. #endregion
  65. #region Update and Draw
  66. /// <summary>
  67. /// Updates the NetworkBusyScreen.
  68. /// </summary>
  69. public override void Update (GameTime gameTime, bool otherScreenHasFocus,
  70. bool coveredByOtherScreen)
  71. {
  72. base.Update (gameTime, otherScreenHasFocus, coveredByOtherScreen);
  73. // Has our asynchronous operation completed?
  74. if ((asyncResult != null) && asyncResult.IsCompleted) {
  75. // If so, raise the OperationCompleted event.
  76. if (OperationCompleted != null) {
  77. OperationCompleted (this,
  78. new OperationCompletedEventArgs (asyncResult));
  79. }
  80. ExitScreen ();
  81. asyncResult = null;
  82. }
  83. }
  84. /// <summary>
  85. /// Draws the NetworkBusyScreen.
  86. /// </summary>
  87. public override void Draw (GameTime gameTime)
  88. {
  89. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  90. SpriteFont font = ScreenManager.Font;
  91. string message = Resources.NetworkBusy;
  92. const int hPad = 32 ;
  93. const int vPad = 16 ;
  94. // Center the message text in the viewport.
  95. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  96. Vector2 viewportSize = new Vector2 (viewport.Width, viewport.Height);
  97. Vector2 textSize = font.MeasureString (message);
  98. // Add enough room to spin a cat.
  99. Vector2 catSize = new Vector2 (catTexture.Width);
  100. textSize.X = Math.Max (textSize.X, catSize.X);
  101. textSize.Y += catSize.Y + vPad;
  102. Vector2 textPosition = (viewportSize - textSize) / 2;
  103. // The background includes a border somewhat larger than the text itself.
  104. Rectangle backgroundRectangle = new Rectangle ((int)textPosition.X - hPad,
  105. (int)textPosition.Y - vPad,
  106. (int)textSize.X + hPad * 2,
  107. (int)textSize.Y + vPad * 2);
  108. // Fade the popup alpha during transitions.
  109. Color color = Color.White * TransitionAlpha;
  110. spriteBatch.Begin ();
  111. // Draw the background rectangle.
  112. spriteBatch.Draw (gradientTexture, backgroundRectangle, color);
  113. // Draw the message box text.
  114. spriteBatch.DrawString (font, message, textPosition, color);
  115. // Draw the spinning cat progress indicator.
  116. float catRotation = (float)gameTime.TotalGameTime.TotalSeconds * 3;
  117. Vector2 catPosition = new Vector2 (textPosition.X + textSize.X / 2,
  118. textPosition.Y + textSize.Y -
  119. catSize.Y / 2);
  120. spriteBatch.Draw (catTexture, catPosition, null, color, catRotation,
  121. catSize / 2, 1, SpriteEffects.None, 0);
  122. spriteBatch.End ();
  123. }
  124. #endregion
  125. }
  126. }