snes9xgx/source/ngc/gui/gui_savebrowser.cpp

408 lines
10 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_savebrowser.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
#include "filebrowser.h"
/**
* Constructor for the GuiSaveBrowser class.
*/
GuiSaveBrowser::GuiSaveBrowser(int w, int h, SaveList * s, int a)
{
width = w;
height = h;
saves = s;
action = a;
selectable = true;
if(action == 0) // load
2009-03-11 18:28:37 +01:00
listOffset = 0;
else
listOffset = -2; // save - reserve -2 & -1 for new slots
2009-03-11 18:28:37 +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 03:51:56 +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-11 18:28:37 +01:00
gameSave = new GuiImageData(button_gamesave_png);
gameSaveOver = new GuiImageData(button_gamesave_over_png);
gameSaveBlank = new GuiImageData(button_gamesave_blank_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 03:51:56 +02:00
arrowUpBtn->SetSoundOver(btnSoundOver);
arrowUpBtn->SetSoundClick(btnSoundClick);
2009-03-11 18:28:37 +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 03:51:56 +02:00
arrowDownBtn->SetSoundOver(btnSoundOver);
arrowDownBtn->SetSoundClick(btnSoundClick);
2009-03-11 18:28:37 +01:00
for(int i=0; i<SAVELISTSIZE; i++)
{
2010-03-15 08:41:40 +01:00
saveDate[i] = new GuiText(NULL, 18, (GXColor){0, 0, 0, 0xff});
2009-03-11 18:28:37 +01:00
saveDate[i]->SetAlignment(ALIGN_LEFT, ALIGN_TOP);
saveDate[i]->SetPosition(80,5);
2010-03-15 08:41:40 +01:00
saveTime[i] = new GuiText(NULL, 18, (GXColor){0, 0, 0, 0xff});
2009-03-11 18:28:37 +01:00
saveTime[i]->SetAlignment(ALIGN_LEFT, ALIGN_TOP);
saveTime[i]->SetPosition(80,27);
2010-03-15 08:41:40 +01:00
saveType[i] = new GuiText(NULL, 18, (GXColor){0, 0, 0, 0xff});
2009-03-11 18:28:37 +01:00
saveType[i]->SetAlignment(ALIGN_LEFT, ALIGN_TOP);
saveType[i]->SetPosition(80,50);
saveBgImg[i] = new GuiImage(gameSave);
saveBgOverImg[i] = new GuiImage(gameSaveOver);
savePreviewImg[i] = new GuiImage(gameSaveBlank);
savePreviewImg[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
savePreviewImg[i]->SetPosition(5,0);
saveBtn[i] = new GuiButton(saveBgImg[i]->GetWidth(),saveBgImg[i]->GetHeight());
saveBtn[i]->SetParent(this);
saveBtn[i]->SetLabel(saveDate[i], 0);
saveBtn[i]->SetLabel(saveTime[i], 1);
saveBtn[i]->SetLabel(saveType[i], 2);
saveBtn[i]->SetImage(saveBgImg[i]);
saveBtn[i]->SetImageOver(saveBgOverImg[i]);
saveBtn[i]->SetIcon(savePreviewImg[i]);
saveBtn[i]->SetAlignment(ALIGN_LEFT, ALIGN_TOP);
2010-01-25 08:34:45 +01:00
saveBtn[i]->SetPosition(257*(i % 2),87*(i>>1));
2009-03-11 18:28:37 +01:00
saveBtn[i]->SetTrigger(trigA);
saveBtn[i]->SetState(STATE_DISABLED);
saveBtn[i]->SetEffectGrow();
2009-03-17 06:09:45 +01:00
saveBtn[i]->SetVisible(false);
2009-04-05 03:51:56 +02:00
saveBtn[i]->SetSoundOver(btnSoundOver);
saveBtn[i]->SetSoundClick(btnSoundClick);
2009-06-13 03:38:21 +02:00
saveBtnLastOver[i] = false;
2009-03-11 18:28:37 +01:00
}
2009-06-13 03:38:21 +02:00
saveBtn[0]->SetState(STATE_SELECTED, -1);
saveBtn[0]->SetVisible(true);
2009-03-11 18:28:37 +01:00
}
/**
* Destructor for the GuiSaveBrowser class.
*/
GuiSaveBrowser::~GuiSaveBrowser()
{
delete arrowUpBtn;
delete arrowDownBtn;
delete scrollbarImg;
delete arrowDownImg;
delete arrowDownOverImg;
delete arrowUpImg;
delete arrowUpOverImg;
delete gameSave;
delete gameSaveOver;
delete gameSaveBlank;
delete scrollbar;
delete arrowDown;
delete arrowDownOver;
delete arrowUp;
delete arrowUpOver;
2009-04-05 03:51:56 +02:00
delete btnSoundOver;
delete btnSoundClick;
2009-03-11 18:28:37 +01:00
delete trigA;
for(int i=0; i<SAVELISTSIZE; i++)
{
delete saveBtn[i];
delete saveDate[i];
delete saveTime[i];
delete saveType[i];
delete saveBgImg[i];
delete saveBgOverImg[i];
delete savePreviewImg[i];
}
}
void GuiSaveBrowser::SetFocus(int f)
{
focus = f;
for(int i=0; i<SAVELISTSIZE; i++)
saveBtn[i]->ResetState();
2009-05-30 08:28:12 +02:00
if(f == 1 && selectedItem >= 0)
2009-03-11 18:28:37 +01:00
saveBtn[selectedItem]->SetState(STATE_SELECTED);
}
void GuiSaveBrowser::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
for(int i=0; i<SAVELISTSIZE; i++)
{
saveBtn[i]->ResetState();
}
}
int GuiSaveBrowser::GetClickedSave()
{
int found = -3;
2009-03-11 18:28:37 +01:00
for(int i=0; i<SAVELISTSIZE; i++)
{
if(saveBtn[i]->GetState() == STATE_CLICKED)
{
saveBtn[i]->SetState(STATE_SELECTED);
found = listOffset+i;
2009-03-11 18:28:37 +01:00
break;
}
}
return found;
}
/**
* Draw the button on screen
*/
void GuiSaveBrowser::Draw()
{
if(!this->IsVisible())
return;
for(int i=0; i<SAVELISTSIZE; i++)
saveBtn[i]->Draw();
scrollbarImg->Draw();
arrowUpBtn->Draw();
arrowDownBtn->Draw();
2009-03-16 06:16:20 +01:00
this->UpdateEffects();
2009-03-11 18:28:37 +01:00
}
void GuiSaveBrowser::Update(GuiTrigger * t)
{
2009-03-20 08:30:29 +01:00
if(state == STATE_DISABLED || !t)
return;
2009-03-11 18:28:37 +01:00
int i, len;
char savetext[50];
arrowUpBtn->Update(t);
arrowDownBtn->Update(t);
// pad/joystick navigation
if(!focus)
goto endNavigation; // skip navigation
2009-06-13 03:38:21 +02:00
if(selectedItem < 0) selectedItem = 0;
2009-03-11 18:28:37 +01:00
if(t->Right())
{
if(selectedItem == SAVELISTSIZE-1)
{
if(listOffset + SAVELISTSIZE < saves->length)
{
// move list down by 1 row
listOffset += 2;
selectedItem -= 1;
}
}
else if(saveBtn[selectedItem+1]->IsVisible())
{
saveBtn[selectedItem]->ResetState();
saveBtn[selectedItem+1]->SetState(STATE_SELECTED, t->chan);
2009-03-11 18:28:37 +01:00
selectedItem += 1;
}
}
else if(t->Left())
{
if(selectedItem == 0)
{
if((listOffset - 2 >= 0 && action == 0) ||
2010-01-25 08:34:45 +01:00
(listOffset >= 0 && action == 1))
2009-03-11 18:28:37 +01:00
{
// move list up by 1
listOffset -= 2;
selectedItem = SAVELISTSIZE-1;
}
}
else
{
selectedItem -= 1;
}
}
2009-03-17 06:09:45 +01:00
else if(t->Down() || arrowDownBtn->GetState() == STATE_CLICKED)
2009-03-11 18:28:37 +01:00
{
if(selectedItem >= SAVELISTSIZE-2)
{
if(listOffset + SAVELISTSIZE + 1 < saves->length)
{
listOffset += 2;
}
else if(listOffset + SAVELISTSIZE < saves->length)
{
listOffset += 2;
if(selectedItem == SAVELISTSIZE-1)
selectedItem -= 1;
}
}
else if(saveBtn[selectedItem+2]->IsVisible())
{
selectedItem += 2;
}
}
2009-03-17 06:09:45 +01:00
else if(t->Up() || arrowUpBtn->GetState() == STATE_CLICKED)
2009-03-11 18:28:37 +01:00
{
if(selectedItem < 2)
{
if((listOffset - 2 >= 0 && action == 0) ||
2010-01-25 08:34:45 +01:00
(listOffset >= 0 && action == 1))
2009-03-11 18:28:37 +01:00
{
// move list up by 1
listOffset -= 2;
}
}
else
{
selectedItem -= 2;
}
}
endNavigation:
2009-05-28 09:08:25 +02:00
if(arrowDownBtn->GetState() == STATE_CLICKED)
arrowDownBtn->ResetState();
if(arrowUpBtn->GetState() == STATE_CLICKED)
arrowUpBtn->ResetState();
2009-03-11 18:28:37 +01:00
for(i=0; i<SAVELISTSIZE; i++)
{
if(listOffset+i < 0 && action == 1)
{
saveDate[0]->SetText(NULL);
saveDate[1]->SetText(NULL);
2010-03-15 08:41:40 +01:00
saveTime[0]->SetText("New");
saveTime[1]->SetText("New");
saveType[0]->SetText("SRAM");
saveType[1]->SetText("Snapshot");
2009-03-11 18:28:37 +01:00
savePreviewImg[0]->SetImage(gameSaveBlank);
savePreviewImg[1]->SetImage(gameSaveBlank);
savePreviewImg[0]->SetScale(1);
savePreviewImg[1]->SetScale(1);
saveBtn[0]->SetVisible(true);
saveBtn[1]->SetVisible(true);
if(saveBtn[0]->GetState() == STATE_DISABLED)
saveBtn[0]->SetState(STATE_DEFAULT);
if(saveBtn[1]->GetState() == STATE_DISABLED)
saveBtn[1]->SetState(STATE_DEFAULT);
}
else if(listOffset+i < saves->length)
{
2009-05-30 08:28:12 +02:00
if(saveBtn[i]->GetState() == STATE_DISABLED || !saveBtn[i]->IsVisible())
2009-03-11 18:28:37 +01:00
{
saveBtn[i]->SetVisible(true);
saveBtn[i]->SetState(STATE_DEFAULT);
}
saveDate[i]->SetText(saves->date[listOffset+i]);
saveTime[i]->SetText(saves->time[listOffset+i]);
if(saves->type[listOffset+i] == FILE_SRAM)
sprintf(savetext, "SRAM");
else
sprintf(savetext, "Snapshot");
len = strlen(saves->filename[listOffset+i]);
if(len > 10 &&
2009-03-16 07:58:50 +01:00
((saves->filename[listOffset+i][len-8] == 'A' &&
2009-03-11 18:28:37 +01:00
saves->filename[listOffset+i][len-7] == 'u' &&
saves->filename[listOffset+i][len-6] == 't' &&
2009-03-16 07:58:50 +01:00
saves->filename[listOffset+i][len-5] == 'o') ||
saves->filename[listOffset+i][len-5] == '0')
2009-03-11 18:28:37 +01:00
)
{
strcat(savetext, " (Auto)");
}
saveType[i]->SetText(savetext);
if(saves->previewImg[listOffset+i] != NULL)
{
savePreviewImg[i]->SetImage(saves->previewImg[listOffset+i]);
savePreviewImg[i]->SetScale(0.1);
}
else
{
savePreviewImg[i]->SetImage(gameSaveBlank);
savePreviewImg[i]->SetScale(1);
}
}
else
{
saveBtn[i]->SetVisible(false);
saveBtn[i]->SetState(STATE_DISABLED);
}
2009-05-30 08:28:12 +02:00
if(i != selectedItem && saveBtn[i]->GetState() == STATE_SELECTED)
saveBtn[i]->ResetState();
else if(focus && i == selectedItem && saveBtn[i]->GetState() == STATE_DEFAULT)
saveBtn[selectedItem]->SetState(STATE_SELECTED, t->chan);
2009-10-13 02:48:00 +02:00
if(t->wpad->ir.valid)
2009-03-11 18:28:37 +01:00
{
2009-10-13 02:48:00 +02:00
if(!saveBtnLastOver[i] && saveBtn[i]->IsInside(t->wpad->ir.x, t->wpad->ir.y))
2009-06-13 03:38:21 +02:00
saveBtn[i]->ResetState();
2009-10-13 02:48:00 +02:00
saveBtnLastOver[i] = saveBtn[i]->IsInside(t->wpad->ir.x, t->wpad->ir.y);
2009-03-11 18:28:37 +01:00
}
saveBtn[i]->Update(t);
if(saveBtn[i]->GetState() == STATE_SELECTED)
selectedItem = i;
}
if(updateCB)
updateCB(this);
}