MessageBoxScreen.cs 5.7 KB

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