2012-04-13 03:09:49 +00:00
|
|
|
#region File Description
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// MenuScreen.cs
|
|
|
|
//
|
|
|
|
// XNA Community Game Platform
|
|
|
|
// Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Using Statements
|
2012-03-19 23:57:59 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2012-04-13 03:09:49 +00:00
|
|
|
using Microsoft.Xna.Framework.Input.Touch;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using FarseerPhysics.SamplesFramework;
|
|
|
|
#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>
|
|
|
|
/// Base class for screens that contain a menu of options. The user can
|
|
|
|
/// move up and down to select an entry, or cancel to back out of the screen.
|
|
|
|
/// </summary>
|
2012-04-18 04:34:46 +00:00
|
|
|
public class MenuScreen : GameScreen
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
#region Fields
|
|
|
|
|
2012-04-29 03:00:15 +00:00
|
|
|
// the number of pixels to pad above and below menu entries for touch input
|
|
|
|
const int menuEntryPadding = 10;
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
private List<MenuEntry> menuEntries = new List<MenuEntry>();
|
2012-04-13 03:09:49 +00:00
|
|
|
int selectedEntry = 0;
|
|
|
|
string menuTitle;
|
|
|
|
|
|
|
|
InputAction menuUp;
|
|
|
|
InputAction menuDown;
|
|
|
|
InputAction menuSelect;
|
|
|
|
InputAction menuCancel;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
/// Gets the list of menu entries, so derived classes can add
|
|
|
|
/// or change the menu contents.
|
2012-03-19 23:57:59 +00:00
|
|
|
/// </summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
protected IList<MenuEntry> MenuEntries
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
get { return menuEntries; }
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Initialization
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructor.
|
|
|
|
/// </summary>
|
|
|
|
public MenuScreen(string menuTitle)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
this.menuTitle = menuTitle;
|
2012-04-29 03:00:15 +00:00
|
|
|
// menus generally only need Tap for menu selection
|
|
|
|
EnabledGestures = GestureType.Tap;
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
TransitionOnTime = TimeSpan.FromSeconds(0.5);
|
|
|
|
TransitionOffTime = TimeSpan.FromSeconds(0.5);
|
|
|
|
|
|
|
|
menuUp = new InputAction(
|
|
|
|
new Buttons[] { Buttons.DPadUp, Buttons.LeftThumbstickUp },
|
|
|
|
new Keys[] { Keys.Up },
|
|
|
|
true);
|
|
|
|
menuDown = new InputAction(
|
|
|
|
new Buttons[] { Buttons.DPadDown, Buttons.LeftThumbstickDown },
|
|
|
|
new Keys[] { Keys.Down },
|
|
|
|
true);
|
|
|
|
menuSelect = new InputAction(
|
|
|
|
new Buttons[] { Buttons.A, Buttons.Start },
|
|
|
|
new Keys[] { Keys.Enter, Keys.Space },
|
|
|
|
true);
|
|
|
|
menuCancel = new InputAction(
|
|
|
|
new Buttons[] { Buttons.B, Buttons.Back },
|
|
|
|
new Keys[] { Keys.Escape },
|
|
|
|
true);
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
#endregion
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
public void AddMenuItem(string name)
|
|
|
|
{
|
|
|
|
|
|
|
|
menuEntries.Add(new MenuEntry(name));
|
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
#region Handle Input
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-29 03:00:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Allows the screen to create the hit bounds for a particular menu entry.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry)
|
|
|
|
{
|
|
|
|
// the hit bounds are the entire width of the screen, and the height of the entry
|
|
|
|
// with some additional padding above and below.
|
|
|
|
return new Rectangle(
|
|
|
|
0,
|
|
|
|
(int)entry.Position.Y - menuEntryPadding,
|
|
|
|
ScreenManager.GraphicsDevice.Viewport.Width,
|
|
|
|
entry.GetHeight(this) + (menuEntryPadding * 2));
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Responds to user input, changing the selected entry and accepting
|
|
|
|
/// or cancelling the menu.
|
|
|
|
/// </summary>
|
|
|
|
public override void HandleInput(GameTime gameTime, InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
// For input tests 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
|
|
|
|
// OnSelectEntry and OnCancel, so they can tell which player triggered them.
|
2012-04-29 03:00:15 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-04-29 03:00:15 +00:00
|
|
|
#if WINDOWS || XBOX360
|
|
|
|
PlayerIndex playerIndex;
|
2012-04-13 03:09:49 +00:00
|
|
|
// Move to the previous menu entry?
|
|
|
|
if (menuUp.Evaluate(input, ControllingPlayer, out playerIndex))
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
selectedEntry--;
|
|
|
|
|
|
|
|
if (selectedEntry < 0)
|
|
|
|
selectedEntry = menuEntries.Count - 1;
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
// Move to the next menu entry?
|
|
|
|
if (menuDown.Evaluate(input, ControllingPlayer, out playerIndex))
|
|
|
|
{
|
|
|
|
selectedEntry++;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
if (selectedEntry >= menuEntries.Count)
|
|
|
|
selectedEntry = 0;
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex))
|
|
|
|
{
|
|
|
|
OnSelectEntry(selectedEntry, playerIndex);
|
|
|
|
}
|
|
|
|
else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex))
|
|
|
|
{
|
|
|
|
OnCancel(playerIndex);
|
|
|
|
}
|
2012-04-29 03:00:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WINDOWS_PHONE
|
|
|
|
//selectedEntry = 1;
|
|
|
|
|
|
|
|
PlayerIndex player;
|
|
|
|
if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
|
|
|
|
{
|
|
|
|
OnCancel(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for any taps that occurred and select any entries that were tapped
|
|
|
|
foreach (GestureSample gesture in input.Gestures)
|
|
|
|
{
|
|
|
|
//System.Diagnostics.Debugger.Break();
|
|
|
|
if (gesture.GestureType == GestureType.Tap)
|
|
|
|
{
|
|
|
|
// convert the position to a Point that we can test against a Rectangle
|
|
|
|
Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
|
|
|
|
|
|
|
|
// iterate the entries to see if any were tapped
|
|
|
|
for (int i = 0; i < menuEntries.Count; i++)
|
|
|
|
{
|
|
|
|
MenuEntry menuEntry = menuEntries[i];
|
|
|
|
|
|
|
|
if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
|
|
|
|
{
|
|
|
|
// select the entry. since gestures are only available on Windows Phone,
|
|
|
|
// we can safely pass PlayerIndex.One to all entries since there is only
|
|
|
|
// one player on Windows Phone.
|
|
|
|
OnSelectEntry(i, PlayerIndex.One);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-13 03:09:49 +00:00
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Handler for when the user has chosen a menu entry.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
|
|
|
|
{
|
|
|
|
menuEntries[entryIndex].OnSelectEntry(playerIndex);
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
/// <summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
/// Handler for when the user has cancelled the menu.
|
2012-03-19 23:57:59 +00:00
|
|
|
/// </summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
protected virtual void OnCancel(PlayerIndex playerIndex)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
ExitScreen();
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
/// <summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
/// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
|
2012-03-19 23:57:59 +00:00
|
|
|
/// </summary>
|
2012-04-13 03:09:49 +00:00
|
|
|
protected void OnCancel(object sender, PlayerIndexEventArgs e)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
OnCancel(e.PlayerIndex);
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Update and Draw
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Allows the screen the chance to position the menu entries. By default
|
|
|
|
/// all menu entries are lined up in a vertical list, centered on the screen.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual void UpdateMenuEntryLocations()
|
|
|
|
{
|
|
|
|
// Make the menu slide into place during transitions, using a
|
|
|
|
// power curve to make things look more interesting (this makes
|
|
|
|
// the movement slow down as it nears the end).
|
|
|
|
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
// start at Y = 175; each X value is generated per entry
|
|
|
|
Vector2 position = new Vector2(0f, 175f);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
// update each menu entry's location in turn
|
2012-04-13 03:09:49 +00:00
|
|
|
for (int i = 0; i < menuEntries.Count; i++)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
MenuEntry menuEntry = menuEntries[i];
|
|
|
|
|
|
|
|
// each entry is to be centered horizontally
|
|
|
|
position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
if (ScreenState == ScreenState.TransitionOn)
|
|
|
|
position.X -= transitionOffset * 256;
|
|
|
|
else
|
2012-04-13 03:09:49 +00:00
|
|
|
position.X += transitionOffset * 512;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
// set the entry's position
|
2012-04-13 03:09:49 +00:00
|
|
|
menuEntry.Position = position;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
// move down for the next entry the size of this entry
|
2012-04-13 03:09:49 +00:00
|
|
|
position.Y += menuEntry.GetHeight(this);
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Updates the menu.
|
|
|
|
/// </summary>
|
|
|
|
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
|
2012-04-13 03:09:49 +00:00
|
|
|
bool coveredByOtherScreen)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
|
|
|
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
|
|
|
|
|
|
|
|
// Update each nested MenuEntry object.
|
2012-04-13 03:09:49 +00:00
|
|
|
for (int i = 0; i < menuEntries.Count; i++)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
bool isSelected = IsActive && (i == selectedEntry);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
menuEntries[i].Update(this, isSelected, gameTime);
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Draws the menu.
|
|
|
|
/// </summary>
|
|
|
|
public override void Draw(GameTime gameTime)
|
|
|
|
{
|
|
|
|
// make sure our entries are in the right place before we draw them
|
|
|
|
UpdateMenuEntryLocations();
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
GraphicsDevice graphics = ScreenManager.GraphicsDevice;
|
2012-03-19 23:57:59 +00:00
|
|
|
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
|
2012-04-13 03:09:49 +00:00
|
|
|
SpriteFont font = ScreenManager.Font;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
spriteBatch.Begin();
|
2012-04-13 03:09:49 +00:00
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
// Draw each menu entry in turn.
|
2012-04-13 03:09:49 +00:00
|
|
|
for (int i = 0; i < menuEntries.Count; i++)
|
2012-03-19 23:57:59 +00:00
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
MenuEntry menuEntry = menuEntries[i];
|
|
|
|
|
|
|
|
bool isSelected = IsActive && (i == selectedEntry);
|
|
|
|
|
|
|
|
menuEntry.Draw(this, isSelected, gameTime);
|
2012-03-19 23:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the menu slide into place during transitions, using a
|
|
|
|
// power curve to make things look more interesting (this makes
|
|
|
|
// the movement slow down as it nears the end).
|
2012-04-13 03:09:49 +00:00
|
|
|
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
|
|
|
|
|
|
|
|
// Draw the menu title centered on the screen
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
}
|