MessageBoxScreen.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. namespace RolePlaying
  12. {
  13. /// <summary>
  14. /// A popup message box screen, used to display "are you sure?"
  15. /// confirmation messages.
  16. /// </summary>
  17. /// <remarks>
  18. /// Similar to a class found in the Game State Management sample on the
  19. /// XNA Creators Club Online website (http://creators.xna.com).
  20. /// </remarks>
  21. class MessageBoxScreen : GameScreen
  22. {
  23. string message;
  24. private Texture2D backgroundTexture;
  25. private Vector2 backgroundPosition;
  26. private Texture2D loadingBlackTexture;
  27. private Rectangle loadingBlackTextureDestination;
  28. private Texture2D backTexture;
  29. private Vector2 backPosition;
  30. private Texture2D selectTexture;
  31. private Vector2 selectPosition;
  32. private Vector2 confirmPosition, messagePosition;
  33. public event EventHandler<EventArgs> Accepted;
  34. public event EventHandler<EventArgs> Cancelled;
  35. /// <summary>
  36. /// Constructor lets the caller specify the message.
  37. /// </summary>
  38. public MessageBoxScreen(string message)
  39. {
  40. this.message = message;
  41. IsPopup = true;
  42. TransitionOnTime = TimeSpan.FromSeconds(0.2);
  43. TransitionOffTime = TimeSpan.FromSeconds(0.2);
  44. }
  45. /// <summary>
  46. /// Loads graphics content for this screen. This uses the shared ContentManager
  47. /// provided by the Game class, so the content will remain loaded forever.
  48. /// Whenever a subsequent MessageBoxScreen tries to load this same content,
  49. /// it will just get back another reference to the already loaded data.
  50. /// </summary>
  51. public override void LoadContent()
  52. {
  53. ContentManager content = ScreenManager.Game.Content;
  54. backgroundTexture = content.Load<Texture2D>(@"Textures\MainMenu\Confirm");
  55. backTexture = content.Load<Texture2D>(@"Textures\Buttons\BButton");
  56. selectTexture = content.Load<Texture2D>(@"Textures\Buttons\AButton");
  57. loadingBlackTexture =
  58. content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  59. backgroundPosition = new Vector2(
  60. (Session.BACK_BUFFER_WIDTH - backgroundTexture.Width) / 2,
  61. (Session.BACK_BUFFER_HEIGHT - backgroundTexture.Height) / 2);
  62. loadingBlackTextureDestination = new Rectangle(0, 0,
  63. Session.BACK_BUFFER_WIDTH, Session.BACK_BUFFER_HEIGHT);
  64. backPosition = backgroundPosition + new Vector2(50f,
  65. backgroundTexture.Height - 100);
  66. selectPosition = backgroundPosition + new Vector2(
  67. backgroundTexture.Width - 100, backgroundTexture.Height - 100);
  68. confirmPosition.X = backgroundPosition.X + (backgroundTexture.Width -
  69. Fonts.HeaderFont.MeasureString("Confirmation").X) / 2f;
  70. confirmPosition.Y = backgroundPosition.Y + 47;
  71. message = Fonts.BreakTextIntoLines(message, 36, 10);
  72. messagePosition.X = backgroundPosition.X + (int)((backgroundTexture.Width -
  73. Fonts.GearInfoFont.MeasureString(message).X) / 2);
  74. messagePosition.Y = (backgroundPosition.Y * 2) - 20;
  75. }
  76. /// <summary>
  77. /// Responds to user input, accepting or cancelling the message box.
  78. /// </summary>
  79. public override void HandleInput()
  80. {
  81. if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  82. {
  83. // Raise the accepted event, then exit the message box.
  84. if (Accepted != null)
  85. Accepted(this, EventArgs.Empty);
  86. ExitScreen();
  87. }
  88. else if (InputManager.IsActionTriggered(InputManager.Action.Back))
  89. {
  90. // Raise the cancelled event, then exit the message box.
  91. if (Cancelled != null)
  92. Cancelled(this, EventArgs.Empty);
  93. ExitScreen();
  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. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  103. spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
  104. Color.White);
  105. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  106. spriteBatch.Draw(backTexture, backPosition, Color.White);
  107. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  108. spriteBatch.DrawString(Fonts.ButtonNamesFont, "No",
  109. new Vector2(backPosition.X + backTexture.Width + 5, backPosition.Y + 5),
  110. Color.White);
  111. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Yes",
  112. new Vector2(
  113. selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Yes").X,
  114. selectPosition.Y + 5), Color.White);
  115. spriteBatch.DrawString(Fonts.HeaderFont, "Confirmation", confirmPosition,
  116. Fonts.CountColor);
  117. spriteBatch.DrawString(Fonts.GearInfoFont, message, messagePosition,
  118. Fonts.CountColor);
  119. spriteBatch.End();
  120. }
  121. }
  122. }