#region File Information
//-----------------------------------------------------------------------------
// Controls.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.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using DynamicMenu.Transitions;
#endregion
namespace DynamicMenu.Controls
{
///
/// The base class for all controls
///
public abstract class Control : IControl
{
#region Fields
private List activeTransitions = new List();
#endregion
#region Properties
///
/// The left position of this control relative from its parent
///
public int Left { get; set; }
///
/// The top position of this control relative from its parent
///
public int Top { get; set; }
///
/// The width of this control
///
public int Width { get; set; }
///
/// The height of this control
///
public int Height { get; set; }
///
/// The bottom position of this control relative from its parent
///
[ContentSerializerIgnore]
public int Bottom
{
get { return Top + Height; }
}
///
/// The right position of this control relative from its parent
///
[ContentSerializerIgnore]
public int Right
{
get { return Left + Width; }
}
///
/// The name of this control
///
[ContentSerializer(Optional = true)]
public string Name { get; set; }
///
/// The name of the background texture for this control
///
[ContentSerializer(Optional = true)]
public string BackTextureName { get; set; }
///
/// The Background texture for this control
///
[ContentSerializerIgnore]
public Texture2D BackTexture { get; set; }
///
/// Whether this control is currently visible and enabled
///
[ContentSerializer(Optional = true)]
public bool Visible { get; set; }
///
/// The hugh applied to this control
///
[ContentSerializer(Optional = true)]
public Color Hue { get; set; }
///
/// The parent of this control
///
[ContentSerializerIgnore]
public IControl Parent { get; set; }
///
/// This can be used to store information related to this control
///
[ContentSerializerIgnore]
public object Tag { get; set; }
#endregion
#region Initialization
///
/// Public constructor
///
public Control()
{
Visible = true;
Hue = Color.White;
}
///
/// Control initialization
///
virtual public void Initialize()
{
// Do nothing here
}
///
/// Loads the content for this control
///
virtual public void LoadContent(GraphicsDevice _graphics, ContentManager _content)
{
if (!string.IsNullOrEmpty(BackTextureName))
{
BackTexture = _content.Load(BackTextureName);
}
}
#endregion
#region Update
///
/// Updates this control, called once per game frame
///
/// The current game time
/// The list of recorded gestures for this frame
virtual public void Update(GameTime gameTime, List gestures)
{
List toRemove = new List();
List curTransitions = new List();
curTransitions.AddRange(activeTransitions);
// Update any applied transitions
foreach (Transition transition in curTransitions)
{
transition.Update(gameTime);
if (!transition.TransitionActive)
{
toRemove.Add(transition);
}
}
// Remove any completed transitions
foreach (Transition transition in toRemove)
{
activeTransitions.Remove(transition);
}
}
#endregion
#region Draw
///
/// Draws the control, called once per frame
///
/// The current game time
/// The sprite batch to draw with
virtual public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
Texture2D currTexture = GetCurrTexture();
if (currTexture != null)
{
Rectangle rect = GetAbsoluteRect();
spriteBatch.Draw(currTexture, rect, null, Hue, 0f, new Vector2(), SpriteEffects.None, 0f);
}
}
#endregion
#region Helper Functions
///
/// Overridable by decendants. This returns the current texture being used as the background image.
///
/// The current texture
virtual public Texture2D GetCurrTexture()
{
return BackTexture;
}
///
/// Gets the top left position of this control in screen coordinates.
///
/// The top left point
public Point GetAbsoluteTopLeft()
{
Point absoluteTopLeft = new Point(Left, Top);
if (Parent != null)
{
Point parentTopLeft = Parent.GetAbsoluteTopLeft();
absoluteTopLeft.X += parentTopLeft.X;
absoluteTopLeft.Y += parentTopLeft.Y;
}
return absoluteTopLeft;
}
///
/// Gets the boundaries for this control in screen coordinates.
///
/// The rect boundaries
public Rectangle GetAbsoluteRect()
{
Point topLeft = GetAbsoluteTopLeft();
return new Rectangle(topLeft.X, topLeft.Y, Width, Height);
}
///
/// Starts the passed in transition on this control.
///
/// The transition to apply
public void ApplyTransition(Transition transition)
{
activeTransitions.Add(transition);
transition.Control = this;
transition.StartTranstion();
}
///
/// Draws the specified text in the center of the control.
///
/// The sprite batch to draw with
/// The font to use for the text
/// The boundaries to center the text within
/// The text to draw
/// The hue to use for the text
protected void DrawCenteredText(SpriteBatch _spriteBatch, SpriteFont _font, Rectangle _rect, string _text, Color _color)
{
if (_font == null || string.IsNullOrEmpty(_text)) return;
// Center the text in the rect
Vector2 midPoint = new Vector2(_rect.X + _rect.Width / 2, _rect.Y + _rect.Height / 2);
Vector2 stringSize = _font.MeasureString(_text);
Vector2 fontPos = new Vector2(midPoint.X - stringSize.X * .5f, midPoint.Y - stringSize.Y * .5f);
_spriteBatch.DrawString(_font, _text, fontPos, _color);
}
///
/// Indicates whether the specified position falls within the control
///
/// The position to check
/// Whether the position is contained within the control
protected bool ContainsPos(Vector2 pos)
{
Rectangle rect = GetAbsoluteRect();
return rect.Contains((int)pos.X, (int)pos.Y);
}
#endregion
}
}