NetworkBusyScreen.cs 4.8 KB

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