/*
* GUILabel.cs
* Authors:
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Util;
using System.Text;
namespace tAC_Engine.GUI
{
///
/// Represents a label for the GUI.
///
public class GUILabel : GUIComponent
{
#region Fields
///
/// The background image of the label
///
protected Texture2D mBackground;
///
/// The color of the label's background
///
protected Color mBackgroundColor = Color.White;
///
/// The sprite font
///
protected SpriteFont mFont;
///
/// The text of the label
///
protected string mText;
///
/// The text displayed by the label
///
protected string mDisplayedText;
///
/// The color for the text
///
protected Color mTextColor = Color.Black;
///
/// If the Label displays multiple lines of text or not.
///
protected bool mMultiline = false;
///
/// If the label is selected
///
protected bool mSelected = false;
///
/// Color of the background when mSelected is true
///
protected Color mSelectedColor = Color.Yellow;
#endregion
#region Properties
///
/// Allows access to the size of the component.
///
public override Microsoft.Xna.Framework.Vector2 Size
{
get { return base.Size; }
set { base.Size = value; updateDisplayedText(); }
}
///
/// Allows access to the background image of the label.
///
public Texture2D Background
{
get { return mBackground; }
set { mBackground = value; }
}
///
/// The color of the label's background
///
public Color BackgroundColor
{
get { return mBackgroundColor; }
set { mBackgroundColor = value; }
}
///
/// Allows access to the basic font values.
///
public SpriteFont Font
{
get { return mFont; }
set { mFont = value; updateDisplayedText(); }
}
///
/// If the Label displays multiple lines of text
///
public bool Multiline
{
get { return mMultiline; }
set { mMultiline = value; updateDisplayedText(); }
}
///
/// If the label is selected
///
public bool Selected
{
get { return mSelected; }
set { mSelected = value; }
}
///
/// Color of the background when Selected is true
///
public Color SelectedColor
{
get { return mSelectedColor; }
set { mSelectedColor = value; }
}
///
/// Allows access to the text of the label.
///
public string Text
{
get { return mText; }
set
{
mText = value;
updateDisplayedText();
}
}
///
/// Allows access to the color of the text.
///
public Color TextColor
{
get { return mTextColor; }
set { mTextColor = value; }
}
#endregion
#region Initialization
///
/// Does nothing
///
public override void Initialize() { }
#endregion
#region Management
///
/// Loads the necessary information for all ccontent dependent objects
///
public override void LoadContent()
{
// If we have a parent container
if (ParentScreen != null)
{
// Get the basic background image if we do not have one
if (Background == null)
{
// Load the basic background texture for it
Background = ParentScreen.Content.Load(@"Textures\BasicGUIBackground");
}
// Get the basic font if we do not have one
if (Font == null)
{
// Load the standard font
Font = ParentScreen.Content.Load(@"Fonts\Courier New");
}
}
base.LoadContent();
}
#endregion
#region Update
///
/// Update the label... AKA Do nothing!
///
/// The current game time.
public override void Update(Microsoft.Xna.Framework.GameTime gameTime) { }
///
/// Updates the displayed text
///
protected void updateDisplayedText()
{
if (mText == null || mText.Length <= 0 || mFont == null)
{
mDisplayedText = string.Empty;
}
else
{
if (mMultiline)
{
string[] brokenString = TextUtility.BreakString(mText, mSize.X, mFont);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < brokenString.Length; ++i)
{
sb.Append(brokenString[i]);
if (i != brokenString.Length - 1)
sb.Append("\n");
}
mDisplayedText = sb.ToString();
}
else
{
mDisplayedText = Text;
}
}
}
#endregion
#region Event Handling
///
/// Handle the given key input event.
///
/// The key input event to handle.
public override bool HandleKeyEvent(KeyInputEvent e)
{
return false;
}
///
/// Handle any events we received by passing them to our action.
///
/// The event to check for.
public override bool HandleMouseEvent(MouseInputEvent e)
{
// The return value
bool retVal = false;
// If we're visible
if (Visible && Enabled)
{
// If we contain the press
if (Bounds.Contains(e.X, e.Y))
{
// Set to true
retVal = true;
}
}
// Return the value
return retVal;
}
#endregion
#region Draw
///
/// Draw the label.
///
/// The current game time.
public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
// If we're visible
if (Visible)
{
// If we have a background
if (Background != null)
{
// Draw it!
if (mSelected)
{
Batch.Draw(Background, Bounds, null, mSelectedColor, mRotation, Vector2.Zero, mSpriteEffects, mLayerDepth);
}
else
{
Batch.Draw(Background, Bounds, null, mBackgroundColor, mRotation, Vector2.Zero, mSpriteEffects, mLayerDepth);
}
}
// If we have text and a font
if (Font != null && Text != null)
{
// Draw the text
Batch.DrawString(Font, mDisplayedText, Position - mScreenOffset + mTextOffset, TextColor, mRotation, Vector2.Zero, 1f, mSpriteEffects, mLayerDepth);
}
}
}
#endregion
}
}