MessageBoxScreen.cs 6.0 KB

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