MessageBoxScreen.cs 5.6 KB

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