/*
* GUIContainer.cs
* Authors: Bradley Blankenship, 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.Collections.Generic;
using Util;
using System.Collections;
namespace tAC_Engine.GUI
{
///
/// Defines a container GUI component
///
public abstract class GUIContainer : GUIComponent
{
#region Fields
///
/// List of all internal components contained within this container
///
List mComponents;
///
/// If Initialize has been called on this container
///
protected bool mInitialized = false;
///
/// If LoadContent has been called on this container
///
protected bool mLoaded = false;
#endregion
#region Properties
///
/// Allows access to the components of the Container
///
public List Components
{
get { return mComponents; }
}
///
/// Container specific visibility setting.
///
public override bool Visible
{
// Base call
get { return mIsVisible; }
set
{
// Base call
base.Visible = value;
// Set the visibility of all of our components
foreach (GUIComponent component in Components)
{
// Set them to our visibility
component.Visible = mIsVisible;
}
}
}
///
/// Container specific enabled setting.
///
public override bool Enabled
{
// Base call
get { return mIsEnabled; }
set
{
// Base call
base.Enabled = value;
// Set the visibility of all of our components
foreach (GUIComponent component in Components)
{
// Set them to our visibility
component.Enabled = mIsEnabled;
}
}
}
#endregion
#region Constructs
///
/// Base constructor.
///
public GUIContainer()
{
// Create the list for use
mComponents = new List();
}
#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()
{
// Register us with each component
foreach (GUIComponent component in mComponents)
{
component.Register(this);
component.Initialize();
}
mInitialized = true;
// Resize our components
Resize();
}
///
/// Reinitialize all internal components.
///
public override void Resize()
{
// Reinitialize all of our internal components
foreach (GUIComponent component in Components)
{
// Reinitialize it
component.Resize();
}
}
///
/// Reinitialize the values.
///
public override void Reinitialize()
{
// Reinitialize all of our internal components
foreach (GUIComponent component in Components)
{
// Reinitialize it
component.Reinitialize();
}
}
///
/// Load graphical content for the screen.
///
public override void LoadContent()
{
// Load components
foreach (GUIComponent c in mComponents)
{
c.Batch = mSpriteBatch;
c.LoadContent();
}
mLoaded = true;
// Base call
base.LoadContent();
}
#endregion
#region Management
///
/// Add the given component to us through registration.
///
/// The component to add.
public virtual void Add(GUIComponent component)
{
// Only if we don't have it
if (!Components.Contains(component))
{
// Add it to our components
Components.Add(component);
Components.Sort();
// Make it visible
component.Visible = true;
component.Enabled = true;
if (mInitialized)
{
component.Register(this);
component.Initialize();
}
if (mLoaded)
{
component.Batch = mSpriteBatch;
component.LoadContent();
}
}
}
///
/// Remove the given component.
///
/// The component to remove.
/// Whether it was removed or not.
public virtual bool Remove(GUIComponent component)
{
// Remove the component
return Components.Remove(component);
}
#endregion
#region Update
///
/// Update all internal components.
///
/// the current game time.
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
// If we're visible
if (Visible)
{
// Go through all components
foreach (GUIComponent component in Components)
{
// Update this one
component.Update(gameTime);
}
}
}
#endregion
#region Event Handling
///
/// Checks to see if a mouse click is inside of the component.
///
///
///
public override bool IsInside(Microsoft.Xna.Framework.Point pPoint)
{
// See if we're inside of the bounds of this component
bool retVal = base.IsInside(pPoint);
// If we're not inside of that, check our sub-components
if (!retVal)
{
// Go through each of our components
foreach (GUIComponent component in mComponents)
{
// See if we're inside of them
if (component.IsInside(pPoint))
{
// Then we're inside
retVal = true;
break;
}
}
}
// return
return retVal;
}
///
/// Returns the GUIComponent for the tool tip based on the container.
///
///
///
public override GUIComponent GetToolTipComponent(Microsoft.Xna.Framework.Point p)
{
// The retVal
GUIComponent retVal = base.GetToolTipComponent(p);
// If we're visible
if (Visible)
{
// Go through each of the components and see what happens
foreach (GUIComponent component in Components)
{
// Get the component if it has it
GUIComponent temp = component.GetToolTipComponent(p);
// If temp is not null
if (temp != null)
{
// Set the retval and break
retVal = temp;
break;
}
}
}
// Return the value
return retVal;
}
///
/// Handle the event by disinguishing between the types it has.
///
/// The event to handle.
public override bool HandleKeyEvent(KeyInputEvent e)
{
// The return value
bool retVal = false;
// If we're not invisible and we're enabled
if (Visible && Enabled)
{
// go through our components and see if they can handle it
foreach (GUIComponent component in Components)
{
// Try to handle it
if (component.HandleKeyEvent(e))
{
// Set the retVal 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;
}
// Break out
break;
}
}
}
// Return the value
return retVal;
}
///
/// Check if any components of this container can handle the mouse event
///
/// The event to handle.
/// True if any one component successfully handled the event
public override bool HandleMouseEvent(MouseInputEvent e)
{
// The return value
bool retVal = false;
// If we're visible and enabled
if (IsInside(e.Location))
{
//// Set the event args if they're null
//if (e.EventArgs == null && this.Enabled)
//{
// // Set the event args to us
// e.EventArgs = this;
//}
// go through our components and see if they can handle it
foreach (GUIComponent component in Components)
{
// Try to handle it
if (component.HandleMouseEvent(e))
{
// Set the retVal to true
retVal = true;
// Set the event args to the component
e.EventArgs = component;
// Break out
break;
}
}
}
// Return the value
return retVal;
}
#endregion
#region Draw
///
/// Draw all internal components.
///
/// The current game time.
public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
// If we're visible
if (Visible)
{
// go through all components
foreach (GUIComponent component in Components)
{
// Draw the component
component.Draw(gameTime);
}
}
}
#endregion
}
}