//-----------------------------------------------------------------------------
// MessageBoxScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace RolePlaying
{
///
/// A popup message box screen, used to display "are you sure?"
/// confirmation messages.
///
///
/// Similar to a class found in the Game State Management sample on the
/// XNA Creators Club Online website (http://creators.xna.com).
///
class MessageBoxScreen : GameScreen
{
string message;
private Texture2D backgroundTexture;
private Vector2 backgroundPosition;
private Texture2D loadingBlackTexture;
private Rectangle loadingBlackTextureDestination;
private Texture2D backTexture;
private Vector2 backPosition;
private Texture2D selectTexture;
private Vector2 selectPosition;
private Vector2 confirmPosition, messagePosition;
public event EventHandler Accepted;
public event EventHandler Cancelled;
///
/// Constructor lets the caller specify the message.
///
public MessageBoxScreen(string message)
{
this.message = message;
IsPopup = true;
TransitionOnTime = TimeSpan.FromSeconds(0.2);
TransitionOffTime = TimeSpan.FromSeconds(0.2);
}
///
/// 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()
{
ContentManager content = ScreenManager.Game.Content;
backgroundTexture = content.Load(Path.Combine("Textures", "MainMenu", "Confirm"));
backTexture = content.Load(Path.Combine("Textures", "Buttons", "BButton"));
selectTexture = content.Load(Path.Combine("Textures", "Buttons", "AButton"));
loadingBlackTexture =
content.Load(Path.Combine("Textures", "GameScreens", "FadeScreen"));
backgroundPosition = new Vector2(
(Session.BACK_BUFFER_WIDTH - backgroundTexture.Width) / 2,
(Session.BACK_BUFFER_HEIGHT - backgroundTexture.Height) / 2);
loadingBlackTextureDestination = new Rectangle(0, 0,
Session.BACK_BUFFER_WIDTH, Session.BACK_BUFFER_HEIGHT);
backPosition = backgroundPosition + new Vector2(50f,
backgroundTexture.Height - 100);
selectPosition = backgroundPosition + new Vector2(
backgroundTexture.Width - 100, backgroundTexture.Height - 100);
confirmPosition.X = backgroundPosition.X + (backgroundTexture.Width -
Fonts.HeaderFont.MeasureString("Confirmation").X) / 2f;
confirmPosition.Y = backgroundPosition.Y + 47;
message = Fonts.BreakTextIntoLines(message, 36, 10);
messagePosition.X = backgroundPosition.X + (int)((backgroundTexture.Width -
Fonts.GearInfoFont.MeasureString(message).X) / 2);
messagePosition.Y = (backgroundPosition.Y * 2) - 20;
}
///
/// Responds to user input, accepting or cancelling the message box.
///
public override void HandleInput()
{
if (InputManager.IsActionTriggered(InputManager.InputAction.Ok))
{
// Raise the accepted event, then exit the message box.
if (Accepted != null)
Accepted(this, EventArgs.Empty);
ExitScreen();
}
else if (InputManager.IsActionTriggered(InputManager.InputAction.Back))
{
// Raise the cancelled event, then exit the message box.
if (Cancelled != null)
Cancelled(this, EventArgs.Empty);
ExitScreen();
}
}
///
/// Draws the message box.
///
public override void Draw(GameTime gameTime)
{
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
Color.White);
spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
spriteBatch.Draw(backTexture, backPosition, Color.White);
spriteBatch.Draw(selectTexture, selectPosition, Color.White);
spriteBatch.DrawString(Fonts.ButtonNamesFont, "No",
new Vector2(backPosition.X + backTexture.Width + 5, backPosition.Y + 5),
Color.White);
spriteBatch.DrawString(Fonts.ButtonNamesFont, "Yes",
new Vector2(
selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Yes").X,
selectPosition.Y + 5), Color.White);
spriteBatch.DrawString(Fonts.HeaderFont, "Confirmation", confirmPosition,
Fonts.CountColor);
spriteBatch.DrawString(Fonts.GearInfoFont, message, messagePosition,
Fonts.CountColor);
spriteBatch.End();
}
}
}