#region File Description //----------------------------------------------------------------------------- // MenuEntry.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements 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; #endregion namespace XnaGraphicsDemo { /// /// Base class for each entry in a MenuComponent. /// class MenuEntry { // Constants. public const int Height = 64; public const int Border = 32; // Properties. public virtual string Text { get; set; } public Vector2 Position { get; set; } public bool IsFocused { get; set; } public bool IsDraggable { get; set; } public Action Clicked { get; set; } public Color Color { get { return IsFocused ? Color.Blue : Color.White; } } Vector2 positionOffset; /// /// Draws the menu entry. /// 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. /// 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. /// 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; } /// /// Constructor. /// public BoolMenuEntry(string label) { Label = label; } /// /// Click handler toggles the boolean value. /// public override void OnClicked() { Value = !Value; base.OnClicked(); } /// /// Customize our text string. /// 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; } /// /// Constructor. /// public FloatMenuEntry() { IsDraggable = true; } /// /// Drag handler changes the slider position. /// 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. /// 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); } } }