usbloadergx/source/libwiigui/gui_filebrowser.cpp

457 lines
15 KiB
C++
Raw Normal View History

/****************************************************************************
* libwiigui
*
* Tantric 2009
*
* gui_filebrowser.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
#include "prompts/filebrowser.h"
#include "settings/CSettings.h"
#include "themes/CTheme.h"
/**
* Constructor for the GuiFileBrowser class.
*/
2010-09-24 02:48:03 +02:00
GuiFileBrowser::GuiFileBrowser(int w, int h)
{
width = w;
height = h;
selectedItem = 0;
selectable = true;
listChanged = true; // trigger an initial list update
triggerdisabled = false; // trigger disable
focus = 0; // allow focus
trigA = new GuiTrigger;
2010-09-24 02:48:03 +02:00
trigA->SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
trigHeldA = new GuiTrigger;
2010-09-24 02:48:03 +02:00
trigHeldA->SetHeldTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
bgFileSelection = new GuiImageData(Resources::GetFile("bg_browser.png"), Resources::GetFileSize("bg_browser.png"));
2010-09-24 02:48:03 +02:00
bgFileSelectionImg = new GuiImage(bgFileSelection);
bgFileSelectionImg->SetParent(this);
bgFileSelectionImg->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
bgFileSelectionEntry = Resources::GetImageData("bg_browser_selection.png");
fileFolder = Resources::GetImageData("icon_folder.png");
scrollbar = Resources::GetImageData("scrollbar.png");
2010-09-24 02:48:03 +02:00
scrollbarImg = new GuiImage(scrollbar);
scrollbarImg->SetParent(this);
scrollbarImg->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
scrollbarImg->SetPosition(0, 2);
scrollbarImg->SetSkew(0, 0, 0, 0, 0, -30, 0, -30);
arrowDown = Resources::GetImageData("scrollbar_arrowdown.png");
2010-09-24 02:48:03 +02:00
arrowDownImg = new GuiImage(arrowDown);
arrowUp = Resources::GetImageData("scrollbar_arrowup.png");
2010-09-24 02:48:03 +02:00
arrowUpImg = new GuiImage(arrowUp);
scrollbarBox = Resources::GetImageData("scrollbar_box.png");
2010-09-24 02:48:03 +02:00
scrollbarBoxImg = new GuiImage(scrollbarBox);
arrowUpBtn = new GuiButton(arrowUpImg->GetWidth(), arrowUpImg->GetHeight());
arrowUpBtn->SetParent(this);
arrowUpBtn->SetImage(arrowUpImg);
arrowUpBtn->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
arrowUpBtn->SetPosition(12, -12);
arrowUpBtn->SetSelectable(false);
arrowUpBtn->SetClickable(false);
arrowUpBtn->SetHoldable(true);
arrowUpBtn->SetTrigger(trigHeldA);
arrowUpBtn->SetSoundOver(btnSoundOver);
arrowUpBtn->SetSoundClick(btnSoundClick);
arrowDownBtn = new GuiButton(arrowDownImg->GetWidth(), arrowDownImg->GetHeight());
arrowDownBtn->SetParent(this);
arrowDownBtn->SetImage(arrowDownImg);
arrowDownBtn->SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
arrowDownBtn->SetPosition(12, 12);
arrowDownBtn->SetSelectable(false);
arrowDownBtn->SetClickable(false);
arrowDownBtn->SetHoldable(true);
arrowDownBtn->SetTrigger(trigHeldA);
arrowDownBtn->SetSoundOver(btnSoundOver);
arrowDownBtn->SetSoundClick(btnSoundClick);
scrollbarBoxBtn = new GuiButton(scrollbarBoxImg->GetWidth(), scrollbarBoxImg->GetHeight());
scrollbarBoxBtn->SetParent(this);
scrollbarBoxBtn->SetImage(scrollbarBoxImg);
scrollbarBoxBtn->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
scrollbarBoxBtn->SetPosition(-10, 0);
scrollbarBoxBtn->SetMinY(-10);
scrollbarBoxBtn->SetMaxY(156);
scrollbarBoxBtn->SetSelectable(false);
scrollbarBoxBtn->SetClickable(false);
scrollbarBoxBtn->SetHoldable(true);
scrollbarBoxBtn->SetTrigger(trigHeldA);
for (int i = 0; i < FILEBROWSERSIZE; i++)
{
2010-09-24 02:48:03 +02:00
fileListText[i] = new GuiText((char *) NULL, 20, ( GXColor )
{ 0, 0, 0, 0xff});
fileListText[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
fileListText[i]->SetPosition(5, 0);
fileListText[i]->SetMaxWidth(bgFileSelectionImg->GetWidth() - (arrowDownImg->GetWidth() + 20), DOTTED);
fileListTextOver[i] = new GuiText((char *) NULL, 20, ( GXColor )
{ 0, 0, 0, 0xff});
fileListTextOver[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
fileListTextOver[i]->SetPosition(5, 0);
fileListTextOver[i]->SetMaxWidth(bgFileSelectionImg->GetWidth() - (arrowDownImg->GetWidth() + 20),
SCROLL_HORIZONTAL);
fileListBg[i] = new GuiImage(bgFileSelectionEntry);
//fileListArchives[i] = new GuiImage(fileArchives);
//fileListDefault[i] = new GuiImage(fileDefault);
2010-09-24 02:48:03 +02:00
fileListFolder[i] = new GuiImage(fileFolder);
//fileListGFX[i] = new GuiImage(fileGFX);
//fileListPLS[i] = new GuiImage(filePLS);
//fileListSFX[i] = new GuiImage(fileSFX);
//fileListTXT[i] = new GuiImage(fileTXT);
//fileListXML[i] = new GuiImage(fileXML);
2010-09-24 02:48:03 +02:00
fileList[i] = new GuiButton(350, 30);
fileList[i]->SetParent(this);
fileList[i]->SetLabel(fileListText[i]);
fileList[i]->SetLabelOver(fileListTextOver[i]);
fileList[i]->SetImageOver(fileListBg[i]);
fileList[i]->SetPosition(2, 30 * i + 3);
fileList[i]->SetTrigger(trigA);
fileList[i]->SetRumble(false);
fileList[i]->SetSoundClick(btnSoundClick);
}
}
/**
* Destructor for the GuiFileBrowser class.
*/
GuiFileBrowser::~GuiFileBrowser()
{
delete arrowUpBtn;
delete arrowDownBtn;
delete scrollbarBoxBtn;
delete bgFileSelectionImg;
delete scrollbarImg;
delete arrowDownImg;
delete arrowUpImg;
delete scrollbarBoxImg;
delete bgFileSelection;
delete bgFileSelectionEntry;
//delete fileArchives;
//delete fileDefault;
delete fileFolder;
//delete fileGFX;
//delete filePLS;
//delete fileSFX;
//delete fileTXT;
//delete fileXML;
delete scrollbar;
delete arrowDown;
delete arrowUp;
delete scrollbarBox;
delete trigHeldA;
delete trigA;
2010-09-24 02:48:03 +02:00
for (int i = 0; i < FILEBROWSERSIZE; i++)
{
delete fileListText[i];
delete fileListTextOver[i];
delete fileList[i];
delete fileListBg[i];
//delete fileListArchives[i];
//delete fileListDefault[i];
delete fileListFolder[i];
//delete fileListGFX[i];
//delete fileListPLS[i];
//delete fileListSFX[i];
//delete fileListTXT[i];
//delete fileListXML[i];
}
}
2010-09-24 02:48:03 +02:00
void GuiFileBrowser::SetFocus(int f)
{
LOCK( this );
focus = f;
2010-09-24 02:48:03 +02:00
for (int i = 0; i < FILEBROWSERSIZE; i++)
fileList[i]->ResetState();
2010-09-24 02:48:03 +02:00
if (f == 1) fileList[selectedItem]->SetState(STATE_SELECTED);
}
2010-09-24 02:48:03 +02:00
void GuiFileBrowser::DisableTriggerUpdate(bool set)
{
LOCK( this );
triggerdisabled = set;
}
void GuiFileBrowser::ResetState()
{
LOCK( this );
state = STATE_DEFAULT;
stateChan = -1;
selectedItem = 0;
2010-09-24 02:48:03 +02:00
for (int i = 0; i < FILEBROWSERSIZE; i++)
{
fileList[i]->ResetState();
}
}
void GuiFileBrowser::TriggerUpdate()
{
LOCK( this );
listChanged = true;
}
/**
* Draw the button on screen
*/
void GuiFileBrowser::Draw()
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (!this->IsVisible()) return;
bgFileSelectionImg->Draw();
2010-09-24 02:48:03 +02:00
for (int i = 0; i < FILEBROWSERSIZE; i++)
{
fileList[i]->Draw();
}
scrollbarImg->Draw();
arrowUpBtn->Draw();
arrowDownBtn->Draw();
scrollbarBoxBtn->Draw();
this->UpdateEffects();
}
2010-09-24 02:48:03 +02:00
void GuiFileBrowser::Update(GuiTrigger * t)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (state == STATE_DISABLED || !t || triggerdisabled) return;
int position = 0;
int positionWiimote = 0;
2010-09-24 02:48:03 +02:00
arrowUpBtn->Update(t);
arrowDownBtn->Update(t);
scrollbarBoxBtn->Update(t);
// move the file listing to respond to wiimote cursor movement
2010-09-24 02:48:03 +02:00
if (scrollbarBoxBtn->GetState() == STATE_HELD && scrollbarBoxBtn->GetStateChan() == t->chan && t->wpad.ir.valid
&& browser->browserList.size() > FILEBROWSERSIZE)
{
2010-09-24 02:48:03 +02:00
scrollbarBoxBtn->SetPosition(20, -10);
positionWiimote = t->wpad.ir.y - 60 - scrollbarBoxBtn->GetTop();
2010-09-24 02:48:03 +02:00
if (positionWiimote < scrollbarBoxBtn->GetMinY())
positionWiimote = scrollbarBoxBtn->GetMinY();
2010-09-24 02:48:03 +02:00
else if (positionWiimote > scrollbarBoxBtn->GetMaxY()) positionWiimote = scrollbarBoxBtn->GetMaxY();
2010-09-24 02:48:03 +02:00
browser->pageIndex = (positionWiimote * browser->browserList.size()) / 136.0 - selectedItem;
2010-09-24 02:48:03 +02:00
if (browser->pageIndex <= 0)
{
browser->pageIndex = 0;
}
2010-09-24 02:48:03 +02:00
else if (browser->pageIndex + FILEBROWSERSIZE >= (int) browser->browserList.size())
{
browser->pageIndex = browser->browserList.size() - FILEBROWSERSIZE;
}
listChanged = true;
focus = false;
}
2010-09-24 02:48:03 +02:00
if (arrowDownBtn->GetState() == STATE_HELD && arrowDownBtn->GetStateChan() == t->chan)
{
t->wpad.btns_h |= WPAD_BUTTON_DOWN;
2010-09-24 02:48:03 +02:00
if (!this->IsFocused()) ((GuiWindow *) this->GetParent())->ChangeFocus(this);
}
2010-09-24 02:48:03 +02:00
else if (arrowUpBtn->GetState() == STATE_HELD && arrowUpBtn->GetStateChan() == t->chan)
{
t->wpad.btns_h |= WPAD_BUTTON_UP;
2010-09-24 02:48:03 +02:00
if (!this->IsFocused()) ((GuiWindow *) this->GetParent())->ChangeFocus(this);
}
/* // pad/joystick navigation
2010-09-24 02:48:03 +02:00
if(!focus)
{
goto endNavigation; // skip navigation
listChanged = false;
}
*/
if (t->Right())
{
2010-09-24 02:48:03 +02:00
if (browser->pageIndex < (int) browser->browserList.size() && browser->browserList.size() > FILEBROWSERSIZE)
{
browser->pageIndex += FILEBROWSERSIZE;
2010-09-24 02:48:03 +02:00
if (browser->pageIndex + FILEBROWSERSIZE >= (int) browser->browserList.size()) browser->pageIndex
= browser->browserList.size() - FILEBROWSERSIZE;
listChanged = true;
}
}
2010-09-24 02:48:03 +02:00
else if (t->Left())
{
2010-09-24 02:48:03 +02:00
if (browser->pageIndex > 0)
{
browser->pageIndex -= FILEBROWSERSIZE;
2010-09-24 02:48:03 +02:00
if (browser->pageIndex < 0) browser->pageIndex = 0;
listChanged = true;
}
}
2010-09-24 02:48:03 +02:00
else if (t->Down())
{
2010-09-24 02:48:03 +02:00
if (browser->pageIndex + selectedItem + 1 < (int) browser->browserList.size())
{
2010-09-24 02:48:03 +02:00
if (selectedItem == FILEBROWSERSIZE - 1)
{
// move list down by 1
browser->pageIndex++;
listChanged = true;
}
2010-09-24 02:48:03 +02:00
else if (fileList[selectedItem + 1]->IsVisible())
{
fileList[selectedItem]->ResetState();
2010-09-24 02:48:03 +02:00
fileList[++selectedItem]->SetState(STATE_SELECTED, t->chan);
}
}
}
2010-09-24 02:48:03 +02:00
else if (t->Up())
{
2010-09-24 02:48:03 +02:00
if (selectedItem == 0 && browser->pageIndex + selectedItem > 0)
{
// move list up by 1
browser->pageIndex--;
listChanged = true;
}
2010-09-24 02:48:03 +02:00
else if (selectedItem > 0)
{
fileList[selectedItem]->ResetState();
2010-09-24 02:48:03 +02:00
fileList[--selectedItem]->SetState(STATE_SELECTED, t->chan);
}
}
//endNavigation:
2010-09-24 02:48:03 +02:00
for (int i = 0; i < FILEBROWSERSIZE; i++)
{
2010-09-24 02:48:03 +02:00
if (listChanged)
{
bool haveselected = false;
2010-09-24 02:48:03 +02:00
if (browser->pageIndex + i < (int) browser->browserList.size())
{
2010-09-24 02:48:03 +02:00
if (fileList[i]->GetState() == STATE_DISABLED) fileList[i]->SetState(STATE_DEFAULT);
2010-09-24 02:48:03 +02:00
if (fileList[i]->GetState() == STATE_SELECTED) haveselected = true;
2010-09-24 02:48:03 +02:00
fileList[i]->SetVisible(true);
2010-09-24 02:48:03 +02:00
fileListText[i]->SetText(browser->browserList[browser->pageIndex + i].displayname);
fileListTextOver[i]->SetText(browser->browserList[browser->pageIndex + i].displayname);
2010-09-24 02:48:03 +02:00
if (browser->browserList[browser->pageIndex + i].isdir) // directory
{
2010-09-24 02:48:03 +02:00
fileList[i]->SetIcon(fileListFolder[i]);
fileListText[i]->SetPosition(30, 0);
fileListTextOver[i]->SetPosition(30, 0);
}
else
{
/*
2010-09-24 02:48:03 +02:00
char *fileext = strrchr(browserList[browser.pageIndex+i].displayname, '.');
fileListText[i]->SetPosition(32,0);
fileListTextOver[i]->SetPosition(32,0);
if(fileext)
{
if(!strcasecmp(fileext, ".png") || !strcasecmp(fileext, ".jpg") || !strcasecmp(fileext, ".jpeg") ||
!strcasecmp(fileext, ".gif") || !strcasecmp(fileext, ".tga") || !strcasecmp(fileext, ".tpl") ||
!strcasecmp(fileext, ".bmp")) {
fileList[i]->SetIcon(fileListGFX[i]);
} else if(!strcasecmp(fileext, ".mp3") || !strcasecmp(fileext, ".ogg") || !strcasecmp(fileext, ".flac") ||
!strcasecmp(fileext, ".mpc") || !strcasecmp(fileext, ".m4a") || !strcasecmp(fileext, ".wav")) {
fileList[i]->SetIcon(fileListSFX[i]);
} else if(!strcasecmp(fileext, ".pls") || !strcasecmp(fileext, ".m3u")) {
fileList[i]->SetIcon(fileListPLS[i]);
} else if(!strcasecmp(fileext, ".txt")) {
fileList[i]->SetIcon(fileListTXT[i]);
} else if(!strcasecmp(fileext, ".xml")) {
fileList[i]->SetIcon(fileListXML[i]);
} else if(!strcasecmp(fileext, ".rar") || !strcasecmp(fileext, ".zip") ||
!strcasecmp(fileext, ".gz") || !strcasecmp(fileext, ".7z")) {
fileList[i]->SetIcon(fileListArchives[i]);
} else {
fileList[i]->SetIcon(fileListDefault[i]);
}
} else {
fileList[i]->SetIcon(fileListDefault[i]);
}
*/
fileList[i]->SetIcon(NULL);
fileListText[i]->SetPosition(10, 0);
fileListTextOver[i]->SetPosition(10, 0);
}
}
else
{
2010-09-24 02:48:03 +02:00
fileList[i]->SetVisible(false);
fileList[i]->SetState(STATE_DISABLED);
}
2010-09-24 02:48:03 +02:00
if (!haveselected && browser->pageIndex < (int) browser->browserList.size()) fileList[i]->SetState(
STATE_SELECTED, t->chan);
}
2010-09-24 02:48:03 +02:00
if (i != selectedItem && fileList[i]->GetState() == STATE_SELECTED)
fileList[i]->ResetState();
2010-09-24 02:48:03 +02:00
else if (focus && i == selectedItem && fileList[i]->GetState() == STATE_DEFAULT) fileList[selectedItem]->SetState(
STATE_SELECTED, t->chan);
int currChan = t->chan;
2010-09-24 02:48:03 +02:00
if (t->wpad.ir.valid && !fileList[i]->IsInside(t->wpad.ir.x, t->wpad.ir.y)) t->chan = -1;
2010-09-24 02:48:03 +02:00
fileList[i]->Update(t);
t->chan = currChan;
2010-09-24 02:48:03 +02:00
if (fileList[i]->GetState() == STATE_SELECTED)
{
selectedItem = i;
}
}
// update the location of the scroll box based on the position in the file list
2010-09-24 02:48:03 +02:00
if (positionWiimote > 0)
{
position = positionWiimote; // follow wiimote cursor
}
else
{
2010-09-24 02:48:03 +02:00
position = 136 * (browser->pageIndex + FILEBROWSERSIZE / 2.0) / (browser->browserList.size() * 1.0);
2010-09-24 02:48:03 +02:00
if (browser->pageIndex / (FILEBROWSERSIZE / 2.0) < 1)
position = -10;
2010-09-24 02:48:03 +02:00
else if ((browser->pageIndex + FILEBROWSERSIZE) / (FILEBROWSERSIZE * 1.0) >= (browser->browserList.size())
/ (FILEBROWSERSIZE * 1.0)) position = 156;
}
2010-09-24 02:48:03 +02:00
scrollbarBoxBtn->SetPosition(12, position + 26);
listChanged = false;
2010-09-24 02:48:03 +02:00
if (updateCB) updateCB(this);
}