#region File Description
//-----------------------------------------------------------------------------
// MenuEntry.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace RolePlaying
{
///
/// Helper class represents a single entry in a MenuScreen. By default this
/// just draws the entry text string, but it can be customized to display menu
/// entries in different ways. This also provides an event that will be raised
/// when the menu entry is selected.
///
///
/// Similar to a class found in the Game State Management sample on the
/// XNA Creators Club Online website (http://creators.xna.com).
///
class MenuEntry
{
#region Fields
///
/// The text rendered for this entry.
///
string text;
///
/// The font used for this menu item.
///
SpriteFont spriteFont;
///
/// The position of this menu item on the screen.
///
Vector2 position;
///
/// A description of the function of the button.
///
private string description;
///
/// An optional texture drawn with the text.
///
/// If present, the text will be centered on the texture.
private Texture2D texture;
#endregion
#region Properties
///
/// Gets or sets the text of this menu entry.
///
public string Text
{
get { return text; }
set { text = value; }
}
///
/// Gets or sets the font used to draw this menu entry.
///
public SpriteFont Font
{
get { return spriteFont; }
set { spriteFont = value; }
}
///
/// Gets or sets the position of this menu entry.
///
public Vector2 Position
{
get { return position; }
set { position = value; }
}
///
/// A description of the function of the button.
///
public string Description
{
get { return description; }
set { description = value; }
}
///
/// An optional texture drawn with the text.
///
/// If present, the text will be centered on the texture.
public Texture2D Texture
{
get { return texture; }
set { texture = value; }
}
#endregion
#region Events
///
/// Event raised when the menu entry is selected.
///
public event EventHandler Selected;
///
/// Method for raising the Selected event.
///
protected internal virtual void OnSelectEntry()
{
if (Selected != null)
Selected(this, EventArgs.Empty);
}
#endregion
#region Initialization
///
/// Constructs a new menu entry with the specified text.
///
public MenuEntry(string text)
{
this.text = text;
}
#endregion
#region Update and Draw
///
/// Updates the menu entry.
///
public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
{ }
///
/// Draws the menu entry. This can be overridden to customize the appearance.
///
public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
{
// Draw the selected entry in yellow, otherwise white.
Color color = isSelected ? Fonts.MenuSelectedColor : Fonts.TitleColor;
// Draw text, centered on the middle of each line.
ScreenManager screenManager = screen.ScreenManager;
SpriteBatch spriteBatch = screenManager.SpriteBatch;
if (texture != null)
{
spriteBatch.Draw(texture, position, Color.White);
if ((spriteFont != null) && !String.IsNullOrEmpty(text))
{
Vector2 textSize = spriteFont.MeasureString(text);
Vector2 textPosition = position + new Vector2(
(float)Math.Floor((texture.Width - textSize.X) / 2),
(float)Math.Floor((texture.Height - textSize.Y) / 2));
spriteBatch.DrawString(spriteFont, text, textPosition, color);
}
}
else if ((spriteFont != null) && !String.IsNullOrEmpty(text))
{
spriteBatch.DrawString(spriteFont, text, position, color);
}
}
///
/// Queries how much space this menu entry requires.
///
public virtual int GetHeight(MenuScreen screen)
{
return Font.LineSpacing;
}
#endregion
}
}