2
0

MessageBoxScreen.cs 5.8 KB

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