MessageBoxScreen.cs 5.9 KB

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