fceugx/source/gui/gui_optionbrowser.cpp

366 lines
8.5 KiB
C++
Raw Normal View History

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_optionbrowser.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
/**
* Constructor for the GuiOptionBrowser class.
*/
GuiOptionBrowser::GuiOptionBrowser(int w, int h, OptionList * l)
{
width = w;
height = h;
options = l;
selectable = true;
listOffset = this->FindMenuItem(-1, 1);
2009-07-28 00:37:56 +02:00
listChanged = true; // trigger an initial list update
2009-03-27 05:02:39 +01:00
selectedItem = 0;
focus = 0; // allow focus
trigA = new GuiTrigger;
if(GCSettings.WiimoteOrientation)
trigA->SetSimpleTrigger(-1, WPAD_BUTTON_2 | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
else
trigA->SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
2009-04-05 05:11:38 +02:00
btnSoundOver = new GuiSound(button_over_pcm, button_over_pcm_size, SOUND_PCM);
btnSoundClick = new GuiSound(button_click_pcm, button_click_pcm_size, SOUND_PCM);
2009-03-27 05:02:39 +01:00
bgOptions = new GuiImageData(bg_options_png);
bgOptionsImg = new GuiImage(bgOptions);
bgOptionsImg->SetParent(this);
bgOptionsImg->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
bgOptionsEntry = new GuiImageData(bg_options_entry_png);
scrollbar = new GuiImageData(scrollbar_png);
scrollbarImg = new GuiImage(scrollbar);
scrollbarImg->SetParent(this);
scrollbarImg->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
scrollbarImg->SetPosition(0, 30);
arrowDown = new GuiImageData(scrollbar_arrowdown_png);
arrowDownImg = new GuiImage(arrowDown);
arrowDownOver = new GuiImageData(scrollbar_arrowdown_over_png);
arrowDownOverImg = new GuiImage(arrowDownOver);
arrowUp = new GuiImageData(scrollbar_arrowup_png);
arrowUpImg = new GuiImage(arrowUp);
arrowUpOver = new GuiImageData(scrollbar_arrowup_over_png);
arrowUpOverImg = new GuiImage(arrowUpOver);
arrowUpBtn = new GuiButton(arrowUpImg->GetWidth(), arrowUpImg->GetHeight());
arrowUpBtn->SetParent(this);
arrowUpBtn->SetImage(arrowUpImg);
arrowUpBtn->SetImageOver(arrowUpOverImg);
arrowUpBtn->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
arrowUpBtn->SetSelectable(false);
arrowUpBtn->SetTrigger(trigA);
2009-04-05 05:11:38 +02:00
arrowUpBtn->SetSoundOver(btnSoundOver);
arrowUpBtn->SetSoundClick(btnSoundClick);
2009-03-27 05:02:39 +01:00
arrowDownBtn = new GuiButton(arrowDownImg->GetWidth(), arrowDownImg->GetHeight());
arrowDownBtn->SetParent(this);
arrowDownBtn->SetImage(arrowDownImg);
arrowDownBtn->SetImageOver(arrowDownOverImg);
arrowDownBtn->SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
arrowDownBtn->SetSelectable(false);
arrowDownBtn->SetTrigger(trigA);
2009-04-05 05:11:38 +02:00
arrowDownBtn->SetSoundOver(btnSoundOver);
arrowDownBtn->SetSoundClick(btnSoundClick);
2009-03-27 05:02:39 +01:00
for(int i=0; i<PAGESIZE; i++)
{
2010-03-15 08:43:20 +01:00
optionTxt[i] = new GuiText(NULL, 20, (GXColor){0, 0, 0, 0xff});
2009-03-27 05:02:39 +01:00
optionTxt[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
optionTxt[i]->SetPosition(8,0);
2010-03-15 08:43:20 +01:00
optionVal[i] = new GuiText(NULL, 20, (GXColor){0, 0, 0, 0xff});
2009-03-27 05:02:39 +01:00
optionVal[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
optionVal[i]->SetPosition(250,0);
optionBg[i] = new GuiImage(bgOptionsEntry);
optionBtn[i] = new GuiButton(512,30);
optionBtn[i]->SetParent(this);
optionBtn[i]->SetLabel(optionTxt[i], 0);
optionBtn[i]->SetLabel(optionVal[i], 1);
optionBtn[i]->SetImageOver(optionBg[i]);
optionBtn[i]->SetPosition(0,30*i+3);
optionBtn[i]->SetTrigger(trigA);
2009-04-05 05:11:38 +02:00
optionBtn[i]->SetSoundClick(btnSoundClick);
2009-03-27 05:02:39 +01:00
}
}
/**
* Destructor for the GuiOptionBrowser class.
*/
GuiOptionBrowser::~GuiOptionBrowser()
{
delete arrowUpBtn;
delete arrowDownBtn;
delete bgOptionsImg;
delete scrollbarImg;
delete arrowDownImg;
delete arrowDownOverImg;
delete arrowUpImg;
delete arrowUpOverImg;
delete bgOptions;
delete bgOptionsEntry;
delete scrollbar;
delete arrowDown;
delete arrowDownOver;
delete arrowUp;
delete arrowUpOver;
delete trigA;
2009-04-05 05:11:38 +02:00
delete btnSoundOver;
delete btnSoundClick;
2009-03-27 05:02:39 +01:00
for(int i=0; i<PAGESIZE; i++)
{
delete optionTxt[i];
delete optionVal[i];
delete optionBg[i];
delete optionBtn[i];
}
}
2010-01-25 09:23:03 +01:00
void GuiOptionBrowser::SetCol1Position(int x)
{
for(int i=0; i<PAGESIZE; i++)
optionTxt[i]->SetPosition(x,0);
}
2009-03-27 05:02:39 +01:00
void GuiOptionBrowser::SetCol2Position(int x)
{
for(int i=0; i<PAGESIZE; i++)
optionVal[i]->SetPosition(x,0);
}
void GuiOptionBrowser::SetFocus(int f)
{
focus = f;
for(int i=0; i<PAGESIZE; i++)
optionBtn[i]->ResetState();
if(f == 1)
optionBtn[selectedItem]->SetState(STATE_SELECTED);
}
void GuiOptionBrowser::ResetState()
{
if(state != STATE_DISABLED)
{
2009-03-27 05:02:39 +01:00
state = STATE_DEFAULT;
stateChan = -1;
}
2009-03-27 05:02:39 +01:00
for(int i=0; i<PAGESIZE; i++)
{
optionBtn[i]->ResetState();
}
}
int GuiOptionBrowser::GetClickedOption()
{
int found = -1;
for(int i=0; i<PAGESIZE; i++)
{
if(optionBtn[i]->GetState() == STATE_CLICKED)
{
optionBtn[i]->SetState(STATE_SELECTED);
found = optionIndex[i];
break;
}
}
return found;
}
/****************************************************************************
* FindMenuItem
*
* Help function to find the next visible menu item on the list
***************************************************************************/
int GuiOptionBrowser::FindMenuItem(int currentItem, int direction)
{
int nextItem = currentItem + direction;
if(nextItem < 0 || nextItem >= options->length)
return -1;
if(strlen(options->name[nextItem]) > 0)
return nextItem;
else
return FindMenuItem(nextItem, direction);
}
/**
* Draw the button on screen
*/
void GuiOptionBrowser::Draw()
{
if(!this->IsVisible())
return;
bgOptionsImg->Draw();
int next = listOffset;
2010-01-25 08:33:35 +01:00
for(int i=0; i<PAGESIZE; ++i)
2009-03-27 05:02:39 +01:00
{
if(next >= 0)
{
optionBtn[i]->Draw();
next = this->FindMenuItem(next, 1);
}
else
break;
}
scrollbarImg->Draw();
arrowUpBtn->Draw();
arrowDownBtn->Draw();
this->UpdateEffects();
}
2009-07-28 00:37:56 +02:00
void GuiOptionBrowser::TriggerUpdate()
{
listChanged = true;
}
2010-01-25 09:23:03 +01:00
void GuiOptionBrowser::ResetText()
{
int next = listOffset;
for(int i=0; i<PAGESIZE; i++)
{
if(next >= 0)
{
optionBtn[i]->ResetText();
next = this->FindMenuItem(next, 1);
}
else
break;
}
}
2009-03-27 05:02:39 +01:00
void GuiOptionBrowser::Update(GuiTrigger * t)
{
if(state == STATE_DISABLED || !t)
return;
int next, prev;
arrowUpBtn->Update(t);
arrowDownBtn->Update(t);
next = listOffset;
2009-07-28 00:37:56 +02:00
if(listChanged)
2009-03-27 05:02:39 +01:00
{
2009-07-28 00:37:56 +02:00
listChanged = false;
2010-01-25 08:33:35 +01:00
for(int i=0; i<PAGESIZE; ++i)
2009-03-27 05:02:39 +01:00
{
2009-07-28 00:37:56 +02:00
if(next >= 0)
2009-03-27 05:02:39 +01:00
{
2009-07-28 00:37:56 +02:00
if(optionBtn[i]->GetState() == STATE_DISABLED)
{
optionBtn[i]->SetVisible(true);
optionBtn[i]->SetState(STATE_DEFAULT);
}
optionTxt[i]->SetText(options->name[next]);
optionVal[i]->SetText(options->value[next]);
optionIndex[i] = next;
next = this->FindMenuItem(next, 1);
}
else
{
optionBtn[i]->SetVisible(false);
optionBtn[i]->SetState(STATE_DISABLED);
2009-03-27 05:02:39 +01:00
}
}
2009-07-28 00:37:56 +02:00
}
2009-03-27 05:02:39 +01:00
2010-01-25 08:33:35 +01:00
for(int i=0; i<PAGESIZE; ++i)
2009-07-28 00:37:56 +02:00
{
2009-05-28 09:51:31 +02:00
if(i != selectedItem && optionBtn[i]->GetState() == STATE_SELECTED)
optionBtn[i]->ResetState();
else if(focus && i == selectedItem && optionBtn[i]->GetState() == STATE_DEFAULT)
optionBtn[selectedItem]->SetState(STATE_SELECTED, t->chan);
2009-03-27 05:02:39 +01:00
2009-05-28 09:16:15 +02:00
int currChan = t->chan;
if(t->wpad->ir.valid && !optionBtn[i]->IsInside(t->wpad->ir.x, t->wpad->ir.y))
2009-05-28 09:16:15 +02:00
t->chan = -1;
2009-03-27 05:02:39 +01:00
optionBtn[i]->Update(t);
2009-05-28 09:16:15 +02:00
t->chan = currChan;
2009-03-27 05:02:39 +01:00
if(optionBtn[i]->GetState() == STATE_SELECTED)
selectedItem = i;
}
// pad/joystick navigation
if(!focus)
return; // skip navigation
if(t->Down() || arrowDownBtn->GetState() == STATE_CLICKED)
{
next = this->FindMenuItem(optionIndex[selectedItem], 1);
if(next >= 0)
{
if(selectedItem == PAGESIZE-1)
{
// move list down by 1
listOffset = this->FindMenuItem(listOffset, 1);
2009-07-28 00:37:56 +02:00
listChanged = true;
2009-03-27 05:02:39 +01:00
}
else if(optionBtn[selectedItem+1]->IsVisible())
{
optionBtn[selectedItem]->ResetState();
optionBtn[selectedItem+1]->SetState(STATE_SELECTED, t->chan);
2010-01-25 08:33:35 +01:00
++selectedItem;
2009-03-27 05:02:39 +01:00
}
}
arrowDownBtn->ResetState();
}
else if(t->Up() || arrowUpBtn->GetState() == STATE_CLICKED)
{
prev = this->FindMenuItem(optionIndex[selectedItem], -1);
if(prev >= 0)
{
if(selectedItem == 0)
{
// move list up by 1
listOffset = prev;
2009-07-28 00:37:56 +02:00
listChanged = true;
2009-03-27 05:02:39 +01:00
}
else
{
optionBtn[selectedItem]->ResetState();
optionBtn[selectedItem-1]->SetState(STATE_SELECTED, t->chan);
2010-01-25 08:33:35 +01:00
--selectedItem;
2009-03-27 05:02:39 +01:00
}
}
arrowUpBtn->ResetState();
}
if(updateCB)
updateCB(this);
}