MessageBoxScreen.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if (input.Gestures.Count > 0)
  93. {
  94. foreach(var g in input.Gestures)
  95. {
  96. if (g.GestureType == Microsoft.Xna.Framework.Input.Touch.GestureType.Tap)
  97. {
  98. if (Accepted != null)
  99. Accepted(this, new PlayerIndexEventArgs(playerIndex));
  100. ExitScreen();
  101. }
  102. }
  103. }
  104. }
  105. #endregion
  106. #region Draw
  107. /// <summary>
  108. /// Draws the message box.
  109. /// </summary>
  110. public override void Draw(GameTime gameTime)
  111. {
  112. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  113. SpriteFont font = ScreenManager.Font;
  114. // Darken down any other screens that were drawn beneath the popup.
  115. ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
  116. // Center the message text in the viewport.
  117. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  118. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  119. Vector2 textSize = font.MeasureString(message);
  120. Vector2 textPosition = (viewportSize - textSize) / 2;
  121. // The background includes a border somewhat larger than the text itself.
  122. const int hPad = 32;
  123. const int vPad = 16;
  124. Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
  125. (int)textPosition.Y - vPad,
  126. (int)textSize.X + hPad * 2,
  127. (int)textSize.Y + vPad * 2);
  128. // Fade the popup alpha during transitions.
  129. Color color = Color.White * TransitionAlpha;
  130. spriteBatch.Begin();
  131. // Draw the background rectangle.
  132. spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
  133. // Draw the message box text.
  134. spriteBatch.DrawString(font, message, textPosition, color);
  135. spriteBatch.End();
  136. }
  137. #endregion
  138. }
  139. }