usbloadergx/source/libwiigui/gui_element.cpp

821 lines
18 KiB
C++
Raw Normal View History

/****************************************************************************
* libwiigui
*
* Tantric 2009
*
* gui_element.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
/**
* Constructor for the Object class.
*/
//mutex_t GuiElement::mutex = LWP_MUTEX_NULL;
mutex_t GuiElement::_lock_mutex = LWP_MUTEX_NULL;
GuiElement::GuiElement()
{
xoffset = 0;
yoffset = 0;
zoffset = 0;
xmin = 0;
xmax = 0;
ymin = 0;
ymax = 0;
width = 0;
height = 0;
alpha = 255;
scale = 1;
state = STATE_DEFAULT;
stateChan = -1;
trigger[0] = NULL;
trigger[1] = NULL;
trigger[2] = NULL;
trigger[3] = NULL;
trigger[4] = NULL;
trigger[5] = NULL;
parentElement = NULL;
rumble = true;
selectable = false;
clickable = false;
holdable = false;
visible = true;
focus = -1; // cannot be focused
updateCB = NULL;
yoffsetDyn = 0;
xoffsetDyn = 0;
yoffsetDynFloat = 0;
alphaDyn = -1;
scaleDyn = 1;
effects = 0;
effectAmount = 0;
effectTarget = 0;
effectsOver = 0;
effectAmountOver = 0;
effectTargetOver = 0;
frequency = 0.0f;
changervar = 0;
degree = -90.0f;
circleamount = 360.0f;
Radius = 150;
angleDyn = 0.0f;
anglespeed = 0.0f;
// default alignment - align to top left
alignmentVert = ALIGN_TOP;
alignmentHor = ALIGN_LEFT;
2010-09-24 02:48:03 +02:00
// if(mutex == LWP_MUTEX_NULL) LWP_MutexInit(&mutex, true);
if (_lock_mutex == LWP_MUTEX_NULL) LWP_MutexInit(&_lock_mutex, true);
_lock_thread = LWP_THREAD_NULL;
_lock_count = 0;
_lock_queue = LWP_TQUEUE_NULL;
}
/**
* Destructor for the GuiElement class.
*/
GuiElement::~GuiElement()
{
2010-09-24 02:48:03 +02:00
// LWP_MutexDestroy(mutex);
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetParent(GuiElement * e)
{
LOCK( this );
parentElement = e;
}
GuiElement * GuiElement::GetParent()
{
return parentElement;
}
/**
* Get the left position of the GuiElement.
* @see SetLeft()
* @return Left position in pixel.
*/
int GuiElement::GetLeft()
{
int x = 0;
int pWidth = 0;
int pLeft = 0;
2010-09-24 02:48:03 +02:00
if (parentElement)
{
pWidth = parentElement->GetWidth();
pLeft = parentElement->GetLeft();
}
2010-09-24 02:48:03 +02:00
if (effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT | EFFECT_GOROUND | EFFECT_ROCK_VERTICLE)) pLeft += xoffsetDyn;
2010-09-24 02:48:03 +02:00
switch (alignmentHor)
{
case ALIGN_LEFT:
x = pLeft;
break;
case ALIGN_CENTRE:
2010-09-24 02:48:03 +02:00
x = pLeft + (pWidth / 2) - (width / 2);
break;
case ALIGN_RIGHT:
x = pLeft + pWidth - width;
break;
}
return x + xoffset;
}
/**
* Get the top position of the GuiElement.
* @see SetTop()
* @return Top position in pixel.
*/
int GuiElement::GetTop()
{
int y = 0;
int pHeight = 0;
int pTop = 0;
2010-09-24 02:48:03 +02:00
if (parentElement)
{
pHeight = parentElement->GetHeight();
pTop = parentElement->GetTop();
}
2010-09-24 02:48:03 +02:00
if (effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT | EFFECT_GOROUND | EFFECT_ROCK_VERTICLE)) pTop += yoffsetDyn;
2010-09-24 02:48:03 +02:00
switch (alignmentVert)
{
case ALIGN_TOP:
y = pTop;
break;
case ALIGN_MIDDLE:
2010-09-24 02:48:03 +02:00
y = pTop + (pHeight / 2) - (height / 2);
break;
case ALIGN_BOTTOM:
y = pTop + pHeight - height;
break;
}
return y + yoffset;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetMinX(int x)
{
LOCK( this );
xmin = x;
}
int GuiElement::GetMinX()
{
return xmin;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetMaxX(int x)
{
LOCK( this );
xmax = x;
}
int GuiElement::GetMaxX()
{
return xmax;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetMinY(int y)
{
LOCK( this );
ymin = y;
}
int GuiElement::GetMinY()
{
return ymin;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetMaxY(int y)
{
LOCK( this );
ymax = y;
}
int GuiElement::GetMaxY()
{
return ymax;
}
/**
* Get the width of the GuiElement.
* @see SetWidth()
* @return Width of the GuiElement.
*/
int GuiElement::GetWidth()
{
return width;
}
/**
* Get the height of the GuiElement.
* @see SetHeight()
* @return Height of the GuiElement.
*/
int GuiElement::GetHeight()
{
return height;
}
/**
* Set the width and height of the GuiElement.
* @param[in] Width Width in pixel.
* @param[in] Height Height in pixel.
* @see SetWidth()
* @see SetHeight()
*/
2010-09-24 02:48:03 +02:00
void GuiElement::SetSize(int w, int h)
{
LOCK( this );
width = w;
height = h;
}
/**
* Get visible.
* @see SetVisible()
* @return true if visible, false otherwise.
*/
bool GuiElement::IsVisible()
{
return visible;
}
/**
* Set visible.
* @param[in] Visible Set to true to show GuiElement.
* @see IsVisible()
*/
2010-09-24 02:48:03 +02:00
void GuiElement::SetVisible(bool v)
{
LOCK( this );
visible = v;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetAlpha(int a)
{
LOCK( this );
alpha = a;
}
int GuiElement::GetAlpha()
{
int a;
2010-09-24 02:48:03 +02:00
if (alphaDyn >= 0)
a = alphaDyn;
2010-09-24 02:48:03 +02:00
else a = alpha;
2010-09-24 02:48:03 +02:00
if (parentElement) a *= parentElement->GetAlpha() / 255.0;
return a;
}
float GuiElement::GetAngleDyn()
{
float a = 0.0;
2010-09-24 02:48:03 +02:00
if (angleDyn) a = angleDyn;
2010-09-24 02:48:03 +02:00
if (parentElement && !angleDyn) a = parentElement->GetAngleDyn();
return a;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetScale(float s)
{
LOCK( this );
scale = s;
}
float GuiElement::GetScale()
{
float s = scale * scaleDyn;
2010-09-24 02:48:03 +02:00
if (parentElement) s *= parentElement->GetScale();
return s;
}
int GuiElement::GetState()
{
return state;
}
int GuiElement::GetStateChan()
{
return stateChan;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetState(int s, int c)
{
LOCK( this );
state = s;
stateChan = c;
}
void GuiElement::ResetState()
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (state != STATE_DISABLED)
{
state = STATE_DEFAULT;
stateChan = -1;
}
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetClickable(bool c)
{
LOCK( this );
clickable = c;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetSelectable(bool s)
{
LOCK( this );
selectable = s;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetHoldable(bool d)
{
LOCK( this );
holdable = d;
}
bool GuiElement::IsSelectable()
{
2010-09-24 02:48:03 +02:00
if (state == STATE_DISABLED || state == STATE_CLICKED)
return false;
2010-09-24 02:48:03 +02:00
else return selectable;
}
bool GuiElement::IsClickable()
{
2010-09-24 02:48:03 +02:00
if (state == STATE_DISABLED || state == STATE_CLICKED || state == STATE_HELD)
return false;
2010-09-24 02:48:03 +02:00
else return clickable;
}
bool GuiElement::IsHoldable()
{
2010-09-24 02:48:03 +02:00
if (state == STATE_DISABLED)
return false;
2010-09-24 02:48:03 +02:00
else return holdable;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetFocus(int f)
{
LOCK( this );
focus = f;
}
int GuiElement::IsFocused()
{
return focus;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetTrigger(GuiTrigger * t)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (!trigger[0])
trigger[0] = t;
2010-09-24 02:48:03 +02:00
else if (!trigger[1])
trigger[1] = t;
2010-09-24 02:48:03 +02:00
else if (!trigger[2])
trigger[2] = t;
2010-09-24 02:48:03 +02:00
else if (!trigger[3])
trigger[3] = t;
2010-09-24 02:48:03 +02:00
else if (!trigger[4])
trigger[4] = t;
2010-09-24 02:48:03 +02:00
else if (!trigger[5])
trigger[5] = t;
else // both were assigned, so we'll just overwrite the first one
2010-09-24 02:48:03 +02:00
trigger[0] = t;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetTrigger(u8 i, GuiTrigger * t)
{
LOCK( this );
trigger[i] = t;
}
2010-09-24 02:48:03 +02:00
void GuiElement::RemoveTrigger(u8 i)
{
LOCK( this );
trigger[i] = NULL;
}
bool GuiElement::Rumble()
{
return rumble;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetRumble(bool r)
{
LOCK( this );
rumble = r;
}
int GuiElement::GetEffect()
{
LOCK( this );
return effects;
}
int GuiElement::GetEffectOnOver()
{
LOCK( this );
return effectsOver;
}
float GuiElement::GetFrequency()
{
LOCK( this );
return frequency;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetEffect(int eff, int speed, f32 circles, int r, f32 startdegree, f32 anglespeedset, int center_x,
int center_y)
{
2010-09-24 02:48:03 +02:00
if (eff & EFFECT_GOROUND)
{
2010-09-24 02:48:03 +02:00
xoffsetDyn = 0; //!position of circle in x
yoffsetDyn = 0; //!position of circle in y
Radius = r; //!radius of the circle
degree = startdegree; //!for example -90 (<28>) to start at top of circle
circleamount = circles; //!circleamoutn in degrees for example 360 for 1 circle
angleDyn = 0.0f; //!this is used by the code to calc the angle
anglespeed = anglespeedset; //!This is anglespeed depending on circle speed 1 is same speed and 0.5 half speed
2010-09-24 02:48:03 +02:00
temp_xoffset = center_x; //!position of center in x
temp_yoffset = center_y; //!position of center in y
}
effects |= eff;
2010-09-24 02:48:03 +02:00
effectAmount = speed; //!Circlespeed
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetEffect(int eff, int amount, int target)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (eff & EFFECT_SLIDE_IN)
{
// these calculations overcompensate a little
2010-09-24 02:48:03 +02:00
if (eff & EFFECT_SLIDE_TOP)
yoffsetDyn = -screenheight;
2010-09-24 02:48:03 +02:00
else if (eff & EFFECT_SLIDE_LEFT)
xoffsetDyn = -screenwidth;
2010-09-24 02:48:03 +02:00
else if (eff & EFFECT_SLIDE_BOTTOM)
yoffsetDyn = screenheight;
2010-09-24 02:48:03 +02:00
else if (eff & EFFECT_SLIDE_RIGHT) xoffsetDyn = screenwidth;
}
2010-09-24 02:48:03 +02:00
if (eff & EFFECT_FADE && amount > 0)
{
alphaDyn = 0;
}
2010-09-24 02:48:03 +02:00
else if (eff & EFFECT_FADE && amount < 0)
{
alphaDyn = alpha;
}
2010-09-24 02:48:03 +02:00
else if (eff & EFFECT_ROCK_VERTICLE)
{
changervar = 0;
yoffsetDyn = 0;
yoffsetDynFloat = 0.0;
}
effects |= eff;
effectAmount = amount;
effectTarget = target;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetEffectOnOver(int eff, int amount, int target)
{
LOCK( this );
effectsOver |= eff;
effectAmountOver = amount;
effectTargetOver = target;
}
void GuiElement::SetEffectGrow()
{
2010-09-24 02:48:03 +02:00
SetEffectOnOver(EFFECT_SCALE, 4, 110);
}
void GuiElement::StopEffect()
{
xoffsetDyn = 0;
yoffsetDyn = 0;
effects = 0;
effectsOver = 0;
effectAmount = 0;
effectAmountOver = 0;
effectTarget = 0;
effectTargetOver = 0;
scaleDyn = 1;
frequency = 0.0f;
changervar = 0;
//angleDyn = 0.0f;
anglespeed = 0.0f;
}
void GuiElement::UpdateEffects()
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT | EFFECT_GOROUND))
{
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_SLIDE_IN)
{
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_SLIDE_LEFT)
{
xoffsetDyn += effectAmount;
2010-09-24 02:48:03 +02:00
if (xoffsetDyn >= 0)
{
xoffsetDyn = 0;
effects = 0;
}
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_RIGHT)
{
xoffsetDyn -= effectAmount;
2010-09-24 02:48:03 +02:00
if (xoffsetDyn <= 0)
{
xoffsetDyn = 0;
effects = 0;
}
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_TOP)
{
yoffsetDyn += effectAmount;
2010-09-24 02:48:03 +02:00
if (yoffsetDyn >= 0)
{
yoffsetDyn = 0;
effects = 0;
}
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_BOTTOM)
{
yoffsetDyn -= effectAmount;
2010-09-24 02:48:03 +02:00
if (yoffsetDyn <= 0)
{
yoffsetDyn = 0;
effects = 0;
}
}
}
else
{
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_SLIDE_LEFT)
{
xoffsetDyn -= effectAmount;
2010-09-24 02:48:03 +02:00
if (xoffsetDyn <= -screenwidth) effects = 0; // shut off effect
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_RIGHT)
{
xoffsetDyn += effectAmount;
2010-09-24 02:48:03 +02:00
if (xoffsetDyn >= screenwidth) effects = 0; // shut off effect
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_TOP)
{
yoffsetDyn -= effectAmount;
2010-09-24 02:48:03 +02:00
if (yoffsetDyn <= -screenheight) effects = 0; // shut off effect
}
2010-09-24 02:48:03 +02:00
else if (effects & EFFECT_SLIDE_BOTTOM)
{
yoffsetDyn += effectAmount;
2010-09-24 02:48:03 +02:00
if (yoffsetDyn >= screenheight) effects = 0; // shut off effect
}
}
}
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_GOROUND)
{
//!< check out gui.h for info
xoffset = temp_xoffset;
yoffset = temp_yoffset;
2010-09-24 02:48:03 +02:00
if (fabs(frequency) < circleamount)
{
2010-09-24 02:48:03 +02:00
angleDyn = (frequency + degree + 90.0f) * anglespeed;
xoffsetDyn = (int) lround(((f32) Radius) * cos((frequency + degree) * PI / 180.0f));
yoffsetDyn = (int) lround(((f32) Radius) * sin((frequency + degree) * PI / 180.0f));
frequency += ((f32) effectAmount) * 0.01f;
}
else
{
2010-09-24 02:48:03 +02:00
f32 temp_frequency = ((effectAmount < 0) ? -1.0f : 1.0f) * circleamount;
angleDyn = (temp_frequency + degree + 90.0f) * anglespeed;
xoffsetDyn = (int) lround(((f32) Radius) * cos((temp_frequency + degree) * PI / 180.0f));
yoffsetDyn = (int) lround(((f32) Radius) * sin((temp_frequency + degree) * PI / 180.0f));
xoffset += xoffsetDyn;
yoffset += yoffsetDyn;
effects ^= EFFECT_GOROUND;
frequency = 0.0f;
}
}
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_ROCK_VERTICLE)
{
//move up to 10pixel above 0
2010-09-24 02:48:03 +02:00
if (changervar == 0 && yoffsetDynFloat < 11.0)
{
2010-09-24 02:48:03 +02:00
yoffsetDynFloat += (effectAmount * 0.01);
}
2010-09-24 02:48:03 +02:00
else if (yoffsetDynFloat > 10.0)
{
changervar = 1;
}
//move down till 10pixel under 0
2010-09-24 02:48:03 +02:00
if (changervar == 1 && yoffsetDynFloat > -11.0)
{
2010-09-24 02:48:03 +02:00
yoffsetDynFloat -= (effectAmount * 0.01);
}
2010-09-24 02:48:03 +02:00
else if (yoffsetDynFloat < -10.0)
{
changervar = 0;
}
2010-09-24 02:48:03 +02:00
yoffsetDyn = (int) (yoffsetDynFloat);
}
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_FADE)
{
alphaDyn += effectAmount;
2010-09-24 02:48:03 +02:00
if (effectAmount < 0 && alphaDyn <= 0)
{
alphaDyn = 0;
effects = 0; // shut off effect
}
2010-09-24 02:48:03 +02:00
else if (effectAmount > 0 && alphaDyn >= alpha)
{
alphaDyn = alpha;
effects = 0; // shut off effect
}
}
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_SCALE)
{
scaleDyn += effectAmount / 100.0;
2010-09-24 02:48:03 +02:00
if ((effectAmount < 0 && scaleDyn <= effectTarget / 100.0) || (effectAmount > 0 && scaleDyn >= effectTarget
/ 100.0))
{
scaleDyn = effectTarget / 100.0;
effects = 0; // shut off effect
}
}
2010-09-24 02:48:03 +02:00
if (effects & EFFECT_PULSE)
{
int percent = 10; //go down from target by this
2010-09-24 02:48:03 +02:00
if ((scaleDyn <= (effectTarget * 0.01)) && (!changervar))
{
2010-09-24 02:48:03 +02:00
scaleDyn += (effectAmount * 0.001);
}
2010-09-24 02:48:03 +02:00
else if (scaleDyn > (effectTarget * 0.01))
{
changervar = 1;
}
2010-09-24 02:48:03 +02:00
if ((scaleDyn >= ((effectTarget - percent) * 0.01)) && (changervar))
{
2010-09-24 02:48:03 +02:00
scaleDyn -= (effectAmount * 0.001);
}
2010-09-24 02:48:03 +02:00
else if (scaleDyn < ((effectTarget - percent) * 0.01))
{
changervar = 0;
}
}
}
2010-09-24 02:48:03 +02:00
void GuiElement::Update(GuiTrigger * t)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (updateCB) updateCB(this);
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetUpdateCallback(UpdateCallback u)
{
LOCK( this );
updateCB = u;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetPosition(int xoff, int yoff, int zoff)
{
LOCK( this );
xoffset = xoff;
yoffset = yoff;
zoffset = zoff;
}
2010-09-24 02:48:03 +02:00
void GuiElement::SetAlignment(int hor, int vert)
{
LOCK( this );
alignmentHor = hor;
alignmentVert = vert;
}
int GuiElement::GetSelected()
{
return -1;
}
/**
* Draw an element on screen.
*/
void GuiElement::Draw()
{
}
/**
* Draw Tooltips on screen.
*/
void GuiElement::DrawTooltip()
{
}
/**
* Check if a position is inside the GuiElement.
* @param[in] x X position in pixel.
* @param[in] y Y position in pixel.
*/
2010-09-24 02:48:03 +02:00
bool GuiElement::IsInside(int x, int y)
{
2010-09-24 02:48:03 +02:00
if (x > this->GetLeft() && x < (this->GetLeft() + width) && y > this->GetTop() && y < (this->GetTop() + height)) return true;
return false;
}
void GuiElement::Lock()
{
2010-09-24 02:48:03 +02:00
// LWP_MutexLock(mutex);
for (;;) // loop while element is locked by self
{
2010-09-24 02:48:03 +02:00
LWP_MutexLock(_lock_mutex);
2010-09-24 02:48:03 +02:00
if (_lock_thread == LWP_THREAD_NULL) // element is not locked
{
2010-09-24 02:48:03 +02:00
_lock_thread = LWP_GetSelf(); // mark as locked
_lock_count = 1; // set count of lock to 1
LWP_MutexUnlock(_lock_mutex);
return;
}
2010-09-24 02:48:03 +02:00
else if (_lock_thread == LWP_GetSelf()) // thread is locked by my self
{
2010-09-24 02:48:03 +02:00
_lock_count++; // inc count of locks;
LWP_MutexUnlock(_lock_mutex);
return;
}
2010-09-24 02:48:03 +02:00
else // otherwise the element is locked by an other thread
{
2010-09-24 02:48:03 +02:00
if (_lock_queue == LWP_TQUEUE_NULL) // no queue - meens it is the first access to the locked element
LWP_InitQueue(&_lock_queue); // init queue
LWP_MutexUnlock(_lock_mutex);
LWP_ThreadSleep(_lock_queue); // and sleep
// try lock again;
}
}
}
void GuiElement::Unlock()
{
2010-09-24 02:48:03 +02:00
// LWP_MutexUnlock(mutex);
LWP_MutexLock(_lock_mutex);
// only the thread was locked this element, can call unlock
2010-09-24 02:48:03 +02:00
if (_lock_thread == LWP_GetSelf()) // but we check it here <20> safe is safe
{
2010-09-24 02:48:03 +02:00
if (--_lock_count == 0) // dec count of locks and check if it last lock;
{
2010-09-24 02:48:03 +02:00
_lock_thread = LWP_THREAD_NULL; // mark as unlocked
if (_lock_queue != LWP_TQUEUE_NULL) // has a queue
{
2010-09-24 02:48:03 +02:00
LWP_CloseQueue(_lock_queue); // close the queue and wake all waited threads
_lock_queue = LWP_TQUEUE_NULL;
}
}
}
2010-09-24 02:48:03 +02:00
LWP_MutexUnlock(_lock_mutex);
}
2010-09-24 02:48:03 +02:00
SimpleLock::SimpleLock(GuiElement *e) :
element(e)
{
element->Lock();
}
SimpleLock::~SimpleLock()
{
element->Unlock();
}