/*
* InteractivePopUp.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 System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Util;
namespace tAC_Engine.Graphics.Entities
{
///
/// Defines a pop up object which can contain several different components
///
public class InteractivePopUp : PopupSprite
{
#region Fields
// List of all the components with in the pop up
List mComponents = new List();
#endregion
#region Properties
///
/// Gets/Sets the center of the pop up object
///
public override Vector2 Center
{
get { return base.Center; }
set
{
base.Center = value;
foreach (PopUpComponent puc in mComponents)
{
puc.Center = base.Position + puc.Offset;
}
}
}
///
/// Gets/Sets the current sprite batch used for rendering
///
public override Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch
{
get { return base.SpriteBatch; }
set
{
base.SpriteBatch = value;
foreach (PopUpComponent puc in mComponents)
{
puc.SpriteBatch = base.SpriteBatch;
}
}
}
#endregion
#region Creation
///
/// Constructor for the pop up
///
/// The current content manager used for loading in content objects
/// The path name of the texture
/// The center of the pop up
/// The length of time the pop up lasts for
public InteractivePopUp(ContentManager pContent, string pTextureName, Vector2 pCenter, float pTime)
: base(pContent, pTextureName, pCenter, pTime) { }
///
/// Adds a pop up component to the pop up
///
/// The pop up component to add to the pop up
public void AddComponent(PopUpComponent pIcon)
{
mComponents.Add(pIcon);
}
#endregion
#region Initialization
///
/// Initializes any needed components
///
public override void Initialize()
{
base.Initialize();
foreach (PopUpComponent puc in mComponents)
{
puc.Initialize();
}
}
#endregion
#region Render
///
/// Renders the pop up
///
/// time passed in game
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
if (Visible && !GettingSmaller && !GettingLarger)
{
foreach (PopUpComponent puc in mComponents)
{
if(puc.Visible)
puc.Draw(gameTime);
}
}
}
#endregion
#region Bounds Checking
///
/// Checks if the position is within the pop up's area
///
/// Position to check
/// Whether or not the position is within the pop up's bounds
protected bool WithinBounds(Vector2 mPosition)
{
if (mPosition.X < this.X || mPosition.X > this.X + this.Width
|| mPosition.Y < this.Y || mPosition.Y > this.Y + this.Height)
return false;
else
return true;
}
///
/// Checks which, if any, of the components on the pop up has been "clicked"
///
/// The position in which the pop up has been "clicked" at
/// If any pop up component is "clicked", the component that is "clicked". Otherwise null
public virtual PopUpComponent Click(Vector2 mPosition)
{
if (WithinBounds(mPosition))
{
foreach (PopUpComponent puc in mComponents)
{
if (puc.WithinBounds(mPosition))
return puc;
}
}
return null;
}
#endregion
}
///
/// Defines a component that is part of the pop up object
///
public class PopUpComponent : EntitySprite
{
#region Fields
// Position offset of the pop up component from the center of the pop up
private Vector2 mOffset = Vector2.Zero;
///
/// The center of the pop up component
///
protected Vector2 mCenter = Vector2.Zero;
#endregion
#region Properties
///
/// Gets/Sets the position offset of the pop up component from the center of the pop up
///
public Vector2 Offset
{
get { return mOffset; }
set { mOffset = value; }
}
///
/// Gets/Sets the center of the pop up component
///
public virtual Vector2 Center
{
get { return mCenter; }
set
{
mCenter = value;
Recenter(mCenter);
}
}
#endregion
#region Creation
///
/// Constructor for a pop up component
///
/// Reference to the current content manager
/// Path name to the texture to use
/// Position offset from the center of the pop up object
public PopUpComponent(ContentManager pContent, string pTextureName, Vector2 pOffset)
: base(pContent, pTextureName)
{
mOffset = pOffset;
}
#endregion
#region Initialization
///
/// Initializes all need components upon creation
///
public override void Initialize()
{
int tempHeight = Height;
int tempWidth = Width;
base.Initialize();
Height = tempHeight;
Width = tempWidth;
}
#endregion
#region Checks
///
/// Checks to see if a given 2 dimensional position is with the bounds of the pop up component
///
/// The 2 dimensional position to check
/// Whether or not the given 2 dimensional position is within the bounds of the pop up component
public bool WithinBounds(Vector2 mPosition)
{
if (mPosition.X < this.X || mPosition.X > this.X + this.Width
|| mPosition.Y < this.Y || mPosition.Y > this.Y + this.Height)
return false;
else
return true;
}
#endregion
#region Modifiers
///
/// Recenters the pop up component to the given point
///
/// The 2 dimensional point to center to
protected void Recenter(Vector2 pCenter)
{
this.Position = new Vector2(pCenter.X - this.Width / 2, pCenter.Y - this.Height / 2);
}
#endregion
}
///
/// Defines a pop up string for displaying text in a pop up object
///
public class PopUpString : DrawableGameScreenComponent
{
#region Fields
// Reference to the current sprite batch used for rendering
private SpriteBatch mSpriteBatch;
// Reference to the current sprite font used for rendering the string
private SpriteFont mSpriteFont;
// The color that the text will be displayed in
private Color mColor;
// The position of the pop up string
private Vector2 mPosition = Vector2.Zero;
// The text that is to be displayed
private string mString;
#endregion
#region Properties
///
/// Gets/Sets the reference to the current sprite batch used for rendering
///
public SpriteBatch SpriteBatch
{
get { return mSpriteBatch; }
set { mSpriteBatch = value; }
}
///
/// Gets/Sets the reference to the current sprite font used for rendering the string
///
public SpriteFont SpriteFont
{
get { return mSpriteFont; }
set { mSpriteFont = value; }
}
///
/// Gets/Sets the color that the text will be displayed in
///
public Color Color
{
get { return mColor; }
set { mColor = value; }
}
///
/// Gets/Sets the position of the pop up string
///
public Vector2 Position
{
get { return mPosition; }
set { mPosition = value; }
}
///
/// Gets/Sets the text that is to be displayed
///
public string String
{
get { return mString; }
set { mString = value; }
}
#endregion
#region Creation
///
/// Constructor for the pop up string
///
/// The text that is to be displayed
/// The position of the pop up string
/// The color that the text will be displayed in
/// Reference to the current sprite font used for rendering the string
/// Reference to the current sprite batch used for rendering
public PopUpString(string pString, Vector2 pPosition, Color pColor, SpriteBatch pSpriteBatch, SpriteFont pSpriteFont)
{
mSpriteBatch = pSpriteBatch;
mString = pString;
mSpriteFont = pSpriteFont;
mColor = pColor;
Position = pPosition;
}
#endregion
#region Render
///
/// Renders the pop up string
///
/// Time that has passed in game
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
mSpriteBatch.Begin();
mSpriteBatch.DrawString(mSpriteFont, mString, mPosition, mColor);
mSpriteBatch.End();
}
#endregion
}
}