/*
* GUIPopUpPanel.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;
namespace tAC_Engine.GUI
{
///
/// Represents a Panel that pops up with a given effect.
///
public class GUIPopUpPanel : GUIPanel
{
#region Enums
///
/// The effects that can be performed on this panel.
///
public enum Effects
{
///
/// Event for zooming in
///
ZOOM,
///
/// Event for sliding in from the left to the right
///
SLIDE_RIGHT
}
#endregion
#region Fields
// The current time in the progress of completing the effect
float mCurrentTime = 0;
// The amount of time it takes to complete transitioning
int mTime = 1;
// The copy of the true bounds of the object
Rectangle mTrueBounds;
// Whether we are appearing or fading
bool mAppearOrFade = true;
// The effect for disappearing and fading
Effects mEffect = Effects.ZOOM;
#endregion
#region Properties
///
/// Allows access to the position of the component.
///
public override Vector2 Position
{
get { return mPosition; }
set
{
mPosition = value;
mTrueBounds = new Rectangle(
(int)Position.X, (int)Position.Y,
(int)Size.X, (int)Size.Y); ;
}
}
///
/// Allows access to the size of the component.
///
public override Vector2 Size
{
get { return mSize; }
set
{
mSize = value;
mTrueBounds = new Rectangle(
(int)Position.X, (int)Position.Y,
(int)Size.X, (int)Size.Y);
}
}
///
/// Sets or gets the amount of time, in milliseconds, for the effect
/// to complete.
///
public int Time
{
get { return mTime; }
set { mTime = value; }
}
///
/// Allows the user to view or set if it's appearing(true) or
/// fading(false).
///
public bool AppearOrFade
{
get { return mAppearOrFade; }
set { mAppearOrFade = value; }
}
///
/// Adjust the effects.
///
public Effects Effect
{
get { return mEffect; }
set { mEffect = value; }
}
#endregion
#region Constructs
///
/// Base constructor.
///
public GUIPopUpPanel()
{
// Set the initial bounds
mBounds = new Rectangle(0, 0, 0, 0);
}
#endregion
#region Behavior
///
/// Call this to zoom, based on the current times, etc.
///
private void Zoom()
{
// Set the current bounds based on the value
float ratio = (mCurrentTime / mTime);
int xAdj = (int)(mTrueBounds.Width * (1 - ratio) / 2);
int yAdj = (int)(mTrueBounds.Height * (1 - ratio) / 2);
mBounds = new Rectangle(
mTrueBounds.X + xAdj, mTrueBounds.Y + yAdj,
(int)(mTrueBounds.Width * ratio), (int)(mTrueBounds.Height * ratio));
}
///
/// Call this to slide right, based on the current times, etc.
///
private void SlideRight()
{
// Set the current bounds based on the value
float ratio = (mCurrentTime / mTime);
int x = (int)((mTrueBounds.Right) * (1 - ratio));
mBounds = new Rectangle(
mTrueBounds.Left - x, mTrueBounds.Y,
mTrueBounds.Width, mTrueBounds.Height);
}
#endregion
#region Update
///
/// Update the panel, altering the appearence based on the effect.
///
/// The current game time.
public override void Update(GameTime gameTime)
{
// Base call
base.Update(gameTime);
// Only if we're visible
if (Visible)
{
// Change based on our state
if (mAppearOrFade && mBounds != mTrueBounds)
{
// Adjust the time
mCurrentTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
else if(!mAppearOrFade)
{
// Adjust the time
mCurrentTime -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}
// If we're going either way
if ((mAppearOrFade && mCurrentTime < mTime) ||
(!mAppearOrFade && mCurrentTime > 0))
{
// Switch on the type of effect
switch (mEffect)
{
// Zooming
case Effects.ZOOM:
Zoom();
break;
// Slide right
case Effects.SLIDE_RIGHT:
SlideRight();
break;
}
}
// Fully appeared
else if (mAppearOrFade && mTrueBounds != mBounds)
{
// The value is the current bounds
mBounds = mTrueBounds;
Resize();
}
// Fully faded
else if (!mAppearOrFade)
{
// Set us to invisible
Visible = false;
}
// If necessary
if (mBounds != mTrueBounds)
{
// Resize the screen
Resize();
}
}
}
#endregion
}
}