/*
* GUITextInput.cs
* Authors: Mike DeMauro
* 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 System;
using System.Collections.Generic;
using System.Text;
using Util;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace tAC_Engine.GUI
{
///
/// Defines a text input GUI object
///
public class GUITextInput : GUILabel
{
#region Fields
///
/// Whether or not this text input GUi currently has focus
///
protected bool mHasFocus = false;
///
/// A single white pixel image
///
protected Texture2D whitePixel;
///
/// Color for the background behind the text
///
protected Color backgroundColor = Color.White;
///
/// Color used for the border around the text
///
protected Color borderColor = Color.Black;
///
/// Color used for when text is highlighted
///
protected Color highlightColor = Color.Yellow;
#endregion
#region Construct
///
/// Constructor
///
public GUITextInput()
: base()
{
mTextOffset = new Vector2(4, 3);
}
#endregion
#region Management
///
/// Loads in all the necessary data for content dependent objects
///
public override void LoadContent()
{
// create pixel
whitePixel = new Texture2D(ParentScreen.GraphicsDevice, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
Color[] pixels = new Color[1];
pixels[0] = Color.White;
whitePixel.SetData(pixels);
base.LoadContent();
}
#endregion
#region Event Handling
///
/// The event that is thrown when the text changes due to keyboard input
///
public event EventHandler TextTyped;
#endregion
#region Render
///
/// Performs rendering of the Gui text input
/// TODO: optimize Vector2s
///
/// How much time has passed in game
public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
base.Draw(gameTime);
// Draw (inset) borders
// -top
Batch.Draw(whitePixel, Position - mScreenOffset, null, borderColor, 0f, Vector2.Zero, new Vector2(Size.X, 1f), SpriteEffects.None, mLayerDepth);
// -right
Batch.Draw(whitePixel, new Vector2(Position.X + Size.X - 1, Position.Y) - mScreenOffset, null, borderColor, 0f, Vector2.Zero, new Vector2(1f, Size.Y), SpriteEffects.None, mLayerDepth);
// -bottom
Batch.Draw(whitePixel, new Vector2(Position.X, Position.Y + Size.Y - 1) - mScreenOffset, null, borderColor, 0f, Vector2.Zero, new Vector2(Size.X, 1f), SpriteEffects.None, mLayerDepth);
// -left
Batch.Draw(whitePixel, Position - mScreenOffset, null, borderColor, 0f, Vector2.Zero, new Vector2(1f, Size.Y), SpriteEffects.None, mLayerDepth);
// Draw (double inset) highlight
if (mHasFocus)
{
// -top
Batch.Draw(whitePixel, new Vector2(Position.X + 1, Position.Y + 1) - mScreenOffset, null, highlightColor, 0f, Vector2.Zero, new Vector2(Size.X - 2, 1f), SpriteEffects.None, mLayerDepth);
// -right
Batch.Draw(whitePixel, new Vector2(Position.X + Size.X - 2, Position.Y + 1) - mScreenOffset, null, highlightColor, 0f, Vector2.Zero, new Vector2(1f, Size.Y - 2), SpriteEffects.None, mLayerDepth);
// -bottom
Batch.Draw(whitePixel, new Vector2(Position.X + 1, Position.Y + Size.Y - 2) - mScreenOffset, null, highlightColor, 0f, Vector2.Zero, new Vector2(Size.X - 2, 1f), SpriteEffects.None, mLayerDepth);
// -left
Batch.Draw(whitePixel, new Vector2(Position.X + 1, Position.Y + 1) - mScreenOffset, null, highlightColor, 0f, Vector2.Zero, new Vector2(1f, Size.Y - 2), SpriteEffects.None, mLayerDepth);
}
}
#endregion
#region Input Handle
///
/// Handles processing of specific key pressed events
///
/// The key input event that has just occured
/// Whether one of the specified keys was pressed or not
public override bool HandleKeyEvent(KeyInputEvent e)
{
bool retVal = false;
if (mHasFocus && e != null && e.Trigger == Trigger.Activated)
{
if (e.Key == Keys.Back)
{
Text = Text.Substring(0, Math.Max(Text.Length - 1, 0));
}
else if (e.Key == Keys.Enter)
{
Text += "\n";
}
else if (e.Key == Keys.Escape)
{
mHasFocus = false;
}
else
{
Text += e.ToAlphanumericString();
}
// Set the event args if they're null
if (e.EventArgs == null)
{
// Set the event args to us
e.EventArgs = this;
}
// Fire the event handler
if (TextTyped != null) TextTyped(this, EventArgs.Empty);
retVal = true;
}
return retVal;
}
///
/// Handles processing of the mouse events
///
/// The mouse input event that has just occured
/// Whether or not the mouse event needed to be handled
public override bool HandleMouseEvent(MouseInputEvent e)
{
bool retVal = base.HandleMouseEvent(e);
mHasFocus = retVal;
if (retVal)
{
// Set the event args if they're null
if (e.EventArgs == null)
{
// Set the event args to us
e.EventArgs = this;
}
}
return retVal;
}
#endregion
}
}