#region File Description
#endregion
using
System;
using
System.Collections.Generic;
using
GameStateManagement;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
Microsoft.Xna.Framework.Input.Touch;
namespace
GameStateManagement
{
/// <summary>
/// Provides a basic base screen for menus on Windows Phone leveraging the Button class.
/// </summary>
class
PhoneMenuScreen : GameScreen
{
List<Button> menuButtons =
new
List<Button>();
string
menuTitle;
InputAction menuCancel;
/// <summary>
/// Gets the list of buttons, so derived classes can add or change the menu contents.
/// </summary>
protected
IList<Button> MenuButtons
{
get
{
return
menuButtons; }
}
/// <summary>
/// Creates the PhoneMenuScreen with a particular title.
/// </summary>
/// <param name="title">The title of the screen</param>
public
PhoneMenuScreen(
string
title)
{
menuTitle = title;
TransitionOnTime = TimeSpan.FromSeconds(0.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
menuCancel =
new
InputAction(
new
Buttons[] { Buttons.Back },
null
,
true
);
EnabledGestures = GestureType.Tap;
}
public
override
void
Activate(
bool
instancePreserved)
{
float
y = 140f;
float
center = ScreenManager.GraphicsDevice.Viewport.Bounds.Center.X;
for
(
int
i = 0; i < MenuButtons.Count; i++)
{
Button b = MenuButtons[i];
b.Position =
new
Vector2(center - b.Size.X / 2, y);
y += b.Size.Y * 1.5f;
}
base
.Activate(instancePreserved);
}
public
override
void
Update(GameTime gameTime,
bool
otherScreenHasFocus,
bool
coveredByOtherScreen)
{
foreach
(Button b
in
menuButtons)
{
b.Alpha = TransitionAlpha;
}
base
.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
/// <summary>
/// An overrideable method called whenever the menuCancel action is triggered
/// </summary>
protected
virtual
void
OnCancel() { }
public
override
void
HandleInput(GameTime gameTime, InputState input)
{
PlayerIndex player;
if
(menuCancel.Evaluate(input, ControllingPlayer,
out
player))
{
OnCancel();
}
foreach
(GestureSample gesture
in
input.Gestures)
{
if
(gesture.GestureType == GestureType.Tap)
{
foreach
(Button b
in
menuButtons)
{
if
(b.HandleTap(gesture.Position))
break
;
}
}
}
base
.HandleInput(gameTime, input);
}
public
override
void
Draw(GameTime gameTime)
{
GraphicsDevice graphics = ScreenManager.GraphicsDevice;
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Font;
spriteBatch.Begin();
foreach
(Button b
in
menuButtons)
b.Draw(
this
);
float
transitionOffset = (
float
)Math.Pow(TransitionPosition, 2);
Vector2 titlePosition =
new
Vector2(graphics.Viewport.Width / 2, 80);
Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
Color titleColor =
new
Color(192, 192, 192) * TransitionAlpha;
float
titleScale = 1.25f;
titlePosition.Y -= transitionOffset * 100;
spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
titleOrigin, titleScale, SpriteEffects.None, 0);
spriteBatch.End();
base
.Draw(gameTime);
}
}
}