2
0

MessageBoxScreen.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// A popup message box screen, used to display "are you sure?"
  19. /// confirmation messages.
  20. /// </summary>
  21. /// <remarks>
  22. /// Similar to a class found in the Game State Management sample on the
  23. /// XNA Creators Club Online website (http://creators.xna.com).
  24. /// </remarks>
  25. class MessageBoxScreen : GameScreen
  26. {
  27. #region Fields
  28. string message;
  29. private Texture2D backgroundTexture;
  30. private Vector2 backgroundPosition;
  31. private Texture2D loadingBlackTexture;
  32. private Rectangle loadingBlackTextureDestination;
  33. private Texture2D backTexture;
  34. private Vector2 backPosition;
  35. private Texture2D selectTexture;
  36. private Vector2 selectPosition;
  37. private Vector2 confirmPosition, messagePosition;
  38. #endregion
  39. #region Events
  40. public event EventHandler<EventArgs> Accepted;
  41. public event EventHandler<EventArgs> Cancelled;
  42. #endregion
  43. #region Initialization
  44. /// <summary>
  45. /// Constructor lets the caller specify the message.
  46. /// </summary>
  47. public MessageBoxScreen(string message)
  48. {
  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. backgroundTexture = content.Load<Texture2D>(@"Textures\MainMenu\Confirm");
  64. backTexture = content.Load<Texture2D>(@"Textures\Buttons\BButton");
  65. selectTexture = content.Load<Texture2D>(@"Textures\Buttons\AButton");
  66. loadingBlackTexture =
  67. content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  68. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  69. backgroundPosition = new Vector2(
  70. (viewport.Width - backgroundTexture.Width) / 2,
  71. (viewport.Height - backgroundTexture.Height) / 2);
  72. loadingBlackTextureDestination = new Rectangle(viewport.X, viewport.Y,
  73. viewport.Width, viewport.Height);
  74. backPosition = backgroundPosition + new Vector2(50f,
  75. backgroundTexture.Height - 100);
  76. selectPosition = backgroundPosition + new Vector2(
  77. backgroundTexture.Width - 100, backgroundTexture.Height - 100);
  78. confirmPosition.X = backgroundPosition.X + (backgroundTexture.Width -
  79. Fonts.HeaderFont.MeasureString("Confirmation").X) / 2f;
  80. confirmPosition.Y = backgroundPosition.Y + 47;
  81. message = Fonts.BreakTextIntoLines(message, 36, 10);
  82. messagePosition.X = backgroundPosition.X + (int)((backgroundTexture.Width -
  83. Fonts.GearInfoFont.MeasureString(message).X) / 2);
  84. messagePosition.Y = (backgroundPosition.Y * 2) - 20;
  85. }
  86. #endregion
  87. #region Handle Input
  88. /// <summary>
  89. /// Responds to user input, accepting or cancelling the message box.
  90. /// </summary>
  91. public override void HandleInput()
  92. {
  93. if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  94. {
  95. // Raise the accepted event, then exit the message box.
  96. if (Accepted != null)
  97. Accepted(this, EventArgs.Empty);
  98. ExitScreen();
  99. }
  100. else if (InputManager.IsActionTriggered(InputManager.Action.Back))
  101. {
  102. // Raise the cancelled event, then exit the message box.
  103. if (Cancelled != null)
  104. Cancelled(this, EventArgs.Empty);
  105. ExitScreen();
  106. }
  107. }
  108. #endregion
  109. #region Draw
  110. /// <summary>
  111. /// Draws the message box.
  112. /// </summary>
  113. public override void Draw(GameTime gameTime)
  114. {
  115. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  116. spriteBatch.Begin();
  117. spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
  118. Color.White);
  119. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  120. spriteBatch.Draw(backTexture, backPosition, Color.White);
  121. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  122. spriteBatch.DrawString(Fonts.ButtonNamesFont, "No",
  123. new Vector2(backPosition.X + backTexture.Width + 5, backPosition.Y + 5),
  124. Color.White);
  125. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Yes",
  126. new Vector2(
  127. selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Yes").X,
  128. selectPosition.Y + 5), Color.White);
  129. spriteBatch.DrawString(Fonts.HeaderFont, "Confirmation", confirmPosition,
  130. Fonts.CountColor);
  131. spriteBatch.DrawString(Fonts.GearInfoFont, message, messagePosition,
  132. Fonts.CountColor);
  133. spriteBatch.End();
  134. }
  135. #endregion
  136. }
  137. }