2
0

MessageBoxScreen.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace FarseerPhysics.SamplesFramework
  6. {
  7. /// <summary>
  8. /// A popup message box screen, used to display "are you sure?"
  9. /// confirmation messages.
  10. /// </summary>
  11. public class MessageBoxScreen : GameScreen
  12. {
  13. private Rectangle _backgroundRectangle;
  14. private Texture2D _gradientTexture;
  15. private string _message;
  16. private Vector2 _textPosition;
  17. public MessageBoxScreen(string message)
  18. {
  19. _message = message;
  20. IsPopup = true;
  21. HasCursor = true;
  22. TransitionOnTime = TimeSpan.FromSeconds(0.4);
  23. TransitionOffTime = TimeSpan.FromSeconds(0.4);
  24. }
  25. /// <summary>
  26. /// Loads graphics content for this screen. This uses the shared ContentManager
  27. /// provided by the Game class, so the content will remain loaded forever.
  28. /// Whenever a subsequent MessageBoxScreen tries to load this same content,
  29. /// it will just get back another reference to the already loaded data.
  30. /// </summary>
  31. public override void LoadContent()
  32. {
  33. SpriteFont font = ScreenManager.Fonts.DetailsFont;
  34. ContentManager content = ScreenManager.Game.Content;
  35. _gradientTexture = content.Load<Texture2D>("Common/popup");
  36. // Center the message text in the viewport.
  37. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  38. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  39. Vector2 textSize = font.MeasureString(_message);
  40. _textPosition = (viewportSize - textSize) / 2;
  41. // The background includes a border somewhat larger than the text itself.
  42. const int hPad = 32;
  43. const int vPad = 16;
  44. _backgroundRectangle = new Rectangle((int)_textPosition.X - hPad,
  45. (int)_textPosition.Y - vPad,
  46. (int)textSize.X + hPad * 2,
  47. (int)textSize.Y + vPad * 2);
  48. }
  49. /// <summary>
  50. /// Responds to user input, accepting or cancelling the message box.
  51. /// </summary>
  52. public override void HandleInput(InputHelper input, GameTime gameTime)
  53. {
  54. if (input.IsMenuSelect() || input.IsMenuCancel() ||
  55. input.IsNewMouseButtonPress(MouseButtons.LeftButton))
  56. {
  57. ExitScreen();
  58. }
  59. }
  60. /// <summary>
  61. /// Draws the message box.
  62. /// </summary>
  63. public override void Draw(GameTime gameTime)
  64. {
  65. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  66. SpriteFont font = ScreenManager.Fonts.DetailsFont;
  67. // Fade the popup alpha during transitions.
  68. Color color = Color.White * TransitionAlpha * (2f / 3f);
  69. spriteBatch.Begin();
  70. // Draw the background rectangle.
  71. spriteBatch.Draw(_gradientTexture, _backgroundRectangle, color);
  72. // Draw the message box text.
  73. spriteBatch.DrawString(font, _message, _textPosition + Vector2.One, Color.Black);
  74. spriteBatch.DrawString(font, _message, _textPosition, Color.White);
  75. spriteBatch.End();
  76. }
  77. }
  78. }