NetworkBusyScreen.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. {
  76. // If so, raise the OperationCompleted event.
  77. if (OperationCompleted != null)
  78. {
  79. OperationCompleted(this,
  80. new OperationCompletedEventArgs(asyncResult));
  81. }
  82. ExitScreen();
  83. asyncResult = null;
  84. }
  85. }
  86. /// <summary>
  87. /// Draws the NetworkBusyScreen.
  88. /// </summary>
  89. public override void Draw(GameTime gameTime)
  90. {
  91. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  92. SpriteFont font = ScreenManager.Font;
  93. string message = Resources.NetworkBusy;
  94. const int hPad = 32;
  95. const int vPad = 16;
  96. // Center the message text in the viewport.
  97. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  98. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  99. Vector2 textSize = font.MeasureString(message);
  100. // Add enough room to spin a cat.
  101. Vector2 catSize = new Vector2(catTexture.Width);
  102. textSize.X = Math.Max(textSize.X, catSize.X);
  103. textSize.Y += catSize.Y + vPad;
  104. Vector2 textPosition = (viewportSize - textSize) / 2;
  105. // The background includes a border somewhat larger than the text itself.
  106. Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
  107. (int)textPosition.Y - vPad,
  108. (int)textSize.X + hPad * 2,
  109. (int)textSize.Y + vPad * 2);
  110. // Fade the popup alpha during transitions.
  111. Color color = Color.White * TransitionAlpha;
  112. spriteBatch.Begin();
  113. // Draw the background rectangle.
  114. spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
  115. // Draw the message box text.
  116. spriteBatch.DrawString(font, message, textPosition, color);
  117. // Draw the spinning cat progress indicator.
  118. float catRotation = (float)gameTime.TotalGameTime.TotalSeconds * 3;
  119. Vector2 catPosition = new Vector2(textPosition.X + textSize.X / 2,
  120. textPosition.Y + textSize.Y -
  121. catSize.Y / 2);
  122. spriteBatch.Draw(catTexture, catPosition, null, color, catRotation,
  123. catSize / 2, 1, SpriteEffects.None, 0);
  124. spriteBatch.End();
  125. }
  126. #endregion
  127. }
  128. }