MessageBoxScreen.cs 6.1 KB

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