MessageBoxScreen.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Content;
  10. using Microsoft.Xna.Framework.Graphics;
  11. namespace GameStateManagement
  12. {
  13. /// <summary>
  14. /// A popup message box screen, used to display "are you sure?"
  15. /// confirmation messages.
  16. /// </summary>
  17. class MessageBoxScreen : GameScreen
  18. {
  19. string message;
  20. Texture2D gradientTexture;
  21. public event EventHandler<PlayerIndexEventArgs> Accepted;
  22. public event EventHandler<PlayerIndexEventArgs> Cancelled;
  23. /// <summary>
  24. /// Constructor automatically includes the standard "A=ok, B=cancel"
  25. /// usage text prompt.
  26. /// </summary>
  27. public MessageBoxScreen(string message)
  28. : this(message, true)
  29. { }
  30. /// <summary>
  31. /// Constructor lets the caller specify whether to include the standard
  32. /// "A=ok, B=cancel" usage text prompt.
  33. /// </summary>
  34. public MessageBoxScreen(string message, bool includeUsageText)
  35. {
  36. const string usageText = "\nA button, Space, Enter = ok" +
  37. "\nB button, Esc = cancel";
  38. if (includeUsageText)
  39. this.message = message + usageText;
  40. else
  41. this.message = message;
  42. IsPopup = true;
  43. TransitionOnTime = TimeSpan.FromSeconds(0.2);
  44. TransitionOffTime = TimeSpan.FromSeconds(0.2);
  45. }
  46. /// <summary>
  47. /// Loads graphics content for this screen. This uses the shared ContentManager
  48. /// provided by the Game class, so the content will remain loaded forever.
  49. /// Whenever a subsequent MessageBoxScreen tries to load this same content,
  50. /// it will just get back another reference to the already loaded data.
  51. /// </summary>
  52. public override void LoadContent()
  53. {
  54. ContentManager content = ScreenManager.Game.Content;
  55. gradientTexture = content.Load<Texture2D>("gradient");
  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. PlayerIndex playerIndex;
  63. // We pass in our ControllingPlayer, which may either be null (to
  64. // accept input from any player) or a specific index. If we pass a null
  65. // controlling player, the InputState helper returns to us which player
  66. // actually provided the input. We pass that through to our Accepted and
  67. // Cancelled events, so they can tell which player triggered them.
  68. if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
  69. {
  70. // Raise the accepted event, then exit the message box.
  71. if (Accepted != null)
  72. Accepted(this, new PlayerIndexEventArgs(playerIndex));
  73. ExitScreen();
  74. }
  75. else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
  76. {
  77. // Raise the cancelled event, then exit the message box.
  78. if (Cancelled != null)
  79. Cancelled(this, new PlayerIndexEventArgs(playerIndex));
  80. ExitScreen();
  81. }
  82. }
  83. /// <summary>
  84. /// Draws the message box.
  85. /// </summary>
  86. public override void Draw(GameTime gameTime)
  87. {
  88. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  89. SpriteFont font = ScreenManager.Font;
  90. // Darken down any other screens that were drawn beneath the popup.
  91. ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
  92. // Center the message text in the viewport.
  93. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  94. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  95. Vector2 textSize = font.MeasureString(message);
  96. Vector2 textPosition = (viewportSize - textSize) / 2;
  97. // The background includes a border somewhat larger than the text itself.
  98. const int hPad = 32;
  99. const int vPad = 16;
  100. Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
  101. (int)textPosition.Y - vPad,
  102. (int)textSize.X + hPad * 2,
  103. (int)textSize.Y + vPad * 2);
  104. // Fade the popup alpha during transitions.
  105. Color color = Color.White * TransitionAlpha;
  106. spriteBatch.Begin();
  107. // Draw the background rectangle.
  108. spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
  109. // Draw the message box text.
  110. spriteBatch.DrawString(font, message, textPosition, color);
  111. spriteBatch.End();
  112. }
  113. }
  114. }