2009-03-27 05:02:39 +01:00
|
|
|
/****************************************************************************
|
2009-04-10 09:50:10 +02:00
|
|
|
* libwiigui
|
2009-03-27 05:02:39 +01:00
|
|
|
*
|
2009-04-10 09:50:10 +02:00
|
|
|
* Tantric 2009
|
2009-03-27 05:02:39 +01:00
|
|
|
*
|
|
|
|
* gui_button.cpp
|
|
|
|
*
|
|
|
|
* GUI class definitions
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gui.h"
|
|
|
|
/**
|
|
|
|
* Constructor for the GuiButton class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
GuiButton::GuiButton(int w, int h)
|
|
|
|
{
|
|
|
|
width = w;
|
|
|
|
height = h;
|
|
|
|
image = NULL;
|
|
|
|
imageOver = NULL;
|
2009-04-13 10:43:20 +02:00
|
|
|
imageHold = NULL;
|
|
|
|
imageClick = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
icon = NULL;
|
|
|
|
iconOver = NULL;
|
2009-04-13 10:43:20 +02:00
|
|
|
iconHold = NULL;
|
|
|
|
iconClick = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
for(int i=0; i < 3; i++)
|
|
|
|
{
|
|
|
|
label[i] = NULL;
|
|
|
|
labelOver[i] = NULL;
|
2009-04-13 10:43:20 +02:00
|
|
|
labelHold[i] = NULL;
|
|
|
|
labelClick[i] = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
soundOver = NULL;
|
2009-04-13 10:43:20 +02:00
|
|
|
soundHold = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
soundClick = NULL;
|
|
|
|
selectable = true;
|
2009-04-13 10:43:20 +02:00
|
|
|
holdable = false;
|
2009-03-27 05:02:39 +01:00
|
|
|
clickable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor for the GuiButton class.
|
|
|
|
*/
|
|
|
|
GuiButton::~GuiButton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiButton::SetImage(GuiImage* img)
|
|
|
|
{
|
|
|
|
image = img;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetImageOver(GuiImage* img)
|
|
|
|
{
|
|
|
|
imageOver = img;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(img) img->SetParent(this);
|
|
|
|
}
|
|
|
|
void GuiButton::SetImageHold(GuiImage* img)
|
|
|
|
{
|
|
|
|
imageHold = img;
|
|
|
|
if(img) img->SetParent(this);
|
|
|
|
}
|
|
|
|
void GuiButton::SetImageClick(GuiImage* img)
|
|
|
|
{
|
|
|
|
imageClick = img;
|
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetIcon(GuiImage* img)
|
|
|
|
{
|
|
|
|
icon = img;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetIconOver(GuiImage* img)
|
|
|
|
{
|
|
|
|
iconOver = img;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
2009-04-13 10:43:20 +02:00
|
|
|
void GuiButton::SetIconHold(GuiImage* img)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
iconHold = img;
|
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
2009-04-13 10:43:20 +02:00
|
|
|
void GuiButton::SetIconClick(GuiImage* img)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
iconClick = img;
|
|
|
|
if(img) img->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetLabel(GuiText* txt, int n)
|
|
|
|
{
|
|
|
|
label[n] = txt;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(txt) txt->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetLabelOver(GuiText* txt, int n)
|
|
|
|
{
|
|
|
|
labelOver[n] = txt;
|
2009-04-13 10:43:20 +02:00
|
|
|
if(txt) txt->SetParent(this);
|
|
|
|
}
|
|
|
|
void GuiButton::SetLabelHold(GuiText* txt, int n)
|
|
|
|
{
|
|
|
|
labelHold[n] = txt;
|
|
|
|
if(txt) txt->SetParent(this);
|
|
|
|
}
|
|
|
|
void GuiButton::SetLabelClick(GuiText* txt, int n)
|
|
|
|
{
|
|
|
|
labelClick[n] = txt;
|
|
|
|
if(txt) txt->SetParent(this);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
void GuiButton::SetSoundOver(GuiSound * snd)
|
|
|
|
{
|
|
|
|
soundOver = snd;
|
|
|
|
}
|
2009-04-13 10:43:20 +02:00
|
|
|
void GuiButton::SetSoundHold(GuiSound * snd)
|
|
|
|
{
|
|
|
|
soundHold = snd;
|
|
|
|
}
|
2009-03-27 05:02:39 +01:00
|
|
|
void GuiButton::SetSoundClick(GuiSound * snd)
|
|
|
|
{
|
|
|
|
soundClick = snd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the button on screen
|
|
|
|
*/
|
|
|
|
void GuiButton::Draw()
|
|
|
|
{
|
|
|
|
if(!this->IsVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// draw image
|
2009-04-10 09:50:10 +02:00
|
|
|
if((state == STATE_SELECTED || state == STATE_HELD) && imageOver)
|
2009-03-27 05:02:39 +01:00
|
|
|
imageOver->Draw();
|
|
|
|
else if(image)
|
|
|
|
image->Draw();
|
|
|
|
// draw icon
|
2009-04-10 09:50:10 +02:00
|
|
|
if((state == STATE_SELECTED || state == STATE_HELD) && iconOver)
|
2009-03-27 05:02:39 +01:00
|
|
|
iconOver->Draw();
|
|
|
|
else if(icon)
|
|
|
|
icon->Draw();
|
|
|
|
// draw text
|
|
|
|
for(int i=0; i<3; i++)
|
|
|
|
{
|
2009-04-10 09:50:10 +02:00
|
|
|
if((state == STATE_SELECTED || state == STATE_HELD) && labelOver[i])
|
2009-03-27 05:02:39 +01:00
|
|
|
labelOver[i]->Draw();
|
|
|
|
else if(label[i])
|
|
|
|
label[i]->Draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->UpdateEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiButton::Update(GuiTrigger * t)
|
|
|
|
{
|
|
|
|
if(state == STATE_CLICKED || state == STATE_DISABLED || !t)
|
|
|
|
return;
|
|
|
|
else if(parentElement && parentElement->GetState() == STATE_DISABLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef HW_RVL
|
|
|
|
// cursor
|
|
|
|
if(t->wpad.ir.valid)
|
|
|
|
{
|
|
|
|
if(this->IsInside(t->wpad.ir.x, t->wpad.ir.y))
|
|
|
|
{
|
|
|
|
if(state == STATE_DEFAULT) // we weren't on the button before!
|
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
this->SetState(STATE_SELECTED, t->chan);
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
if(this->Rumble())
|
|
|
|
rumbleRequest[t->chan] = 1;
|
|
|
|
|
|
|
|
if(soundOver)
|
|
|
|
soundOver->Play();
|
|
|
|
|
|
|
|
if(effectsOver && !effects)
|
|
|
|
{
|
|
|
|
// initiate effects
|
|
|
|
effects = effectsOver;
|
|
|
|
effectAmount = effectAmountOver;
|
|
|
|
effectTarget = effectTargetOver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
if(state == STATE_SELECTED && (stateChan == t->chan || stateChan == -1))
|
|
|
|
this->ResetState();
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
if(effectTarget == effectTargetOver && effectAmount == effectAmountOver)
|
|
|
|
{
|
|
|
|
// initiate effects (in reverse)
|
|
|
|
effects = effectsOver;
|
|
|
|
effectAmount = -effectAmountOver;
|
|
|
|
effectTarget = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// button triggers
|
|
|
|
if(this->IsClickable())
|
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
s32 wm_btns, wm_btns_trig, cc_btns, cc_btns_trig;
|
2009-03-27 05:02:39 +01:00
|
|
|
for(int i=0; i<2; i++)
|
|
|
|
{
|
|
|
|
if(trigger[i] && (trigger[i]->chan == -1 || trigger[i]->chan == t->chan))
|
|
|
|
{
|
|
|
|
// higher 16 bits only (wiimote)
|
2009-04-13 10:43:20 +02:00
|
|
|
wm_btns = t->wpad.btns_d << 16;
|
|
|
|
wm_btns_trig = trigger[i]->wpad.btns_d << 16;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
// lower 16 bits only (classic controller)
|
2009-04-13 10:43:20 +02:00
|
|
|
cc_btns = t->wpad.btns_d >> 16;
|
|
|
|
cc_btns_trig = trigger[i]->wpad.btns_d >> 16;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
if(
|
|
|
|
(t->wpad.btns_d > 0 &&
|
|
|
|
wm_btns == wm_btns_trig ||
|
|
|
|
(cc_btns == cc_btns_trig && t->wpad.exp.type == EXP_CLASSIC)) ||
|
|
|
|
(t->pad.btns_d == trigger[i]->pad.btns_d && t->pad.btns_d > 0))
|
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
if(t->chan == stateChan || stateChan == -1)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
if(state == STATE_SELECTED)
|
|
|
|
{
|
|
|
|
this->SetState(STATE_CLICKED, t->chan);
|
|
|
|
|
|
|
|
if(soundClick)
|
|
|
|
soundClick->Play();
|
|
|
|
}
|
|
|
|
else if(trigger[i]->type == TRIGGER_BUTTON_ONLY)
|
|
|
|
{
|
|
|
|
this->SetState(STATE_CLICKED, t->chan);
|
|
|
|
}
|
|
|
|
else if(trigger[i]->type == TRIGGER_BUTTON_ONLY_IN_FOCUS &&
|
|
|
|
parentElement->IsFocused())
|
|
|
|
{
|
|
|
|
this->SetState(STATE_CLICKED, t->chan);
|
|
|
|
}
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 10:43:20 +02:00
|
|
|
if(this->IsHoldable())
|
2009-04-10 09:50:10 +02:00
|
|
|
{
|
|
|
|
bool held = false;
|
2009-04-13 10:43:20 +02:00
|
|
|
s32 wm_btns, wm_btns_h, wm_btns_trig, cc_btns, cc_btns_h, cc_btns_trig;
|
2009-04-10 09:50:10 +02:00
|
|
|
|
|
|
|
for(int i=0; i<2; i++)
|
|
|
|
{
|
|
|
|
if(trigger[i] && (trigger[i]->chan == -1 || trigger[i]->chan == t->chan))
|
|
|
|
{
|
|
|
|
// higher 16 bits only (wiimote)
|
2009-04-13 10:43:20 +02:00
|
|
|
wm_btns = t->wpad.btns_d << 16;
|
|
|
|
wm_btns_h = t->wpad.btns_h << 16;
|
|
|
|
wm_btns_trig = trigger[i]->wpad.btns_h << 16;
|
2009-04-10 09:50:10 +02:00
|
|
|
|
|
|
|
// lower 16 bits only (classic controller)
|
2009-04-13 10:43:20 +02:00
|
|
|
cc_btns = t->wpad.btns_d >> 16;
|
|
|
|
cc_btns_h = t->wpad.btns_h >> 16;
|
|
|
|
cc_btns_trig = trigger[i]->wpad.btns_h >> 16;
|
2009-04-10 09:50:10 +02:00
|
|
|
|
|
|
|
if(
|
2009-04-13 10:43:20 +02:00
|
|
|
(t->wpad.btns_d > 0 &&
|
2009-04-10 09:50:10 +02:00
|
|
|
wm_btns == wm_btns_trig ||
|
|
|
|
(cc_btns == cc_btns_trig && t->wpad.exp.type == EXP_CLASSIC)) ||
|
2009-04-13 10:43:20 +02:00
|
|
|
(t->pad.btns_d == trigger[i]->pad.btns_h && t->pad.btns_d > 0))
|
|
|
|
{
|
|
|
|
if(trigger[i]->type == TRIGGER_HELD && state == STATE_SELECTED &&
|
|
|
|
(t->chan == stateChan || stateChan == -1))
|
|
|
|
this->SetState(STATE_CLICKED, t->chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
|
|
|
(t->wpad.btns_h > 0 &&
|
|
|
|
wm_btns_h == wm_btns_trig ||
|
|
|
|
(cc_btns_h == cc_btns_trig && t->wpad.exp.type == EXP_CLASSIC)) ||
|
2009-04-10 09:50:10 +02:00
|
|
|
(t->pad.btns_h == trigger[i]->pad.btns_h && t->pad.btns_h > 0))
|
|
|
|
{
|
|
|
|
if(trigger[i]->type == TRIGGER_HELD)
|
|
|
|
held = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!held && state == STATE_HELD && stateChan == t->chan)
|
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
this->ResetState();
|
2009-04-10 09:50:10 +02:00
|
|
|
}
|
2009-04-13 10:43:20 +02:00
|
|
|
else if(held && state == STATE_CLICKED && stateChan == t->chan)
|
2009-04-10 09:50:10 +02:00
|
|
|
{
|
2009-04-13 10:43:20 +02:00
|
|
|
this->SetState(STATE_HELD, t->chan);
|
2009-04-10 09:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-27 05:02:39 +01:00
|
|
|
if(updateCB)
|
|
|
|
updateCB(this);
|
|
|
|
}
|