MessageBoxScreen.cs 5.8 KB

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