//-----------------------------------------------------------------------------
// MenuEntry.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XnaGraphicsDemo
{
///
/// Base class for each entry in a MenuComponent.
///
class MenuEntry
{
// Constants.
public const int Height = 64;
public const int Border = 32;
// Properties.
///
/// Gets or sets the text displayed for this menu entry.
///
public virtual string Text { get; set; }
/// The text displayed for this menu entry.
///
/// Gets or sets the position of this menu entry on the screen.
///
public Vector2 Position { get; set; }
/// The position of this menu entry on the screen.
///
/// Gets or sets whether this menu entry is currently focused.
///
public bool IsFocused { get; set; }
/// True if the menu entry is focused; otherwise, false.
///
/// Gets or sets whether this menu entry can be dragged.
///
public bool IsDraggable { get; set; }
/// True if the menu entry can be dragged; otherwise, false.
///
/// Gets or sets the action to invoke when this menu entry is clicked.
///
public Action Clicked { get; set; }
/// The action to invoke when this menu entry is clicked.
///
/// Gets the color of the menu entry, blue if focused, white otherwise.
///
public Color Color { get { return IsFocused ? Color.Blue : Color.White; } }
/// The color of the menu entry.
Vector2 positionOffset;
///
/// Draws the menu entry.
///
/// The SpriteBatch used for drawing.
/// The font used to draw the text.
/// A blank texture for drawing backgrounds or highlights.
public virtual void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
{
positionOffset = new Vector2(0, (Height - font.LineSpacing) / 2);
spriteBatch.DrawString(font, Text, Position + positionOffset, Color);
}
///
/// Handles clicks on this menu entry, invoking the Clicked action and spawning feedback if not draggable.
///
public virtual void OnClicked()
{
// If we have a click delegate, call that now.
if (Clicked != null)
Clicked();
// If we are not draggable, spawn a visual feedback effect.
if (!IsDraggable)
DemoGame.SpawnZoomyText(Text, Position + positionOffset);
}
///
/// Handles dragging this menu entry from left to right.
///
/// The amount the pointer has moved since the last drag event.
public virtual void OnDragged(float delta)
{
}
}
///
/// Menu entry subclass for boolean toggle values.
///
class BoolMenuEntry : MenuEntry
{
// Properties.
public bool Value { get; set; }
public string Label { get; set; }
///
/// Initializes a new instance of the class with the specified label.
///
/// The label for the toggle entry.
public BoolMenuEntry(string label)
{
Label = label;
}
///
/// Click handler toggles the boolean value and invokes the base click logic.
///
public override void OnClicked()
{
Value = !Value;
base.OnClicked();
}
///
/// Gets the display text for the toggle entry, showing the label and current value.
///
public override string Text
{
get { return Label + " " + (Value ? "on" : "off"); }
set { }
}
}
///
/// Menu entry subclass for floating point slider values.
///
class FloatMenuEntry : MenuEntry
{
// Properties.
public float Value { get; set; }
///
/// Initializes a new instance of the class and marks it as draggable.
///
public FloatMenuEntry()
{
IsDraggable = true;
}
///
/// Drag handler changes the slider position.
///
/// The amount the pointer has moved since the last drag event.
public override void OnDragged(float delta)
{
const float speed = 1f / 300;
Value = MathHelper.Clamp(Value + delta * speed, 0, 1);
}
///
/// Custom draw function displays a slider bar in addition to the item text.
///
/// The SpriteBatch used for drawing.
/// The font used to draw the text.
/// A blank texture for drawing the slider bar.
public override void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
{
base.Draw(spriteBatch, font, blankTexture);
Vector2 size = font.MeasureString(Text);
size.Y /= 2;
Vector2 pos = Position + size;
pos.X += 8;
pos.Y += (Height - font.LineSpacing) / 2;
float w = 480 - Border - pos.X;
spriteBatch.Draw(blankTexture, new Rectangle((int)pos.X, (int)pos.Y - 3, (int)(w * Value), 6), Color);
}
}
}