using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace FarseerPhysics.SamplesFramework { /// /// A popup message box screen, used to display "are you sure?" /// confirmation messages. /// public class MessageBoxScreen : GameScreen { protected Rectangle _backgroundRectangle; protected Texture2D _gradientTexture; protected string _message; protected Vector2 _textPosition; public MessageBoxScreen(string message) { _message = message; IsPopup = true; HasCursor = true; TransitionOnTime = TimeSpan.FromSeconds(0.4); TransitionOffTime = TimeSpan.FromSeconds(0.4); } public MessageBoxScreen() { IsPopup = true; } /// /// Loads graphics content for this screen. This uses the shared ContentManager /// provided by the Game class, so the content will remain loaded forever. /// Whenever a subsequent MessageBoxScreen tries to load this same content, /// it will just get back another reference to the already loaded data. /// public override void LoadContent() { SpriteFont font = ScreenManager.Fonts.DetailsFont; ContentManager content = ScreenManager.Game.Content; _gradientTexture = content.Load("Common/popup"); // Center the message text in the viewport. Viewport viewport = ScreenManager.GraphicsDevice.Viewport; Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height); Vector2 textSize = font.MeasureString(_message); _textPosition = (viewportSize - textSize) / 2; // The background includes a border somewhat larger than the text itself. const int hPad = 32; const int vPad = 16; _backgroundRectangle = new Rectangle((int)_textPosition.X - hPad, (int)_textPosition.Y - vPad, (int)textSize.X + hPad * 2, (int)textSize.Y + vPad * 2); } /// /// Responds to user input, accepting or cancelling the message box. /// public override void HandleInput(InputHelper input, GameTime gameTime) { if (input.IsMenuSelect() || input.IsMenuCancel() || input.IsNewMouseButtonPress(MouseButtons.LeftButton)) { ExitScreen(); } } /// /// Draws the message box. /// public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; SpriteFont font = ScreenManager.Fonts.DetailsFont; // Fade the popup alpha during transitions. Color color = Color.White * TransitionAlpha * (2f / 3f); spriteBatch.Begin(); // Draw the background rectangle. spriteBatch.Draw(_gradientTexture, _backgroundRectangle, color); // Draw the message box text. spriteBatch.DrawString(font, _message, _textPosition + Vector2.One, Color.Black); spriteBatch.DrawString(font, _message, _textPosition, Color.White); spriteBatch.End(); } } }