MessageBoxScreen.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-----------------------------------------------------------------------------
  2. // MessageBoxScreen.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.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. namespace NetRumble
  12. {
  13. /// <summary>
  14. /// A popup message box screen, used to display "are you sure?"
  15. /// confirmation messages.
  16. /// </summary>
  17. /// <remarks>
  18. /// This public class is somewhat similar to one of the same name in the
  19. /// GameStateManagement sample.
  20. /// </remarks>
  21. public class MessageBoxScreen : GameScreen
  22. {
  23. const string usageText = "A button = Okay\n" +
  24. "B button = Cancel";
  25. bool pauseMenu = false;
  26. string message;
  27. SpriteFont smallFont;
  28. public event EventHandler<EventArgs> Accepted;
  29. public event EventHandler<EventArgs> Cancelled;
  30. /// <summary>
  31. /// Constructor.
  32. /// </summary>
  33. public MessageBoxScreen(string message)
  34. {
  35. this.message = message;
  36. IsPopup = true;
  37. TransitionOnTime = TimeSpan.FromSeconds(0.25);
  38. TransitionOffTime = TimeSpan.FromSeconds(0.25);
  39. }
  40. /// <summary>
  41. /// Constructor.
  42. /// </summary>
  43. public MessageBoxScreen(string message, bool pauseMenu) : this(message)
  44. {
  45. this.pauseMenu = pauseMenu;
  46. }
  47. /// <summary>
  48. /// Loads graphics content for this screen. This uses the shared ContentManager
  49. /// provided by the ScreenManager, so the content will remain loaded forever.
  50. /// Whenever a subsequent MessageBoxScreen tries to load this same content,
  51. /// it will just get back another reference to the already loaded instance.
  52. /// </summary>
  53. public override void LoadContent()
  54. {
  55. smallFont = ScreenManager.Content.Load<SpriteFont>("Fonts/MessageBox");
  56. }
  57. /// <summary>
  58. /// Responds to user input, accepting or cancelling the message box.
  59. /// </summary>
  60. public override void HandleInput(InputState input)
  61. {
  62. if (input.MenuSelect && (!pauseMenu ||
  63. (input.CurrentGamePadState.Buttons.A == ButtonState.Pressed)))
  64. {
  65. // Raise the accepted event, then exit the message box.
  66. if (Accepted != null)
  67. Accepted(this, EventArgs.Empty);
  68. ExitScreen();
  69. }
  70. else if (input.MenuCancel || (input.MenuSelect && pauseMenu &&
  71. (input.CurrentGamePadState.Buttons.A == ButtonState.Released)))
  72. {
  73. // Raise the cancelled event, then exit the message box.
  74. if (Cancelled != null)
  75. Cancelled(this, EventArgs.Empty);
  76. ExitScreen();
  77. }
  78. }
  79. /// <summary>
  80. /// Draws the message box.
  81. /// </summary>
  82. public override void Draw(GameTime gameTime)
  83. {
  84. // Darken down any other screens that were drawn beneath the popup.
  85. ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
  86. // Center the message text in the viewport.
  87. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  88. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  89. Vector2 textSize = ScreenManager.Font.MeasureString(message);
  90. Vector2 textPosition = (viewportSize - textSize) / 2;
  91. Vector2 usageTextSize = smallFont.MeasureString(usageText);
  92. Vector2 usageTextPosition = (viewportSize - usageTextSize) / 2;
  93. usageTextPosition.Y = textPosition.Y +
  94. ScreenManager.Font.LineSpacing * 1.1f;
  95. // Fade the popup alpha during transitions.
  96. Color color = new Color((byte)255, (byte)255, (byte)255, (byte)TransitionAlpha);
  97. // Draw the background rectangles
  98. Rectangle rect = new Rectangle(
  99. (int)(Math.Min(usageTextPosition.X, textPosition.X)),
  100. (int)(textPosition.Y),
  101. (int)(Math.Max(usageTextSize.X, textSize.X)),
  102. (int)(ScreenManager.Font.LineSpacing * 1.1f+ usageTextSize.Y)
  103. );
  104. rect.X -= (int)(0.1f * rect.Width);
  105. rect.Y -= (int)(0.1f * rect.Height);
  106. rect.Width += (int)(0.2f * rect.Width);
  107. rect.Height += (int)(0.2f * rect.Height);
  108. Rectangle rect2 = new Rectangle(rect.X - 1, rect.Y - 1,
  109. rect.Width + 2, rect.Height + 2);
  110. ScreenManager.DrawRectangle(rect2, new Color((byte)128, (byte)128, (byte)128,
  111. (byte)(192.0f * (float)TransitionAlpha / 255.0f)));
  112. ScreenManager.DrawRectangle(rect, new Color((byte)0, (byte)0, (byte)0,
  113. (byte)(232.0f * (float)TransitionAlpha / 255.0f)));
  114. // Draw the message box text.
  115. ScreenManager.SpriteBatch.Begin();
  116. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, message,
  117. textPosition, color);
  118. ScreenManager.SpriteBatch.DrawString(smallFont, usageText,
  119. usageTextPosition, color);
  120. ScreenManager.SpriteBatch.End();
  121. }
  122. }
  123. }