#region File Description
#endregion
#region Using Statements
using
System;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Content;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
GameStateManagement;
#endregion
namespace
GameStateManagement
{
/// <summary>
/// A popup message box screen, used to display "are you sure?"
/// confirmation messages.
/// </summary>
class
MessageBoxScreen : GameScreen
{
#region Fields
string
message;
Texture2D gradientTexture;
InputAction menuSelect;
InputAction menuCancel;
#endregion
#region Events
public
event
EventHandler<PlayerIndexEventArgs> Accepted;
public
event
EventHandler<PlayerIndexEventArgs> Cancelled;
#endregion
#region Initialization
/// <summary>
/// Constructor automatically includes the standard "A=ok, B=cancel"
/// usage text prompt.
/// </summary>
public
MessageBoxScreen(
string
message)
:
this
(message,
true
)
{ }
/// <summary>
/// Constructor lets the caller specify whether to include the standard
/// "A=ok, B=cancel" usage text prompt.
/// </summary>
public
MessageBoxScreen(
string
message,
bool
includeUsageText)
{
const
string
usageText =
"\nA button, Space, Enter = ok"
+
"\nB button, Esc = cancel"
;
if
(includeUsageText)
this
.message = message + usageText;
else
this
.message = message;
IsPopup =
true
;
TransitionOnTime = TimeSpan.FromSeconds(0.2);
TransitionOffTime = TimeSpan.FromSeconds(0.2);
menuSelect =
new
InputAction(
new
Buttons[] { Buttons.A, Buttons.Start },
new
Keys[] { Keys.Space, Keys.Enter },
true
);
menuCancel =
new
InputAction(
new
Buttons[] { Buttons.B, Buttons.Back },
new
Keys[] { Keys.Escape, Keys.Back },
true
);
}
/// <summary>
/// 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.
/// </summary>
public
override
void
Activate(
bool
instancePreserved)
{
if
(!instancePreserved)
{
ContentManager content = ScreenManager.Game.Content;
gradientTexture = content.Load<Texture2D>(
"gradient"
);
}
}
#endregion
#region Handle Input
/// <summary>
/// Responds to user input, accepting or cancelling the message box.
/// </summary>
public
override
void
HandleInput(GameTime gameTime, InputState input)
{
PlayerIndex playerIndex;
if
(menuSelect.Evaluate(input, ControllingPlayer,
out
playerIndex))
{
if
(Accepted !=
null
)
Accepted(
this
,
new
PlayerIndexEventArgs(playerIndex));
ExitScreen();
}
else
if
(menuCancel.Evaluate(input, ControllingPlayer,
out
playerIndex))
{
if
(Cancelled !=
null
)
Cancelled(
this
,
new
PlayerIndexEventArgs(playerIndex));
ExitScreen();
}
}
#endregion
#region Draw
/// <summary>
/// Draws the message box.
/// </summary>
public
override
void
Draw(GameTime gameTime)
{
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Font;
ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
Vector2 viewportSize =
new
Vector2(viewport.Width, viewport.Height);
Vector2 textSize = font.MeasureString(message);
Vector2 textPosition = (viewportSize - textSize) / 2;
const
int
hPad = 32;
const
int
vPad = 16;
Rectangle backgroundRectangle =
new
Rectangle((
int
)textPosition.X - hPad,
(
int
)textPosition.Y - vPad,
(
int
)textSize.X + hPad * 2,
(
int
)textSize.Y + vPad * 2);
Color color = Color.White * TransitionAlpha;
spriteBatch.Begin();
spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
spriteBatch.DrawString(font, message, textPosition, color);
spriteBatch.End();
}
#endregion
}
}