MessageBoxScreen.cs 5.5 KB

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