NetworkBusyScreen.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace NetRumble
  12. {
  13. /// <summary>
  14. /// When an asynchronous network operation (for instance searching for or joining a
  15. /// session) is in progress, we want to display some sort of busy indicator to let
  16. /// the user know the game hasn't just locked up. We also want to make sure they
  17. /// can't pick some other menu option before the current operation has finished.
  18. /// This screen takes care of both requirements in a single stroke. It monitors
  19. /// the IAsyncResult returned by an asynchronous network call, displaying a busy
  20. /// indicator for as long as the call is still in progress. When it notices the
  21. /// IAsyncResult has completed, it raises an event to let the game know it should
  22. /// proceed to the next step, after which the busy screen automatically goes away.
  23. /// Because this screen is on top of all others for as long as the asynchronous
  24. /// operation is in progress, it automatically takes over all user input,
  25. /// preventing any other menu entries being selected until the operation completes.
  26. /// </summary>
  27. /// <remarks>Based on a class in the Network Game State Management sample.</remarks>
  28. class NetworkBusyScreen : GameScreen
  29. {
  30. const float busyTextureScale = 0.8f;
  31. /// <summary>
  32. /// The message displayed in the screen.
  33. /// </summary>
  34. string message;
  35. /// <summary>
  36. /// The async result polled by the screen.
  37. /// </summary>
  38. IAsyncResult asyncResult;
  39. /// <summary>
  40. /// The rotating "activity" texture in the screen.
  41. /// </summary>
  42. Texture2D busyTexture;
  43. public event EventHandler<OperationCompletedEventArgs> OperationCompleted;
  44. /// <summary>
  45. /// Constructs a network busy screen for the specified asynchronous operation.
  46. /// </summary>
  47. public NetworkBusyScreen(string message, IAsyncResult asyncResult)
  48. {
  49. this.message = message;
  50. this.asyncResult = asyncResult;
  51. IsPopup = true;
  52. TransitionOnTime = TimeSpan.FromSeconds(0.1);
  53. TransitionOffTime = TimeSpan.FromSeconds(0.2);
  54. }
  55. /// <summary>
  56. /// Loads graphics content for this screen. This uses the shared ContentManager
  57. /// provided by the Game class, so the content will remain loaded forever.
  58. /// Whenever a subsequent NetworkBusyScreen tries to load this same content,
  59. /// it will just get back another reference to the already loaded data.
  60. /// </summary>
  61. public override void LoadContent()
  62. {
  63. ContentManager content = ScreenManager.Game.Content;
  64. busyTexture = content.Load<Texture2D>("Textures/chatTalking");
  65. }
  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. const int hPad = 32;
  94. const int vPad = 16;
  95. // Center the message text in the viewport.
  96. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  97. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  98. Vector2 textSize = font.MeasureString(message);
  99. // Add enough room to spin a texture.
  100. Vector2 busyTextureSize = new Vector2(busyTexture.Width * busyTextureScale);
  101. Vector2 busyTextureOrigin = new Vector2(busyTexture.Width / 2,
  102. busyTexture.Height / 2);
  103. textSize.X = Math.Max(textSize.X, busyTextureSize.X);
  104. textSize.Y += busyTextureSize.Y + vPad;
  105. Vector2 textPosition = (viewportSize - textSize) / 2;
  106. // The background includes a border somewhat larger than the text itself.
  107. Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
  108. (int)textPosition.Y - vPad,
  109. (int)textSize.X + hPad * 2,
  110. (int)textSize.Y + vPad * 2);
  111. // Fade the popup alpha during transitions.
  112. Color color = new Color((byte)255, (byte)255, (byte)255, (byte)TransitionAlpha);
  113. // Draw the background rectangle.
  114. Rectangle backgroundRectangle2 = new Rectangle(backgroundRectangle.X - 1,
  115. backgroundRectangle.Y - 1, backgroundRectangle.Width + 2,
  116. backgroundRectangle.Height + 2);
  117. ScreenManager.DrawRectangle(backgroundRectangle2, new Color((byte)128, (byte)128, (byte)128,
  118. (byte)(192.0f * (float)TransitionAlpha / 255.0f)));
  119. ScreenManager.DrawRectangle(backgroundRectangle, new Color((byte)0, (byte)0, (byte)0,
  120. (byte)(232.0f * (float)TransitionAlpha / 255.0f)));
  121. //spriteBatch.Begin(0,BlendState.NonPremultiplied, null, null, null);
  122. spriteBatch.Begin();
  123. // Draw the message box text.
  124. spriteBatch.DrawString(font, message, textPosition, color);
  125. // Draw the spinning cat progress indicator.
  126. float busyTextureRotation = (float)gameTime.TotalGameTime.TotalSeconds * 3;
  127. Vector2 busyTexturePosition = new Vector2(textPosition.X + textSize.X / 2,
  128. textPosition.Y + textSize.Y - busyTextureSize.Y / 2);
  129. spriteBatch.Draw(busyTexture, busyTexturePosition, null, color,
  130. busyTextureRotation, busyTextureOrigin, busyTextureScale,
  131. SpriteEffects.None, 0);
  132. spriteBatch.End();
  133. }
  134. }
  135. }