using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Menu
{
///
/// Menu Class for the XNA Framework
/// by Abraham Morales, 2008.
///
class Menu
{
private int index = 0; //Total menu items.
private int selectedIndex = 0; //Index of selected item.
private string[] items = new string[20]; //Array for all the menu items.
private Vector2[] position = new Vector2[20]; //Position of displayed item.
private Color unselectedColor = Color.White; //Item color when it isn't selected.
private Color selectedColor = Color.Red; //Item color when it is selected.
private SpriteFont spriteFont; //Font used for the displayed items.
private int xTab = 0; //Integer used to move the selected menu item X pixels.
private int yTab = 0; //Integer used to move the selected menu item Y pixels.
private Texture2D cursorTexture; //Texture for the selected item cursor.
private bool drawCursor = false; //Checks to see if the menu object called for a cursor.
private bool scaleItem = false; //Checks to see if the menu object called for scalable items.
private double[] scaleArray = new double[20]; //Used to scale the menu item's font.
///
/// Create a new instance from Menu.
///
/// Specifies the item's font. Must be loaded from the Content Pipeline using SpriteFont.
public Menu(SpriteFont font)
{
spriteFont = font;
}
///
/// Create a new instance from Menu.
///
/// Specifies the color when the item is not selected.
/// Specifies the color when the item is selected.
/// Specifies the item's font. Must be loaded from the Content Pipeline using SpriteFont.
/// Scales menu's selected item.
public Menu(Color unselected, Color selected, SpriteFont font, bool scale)
{
spriteFont = font;
unselectedColor = unselected;
selectedColor = selected;
scaleItem = scale;
}
///
/// Create a new instance from Menu with a tabbed selected item.
///
/// Specifies the color when the item is not selected.
/// Specifies the color when the item is selected.
/// Specifies the item's font. Must be loaded from the Content Pipeline using SpriteFont.
/// X coordinates to move the menu item when selected.
/// y coordinates to move the menu item when selected.
public Menu(Color unselected, Color selected, SpriteFont font, int x, int y)
{
spriteFont = font;
unselectedColor = unselected;
selectedColor = selected;
xTab = x;
yTab = y;
}
///
/// Create a new instance from Menu with a menu cursor.
///
/// Specifies the color when the item is not selected.
/// Specifies the color when the item is selected.
/// Specifies the item's font. Must be loaded from the Content Pipeline using SpriteFont.
/// A texture for the menu's cursor. Image must be 25x25 pixels.
public Menu(Color unselected, Color selected, SpriteFont font, Texture2D cursor)
{
spriteFont = font;
unselectedColor = unselected;
selectedColor = selected;
cursorTexture = cursor;
drawCursor = true;
}
///
/// Returns the item's position in the Menu Item's array.
///
public int SelectedIndex
{
get { return selectedIndex; }
}
///
/// Returns the item's name in the Menu Item's array.
///
public string SelectedItem
{
get { return items[selectedIndex]; }
set { items[selectedIndex] = value; }
}
///
/// Adds an item to the Menu Item's array.
///
/// Specifies the name of the item to be displayed.
/// Specifies the coordinates of where to display the item.
public void Add(string itemName, Vector2 itemPosition)
{
if (index < 20)
{
items[index] = itemName;
scaleArray[index] = 1.0f;
position[index++] = itemPosition;
}
}
///
/// Moves forward one item in the Menu Item's array.
///
public void Next()
{
if (selectedIndex < index - 1)
selectedIndex++;
else
selectedIndex = 0;
}
///
/// Moves backward one item in the Menu Item's array.
///
public void Previous()
{
if (selectedIndex > 0)
selectedIndex--;
else
selectedIndex = index - 1;
}
///
/// Updates the Menu.
///
/// Snapshot of the game timing state expressed in values that can be used by variable-step (real time) or fixed-step (game time) games.
public void Update(GameTime gameTime)
{
for (int i = 0; i < index; i++)
{
if (i == selectedIndex)
{
if (scaleArray[i] < 2.0f)
scaleArray[i] += 0.04 + 10.0f * gameTime.ElapsedGameTime.Seconds;
}
else if (scaleArray[i] > 1.0f && i != selectedIndex)
scaleArray[i] -= 0.04 + 10.0f * gameTime.ElapsedGameTime.Seconds;
}
}
///
/// Draws the Menu.
///
/// Specifies the name of the instance from the SpriteBatch class.
public void Draw(SpriteBatch spriteBatch)
{
//spriteBatch.Begin();
if (scaleItem == false)
{
if (drawCursor == false)
{
for (int i = 0; i < index; i++)
{
if (i == selectedIndex)
spriteBatch.DrawString(spriteFont, items[i], position[i] + new Vector2(xTab, yTab), selectedColor);
else
spriteBatch.DrawString(spriteFont, items[i], position[i], unselectedColor);
}
}
else
{
for (int i = 0; i < index; i++)
{
if (i == selectedIndex)
{
spriteBatch.Draw(cursorTexture, position[i] - new Vector2(30, 0), Color.White);
spriteBatch.DrawString(spriteFont, items[i], position[i] + new Vector2(0, 0), selectedColor);
}
else
spriteBatch.DrawString(spriteFont, items[i], position[i], unselectedColor);
}
}
}
else
{
for (int i = 0; i < index; i++)
{
Vector2 itemPosition = position[i];
itemPosition.X -= (float)(22 * scaleArray[i] / 2);
itemPosition.Y -= (float)(22 * scaleArray[i] / 2);
if (i == selectedIndex)
spriteBatch.DrawString(spriteFont, items[i], itemPosition, selectedColor, 0.0f, new Vector2(0, 0), (float)scaleArray[i], SpriteEffects.None, 0);
else
spriteBatch.DrawString(spriteFont, items[i], itemPosition, unselectedColor, 0.0f, new Vector2(0, 0), (float)scaleArray[i], SpriteEffects.None, 0);
}
}
//spriteBatch.End();
}
}
}