/*
* GUIImage.cs
* Authors: August Zinsser
* Copyright (c) 2007-2009 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;
namespace tAC_Engine.GUI
{
///
/// Defines the base attributes of all GUI components
///
public class GUIImage : GUIComponent
{
#region Fields
///
/// The texture this image renders
///
protected Texture2D mTexture;
///
/// The path to the texture
///
private string mTextureName;
#endregion
#region Properties
///
/// Allows access to the texture of the component.
///
public Texture2D Texture
{
get { return mTexture; }
}
///
/// Set the path to the texture to load (call LoadContent to actually load it)
///
public string TextureName
{
set { mTextureName = value; }
}
#endregion
#region Rescale
///
/// Adjusts the Size of this component based on the size of the texture
///
///
public void Rescale(float uniformScale)
{
Rescale(uniformScale, uniformScale);
}
///
/// Adjusts the Size of this component based on the size of the texture
///
///
///
public void Rescale(float xScale, float yScale)
{
System.Diagnostics.Debug.Assert(mTexture != null, "Must have a texture before calling Rescale");
Size = new Vector2(mTexture.Width * xScale, mTexture.Height * yScale);
}
#endregion
#region Constructs
///
/// Create the intial values
///
public GUIImage() { }
#endregion
#region Initialization
///
/// Initializes the component. Override this method to load any non-graphics
/// resources and query for any required services.
///
public override void Initialize() { }
///
/// Called when graphics resources need to be loaded.
///
public override void LoadContent()
{
mTexture = mParentScreen.Content.Load(mTextureName);
}
#endregion
#region Event Handling
///
/// Event for when the mouse button is released
///
public event EventHandler MouseClicked;
///
/// Handle the given event.
///
/// The event to handle.
public override bool HandleMouseEvent(MouseInputEvent e)
{
// The return value
bool retVal = false;
// If we're visible
if (Visible && Enabled)
{
// Check to see if it's within our bounds
if (Bounds.Contains(e.X, e.Y))
{
// Only pay attention to left mouse clicks
if (e.EventCode == (int)GUIEventCodes.LEFT_MOUSE_CLICKED)
{
// Invoke Event if we're enabled
if (MouseClicked != null)
MouseClicked.Invoke(this, EventArgs.Empty);
// Set the ret val to true
retVal = true;
}
// Set the event args if they're null
if (e.EventArgs == null)
{
// Set the event args to us
e.EventArgs = this;
}
}
}
// Return
return retVal;
}
///
/// Handle the given key input event.
///
/// The key input event to handle.
public override bool HandleKeyEvent(KeyInputEvent e)
{
return false;
}
#endregion
#region Update
///
/// Update the component based on its needs.
///
/// The current game time.
public override void Update(GameTime gameTime) { }
#endregion
#region Draw
///
/// Draw the component to the screen.
///
/// The current game time.
public override void Draw(GameTime gameTime)
{
// Do this if we're visible
if (Visible)
{
// If we have a loaded texture, draw it
if (mTexture != null)
{
Batch.Draw(mTexture, Bounds, null, mAddColor, mRotation, Vector2.Zero, mSpriteEffects, mLayerDepth);
}
}
}
#endregion
}
}