2012-04-13 03:09:49 +00:00
|
|
|
#region File Description
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// MessageBoxScreen.cs
|
|
|
|
//
|
|
|
|
// Microsoft XNA Community Game Platform
|
|
|
|
// Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Using Statements
|
|
|
|
using System;
|
2012-03-19 23:57:59 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2012-04-13 03:09:49 +00:00
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using GameStateManagement;
|
|
|
|
#endregion
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
namespace GameStateManagement
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A popup message box screen, used to display "are you sure?"
|
|
|
|
/// confirmation messages.
|
|
|
|
/// </summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
class MessageBoxScreen : GameScreen
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
#region Fields
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
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>
|
2012-03-19 23:57:59 +00:00
|
|
|
public MessageBoxScreen(string message)
|
2012-04-13 03:09:49 +00:00
|
|
|
: 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)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
const string usageText = "\nA button, Space, Enter = ok" +
|
|
|
|
"\nB button, Esc = cancel";
|
|
|
|
|
|
|
|
if (includeUsageText)
|
|
|
|
this.message = message + usageText;
|
|
|
|
else
|
|
|
|
this.message = message;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
IsPopup = true;
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
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);
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2012-04-13 03:09:49 +00:00
|
|
|
public override void Activate(bool instancePreserved)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
if (!instancePreserved)
|
|
|
|
{
|
|
|
|
ContentManager content = ScreenManager.Game.Content;
|
|
|
|
gradientTexture = content.Load<Texture2D>("gradient");
|
|
|
|
}
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Handle Input
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Responds to user input, accepting or cancelling the message box.
|
|
|
|
/// </summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
public override void HandleInput(GameTime gameTime, InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
PlayerIndex playerIndex;
|
|
|
|
|
|
|
|
// We pass in our ControllingPlayer, which may either be null (to
|
|
|
|
// accept input from any player) or a specific index. If we pass a null
|
|
|
|
// controlling player, the InputState helper returns to us which player
|
|
|
|
// actually provided the input. We pass that through to our Accepted and
|
|
|
|
// Cancelled events, so they can tell which player triggered them.
|
|
|
|
if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex))
|
|
|
|
{
|
|
|
|
// Raise the accepted event, then exit the message box.
|
|
|
|
if (Accepted != null)
|
|
|
|
Accepted(this, new PlayerIndexEventArgs(playerIndex));
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
ExitScreen();
|
|
|
|
}
|
|
|
|
else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex))
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
// Raise the cancelled event, then exit the message box.
|
|
|
|
if (Cancelled != null)
|
|
|
|
Cancelled(this, new PlayerIndexEventArgs(playerIndex));
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
ExitScreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Draw
|
|
|
|
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Draws the message box.
|
|
|
|
/// </summary>
|
|
|
|
public override void Draw(GameTime gameTime)
|
|
|
|
{
|
|
|
|
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
|
2012-04-13 03:09:49 +00:00
|
|
|
SpriteFont font = ScreenManager.Font;
|
|
|
|
|
|
|
|
// Darken down any other screens that were drawn beneath the popup.
|
|
|
|
ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
Vector2 textPosition = (viewportSize - textSize) / 2;
|
|
|
|
|
|
|
|
// The background includes a border somewhat larger than the text itself.
|
|
|
|
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);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
// Fade the popup alpha during transitions.
|
2012-04-13 03:09:49 +00:00
|
|
|
Color color = Color.White * TransitionAlpha;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
spriteBatch.Begin();
|
|
|
|
|
|
|
|
// Draw the background rectangle.
|
2012-04-13 03:09:49 +00:00
|
|
|
spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
// Draw the message box text.
|
2012-04-13 03:09:49 +00:00
|
|
|
spriteBatch.DrawString(font, message, textPosition, color);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
spriteBatch.End();
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
}
|