snes9xgx/source/gui/gui_element.cpp

533 lines
8.3 KiB
C++
Raw Normal View History

2009-03-11 18:28:37 +01:00
/****************************************************************************
2009-04-10 10:12:37 +02:00
* libwiigui
2009-03-11 18:28:37 +01:00
*
2009-04-10 10:12:37 +02:00
* Tantric 2009
2009-03-11 18:28:37 +01:00
*
* gui_element.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
/**
* Constructor for the Object class.
*/
GuiElement::GuiElement()
{
xoffset = 0;
yoffset = 0;
2009-04-10 10:12:37 +02:00
xmin = 0;
xmax = 0;
ymin = 0;
ymax = 0;
2009-03-11 18:28:37 +01:00
width = 0;
height = 0;
alpha = 255;
scale = 1;
state = STATE_DEFAULT;
2009-04-10 10:12:37 +02:00
stateChan = -1;
2009-03-11 18:28:37 +01:00
trigger[0] = NULL;
trigger[1] = NULL;
parentElement = NULL;
2009-03-16 06:16:20 +01:00
rumble = true;
2009-03-11 18:28:37 +01:00
selectable = false;
clickable = false;
holdable = false;
2009-03-11 18:28:37 +01:00
visible = true;
focus = -1; // cannot be focused
updateCB = NULL;
yoffsetDyn = 0;
xoffsetDyn = 0;
alphaDyn = -1;
scaleDyn = 1;
effects = 0;
effectAmount = 0;
effectTarget = 0;
effectsOver = 0;
effectAmountOver = 0;
effectTargetOver = 0;
// default alignment - align to top left
alignmentVert = ALIGN_TOP;
alignmentHor = ALIGN_LEFT;
}
/**
* Destructor for the GuiElement class.
*/
GuiElement::~GuiElement()
{
}
void GuiElement::SetParent(GuiElement * e)
{
parentElement = e;
}
GuiElement * GuiElement::GetParent()
{
return parentElement;
}
2009-10-17 02:15:58 +02:00
2009-03-11 18:28:37 +01:00
int GuiElement::GetLeft()
{
int x = 0;
int pWidth = 0;
int pLeft = 0;
if(parentElement)
{
pWidth = parentElement->GetWidth();
pLeft = parentElement->GetLeft();
}
if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
pLeft += xoffsetDyn;
switch(alignmentHor)
{
case ALIGN_LEFT:
x = pLeft;
break;
case ALIGN_CENTRE:
2010-01-25 08:34:45 +01:00
x = pLeft + (pWidth>>1) - (width>>1);
2009-03-11 18:28:37 +01:00
break;
case ALIGN_RIGHT:
x = pLeft + pWidth - width;
break;
}
return x + xoffset;
}
int GuiElement::GetTop()
{
int y = 0;
int pHeight = 0;
int pTop = 0;
if(parentElement)
{
pHeight = parentElement->GetHeight();
pTop = parentElement->GetTop();
}
if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
pTop += yoffsetDyn;
switch(alignmentVert)
{
case ALIGN_TOP:
y = pTop;
break;
case ALIGN_MIDDLE:
2010-01-25 08:34:45 +01:00
y = pTop + (pHeight>>1) - (height>>1);
2009-03-11 18:28:37 +01:00
break;
case ALIGN_BOTTOM:
y = pTop + pHeight - height;
break;
}
return y + yoffset;
}
2009-04-10 10:12:37 +02:00
void GuiElement::SetMinX(int x)
{
xmin = x;
}
int GuiElement::GetMinX()
{
return xmin;
}
void GuiElement::SetMaxX(int x)
{
xmax = x;
}
int GuiElement::GetMaxX()
{
return xmax;
}
void GuiElement::SetMinY(int y)
{
ymin = y;
}
int GuiElement::GetMinY()
{
return ymin;
}
void GuiElement::SetMaxY(int y)
{
ymax = y;
}
int GuiElement::GetMaxY()
{
return ymax;
}
2009-03-11 18:28:37 +01:00
int GuiElement::GetWidth()
{
return width;
}
int GuiElement::GetHeight()
{
return height;
}
void GuiElement::SetSize(int w, int h)
{
width = w;
height = h;
}
bool GuiElement::IsVisible()
{
return visible;
}
void GuiElement::SetVisible(bool v)
{
visible = v;
}
void GuiElement::SetAlpha(int a)
{
alpha = a;
}
int GuiElement::GetAlpha()
{
2010-01-25 08:34:45 +01:00
int a = alpha;
2009-03-11 18:28:37 +01:00
if(alphaDyn >= 0)
a = alphaDyn;
if(parentElement)
2010-01-25 08:34:45 +01:00
a *= float(parentElement->GetAlpha())/255.0f;
2009-03-11 18:28:37 +01:00
return a;
}
void GuiElement::SetScale(float s)
{
scale = s;
}
float GuiElement::GetScale()
{
float s = scale * scaleDyn;
if(parentElement)
s *= parentElement->GetScale();
return s;
}
int GuiElement::GetState()
{
return state;
}
int GuiElement::GetStateChan()
{
return stateChan;
}
void GuiElement::SetState(int s, int c)
2009-03-11 18:28:37 +01:00
{
state = s;
stateChan = c;
2009-03-11 18:28:37 +01:00
}
void GuiElement::ResetState()
{
if(state != STATE_DISABLED)
{
2009-03-11 18:28:37 +01:00
state = STATE_DEFAULT;
stateChan = -1;
}
2009-03-11 18:28:37 +01:00
}
void GuiElement::SetClickable(bool c)
{
clickable = c;
}
void GuiElement::SetSelectable(bool s)
{
selectable = s;
}
void GuiElement::SetHoldable(bool d)
2009-04-10 10:12:37 +02:00
{
holdable = d;
2009-04-10 10:12:37 +02:00
}
2009-03-11 18:28:37 +01:00
bool GuiElement::IsSelectable()
{
if(state == STATE_DISABLED || state == STATE_CLICKED)
return false;
else
return selectable;
}
bool GuiElement::IsClickable()
{
2009-04-10 10:12:37 +02:00
if(state == STATE_DISABLED ||
state == STATE_CLICKED ||
state == STATE_HELD)
2009-03-11 18:28:37 +01:00
return false;
else
return clickable;
}
bool GuiElement::IsHoldable()
2009-04-10 10:12:37 +02:00
{
if(state == STATE_DISABLED)
return false;
else
return holdable;
2009-04-10 10:12:37 +02:00
}
2009-03-11 18:28:37 +01:00
void GuiElement::SetFocus(int f)
{
focus = f;
}
int GuiElement::IsFocused()
{
return focus;
}
void GuiElement::SetTrigger(GuiTrigger * t)
{
if(!trigger[0])
trigger[0] = t;
else if(!trigger[1])
trigger[1] = t;
else // both were assigned, so we'll just overwrite the first one
trigger[0] = t;
}
void GuiElement::SetTrigger(u8 i, GuiTrigger * t)
{
trigger[i] = t;
}
2009-03-16 06:16:20 +01:00
bool GuiElement::Rumble()
{
return rumble;
}
void GuiElement::SetRumble(bool r)
{
rumble = r;
}
2009-03-11 18:28:37 +01:00
int GuiElement::GetEffect()
{
return effects;
}
void GuiElement::SetEffect(int eff, int amount, int target)
{
if(eff & EFFECT_SLIDE_IN)
{
// these calculations overcompensate a little
if(eff & EFFECT_SLIDE_TOP)
yoffsetDyn = -screenheight;
else if(eff & EFFECT_SLIDE_LEFT)
xoffsetDyn = -screenwidth;
else if(eff & EFFECT_SLIDE_BOTTOM)
yoffsetDyn = screenheight;
else if(eff & EFFECT_SLIDE_RIGHT)
xoffsetDyn = screenwidth;
}
2010-01-25 08:34:45 +01:00
if(eff & EFFECT_FADE)
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
if(amount > 0)
alphaDyn = 0;
else if(amount < 0)
alphaDyn = alpha;
2009-03-11 18:28:37 +01:00
}
effects |= eff;
effectAmount = amount;
effectTarget = target;
}
void GuiElement::SetEffectOnOver(int eff, int amount, int target)
{
effectsOver |= eff;
effectAmountOver = amount;
effectTargetOver = target;
}
void GuiElement::SetEffectGrow()
{
SetEffectOnOver(EFFECT_SCALE, 4, 110);
}
void GuiElement::UpdateEffects()
{
if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
{
if(effects & EFFECT_SLIDE_IN)
{
if(effects & EFFECT_SLIDE_LEFT)
{
xoffsetDyn += effectAmount;
if(xoffsetDyn >= 0)
{
xoffsetDyn = 0;
effects = 0;
}
}
else if(effects & EFFECT_SLIDE_RIGHT)
{
xoffsetDyn -= effectAmount;
if(xoffsetDyn <= 0)
{
xoffsetDyn = 0;
effects = 0;
}
}
else if(effects & EFFECT_SLIDE_TOP)
{
yoffsetDyn += effectAmount;
if(yoffsetDyn >= 0)
{
yoffsetDyn = 0;
effects = 0;
}
}
else if(effects & EFFECT_SLIDE_BOTTOM)
{
yoffsetDyn -= effectAmount;
if(yoffsetDyn <= 0)
{
yoffsetDyn = 0;
effects = 0;
}
}
}
else
{
if(effects & EFFECT_SLIDE_LEFT)
{
xoffsetDyn -= effectAmount;
if(xoffsetDyn <= -screenwidth)
effects = 0; // shut off effect
}
else if(effects & EFFECT_SLIDE_RIGHT)
{
xoffsetDyn += effectAmount;
if(xoffsetDyn >= screenwidth)
effects = 0; // shut off effect
}
else if(effects & EFFECT_SLIDE_TOP)
{
yoffsetDyn -= effectAmount;
if(yoffsetDyn <= -screenheight)
effects = 0; // shut off effect
}
else if(effects & EFFECT_SLIDE_BOTTOM)
{
yoffsetDyn += effectAmount;
if(yoffsetDyn >= screenheight)
effects = 0; // shut off effect
}
}
}
if(effects & EFFECT_FADE)
{
alphaDyn += effectAmount;
if(effectAmount < 0 && alphaDyn <= 0)
{
alphaDyn = 0;
effects = 0; // shut off effect
}
else if(effectAmount > 0 && alphaDyn >= alpha)
{
alphaDyn = alpha;
effects = 0; // shut off effect
}
}
if(effects & EFFECT_SCALE)
{
2010-01-25 08:34:45 +01:00
scaleDyn += f32(effectAmount)*0.01f;
f32 effTar100 = f32(effectTarget)*0.01f;
2009-03-11 18:28:37 +01:00
2010-01-25 08:34:45 +01:00
if((effectAmount < 0 && scaleDyn <= effTar100)
|| (effectAmount > 0 && scaleDyn >= effTar100))
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
scaleDyn = effTar100;
2009-03-11 18:28:37 +01:00
effects = 0; // shut off effect
}
}
}
void GuiElement::Update(GuiTrigger * t)
{
if(updateCB)
updateCB(this);
}
void GuiElement::SetUpdateCallback(UpdateCallback u)
{
updateCB = u;
}
void GuiElement::SetPosition(int xoff, int yoff)
{
xoffset = xoff;
yoffset = yoff;
}
void GuiElement::SetAlignment(int hor, int vert)
{
alignmentHor = hor;
alignmentVert = vert;
}
int GuiElement::GetSelected()
{
return -1;
}
2010-01-25 09:24:11 +01:00
void GuiElement::ResetText()
{
}
2009-03-11 18:28:37 +01:00
void GuiElement::Draw()
{
}
2010-01-25 09:24:11 +01:00
void GuiElement::DrawTooltip()
{
}
2009-03-11 18:28:37 +01:00
bool GuiElement::IsInside(int x, int y)
{
2010-01-25 08:34:45 +01:00
if(unsigned(x - this->GetLeft()) < unsigned(width)
&& unsigned(y - this->GetTop()) < unsigned(height))
2009-03-11 18:28:37 +01:00
return true;
return false;
}