usbloadergx/source/cheats/cheatmenu.cpp

184 lines
6.6 KiB
C++
Raw Normal View History

#include <string.h>
#include <unistd.h>
#include "libfat/fat.h"
#include "libwiigui/gui.h"
#include "libwiigui/gui_customoptionbrowser.h"
#include "prompts/PromptWindows.h"
#include "language/gettext.h"
#include "themes/CTheme.h"
#include "fatmounter.h"
#include "listfiles.h"
#include "menu.h"
#include "filelist.h"
#include "sys.h"
#include "gct.h"
/*** Extern functions ***/
extern void ResumeGui();
extern void HaltGui();
/*** Extern variables ***/
extern GuiWindow * mainWindow;
/****************************************************************************
* CheatMenu
***************************************************************************/
2010-09-24 02:48:03 +02:00
int CheatMenu(const char * gameID)
{
int choice = 0;
bool exit = false;
int ret = 1;
// because destroy GuiSound must wait while sound playing is finished, we use a global sound
2010-09-24 02:48:03 +02:00
if (!btnClick2) btnClick2 = new GuiSound(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume);
// GuiSound btnClick(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume);
char imgPath[100];
2010-09-24 02:48:03 +02:00
snprintf(imgPath, sizeof(imgPath), "%sbutton_dialogue_box.png", Settings.theme_path);
GuiImageData btnOutline(imgPath, button_dialogue_box_png);
snprintf(imgPath, sizeof(imgPath), "%ssettings_background.png", Settings.theme_path);
GuiImageData settingsbg(imgPath, settings_background_png);
GuiImage settingsbackground(&settingsbg);
GuiTrigger trigA;
2010-09-24 02:48:03 +02:00
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
GuiTrigger trigB;
2010-09-24 02:48:03 +02:00
trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B);
GuiText backBtnTxt(tr( "Back" ), 22, Theme.prompttext);
2010-09-24 02:48:03 +02:00
backBtnTxt.SetMaxWidth(btnOutline.GetWidth() - 30);
GuiImage backBtnImg(&btnOutline);
GuiButton backBtn(&backBtnImg, &backBtnImg, 2, 3, -140, 400, &trigA, NULL, btnClick2, 1);
backBtn.SetLabel(&backBtnTxt);
backBtn.SetTrigger(&trigB);
GuiText createBtnTxt(tr( "Create" ), 22, Theme.prompttext);
2010-09-24 02:48:03 +02:00
createBtnTxt.SetMaxWidth(btnOutline.GetWidth() - 30);
GuiImage createBtnImg(&btnOutline);
GuiButton createBtn(&createBtnImg, &createBtnImg, 2, 3, 160, 400, &trigA, NULL, btnClick2, 1);
createBtn.SetLabel(&createBtnTxt);
char txtfilename[55];
2010-09-24 02:48:03 +02:00
snprintf(txtfilename, sizeof(txtfilename), "%s%s.txt", Settings.TxtCheatcodespath, gameID);
GCTCheats c;
2010-09-24 02:48:03 +02:00
int check = c.openTxtfile(txtfilename);
int download = 0;
2010-09-24 02:48:03 +02:00
switch (check)
{
case -1:
2010-09-24 02:48:03 +02:00
WindowPrompt(tr( "Error" ), tr( "Cheatfile is blank" ), tr( "OK" ));
break;
case 0:
2010-09-24 02:48:03 +02:00
download = WindowPrompt(tr( "Error" ), tr( "No Cheatfile found" ), tr( "Download Now" ), tr( "Cancel" ));
if (download == 1)
{
2010-09-24 02:48:03 +02:00
download = CodeDownload(gameID);
if (download < 0 || c.openTxtfile(txtfilename) != 1) break;
}
2010-09-24 02:48:03 +02:00
else break;
case 1:
int cntcheats = c.getCnt();
2010-09-24 02:48:03 +02:00
customOptionList cheatslst(cntcheats);
GuiCustomOptionBrowser chtBrowser(400, 280, &cheatslst, Settings.theme_path, "bg_options_settings.png",
bg_options_settings_png, 1, 90);
chtBrowser.SetPosition(0, 90);
chtBrowser.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
chtBrowser.SetClickable(true);
GuiText titleTxt(c.getGameName().c_str(), 28, ( GXColor )
{ 0, 0, 0, 255});
titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
titleTxt.SetMaxWidth(350, SCROLL_HORIZONTAL);
titleTxt.SetPosition(12, 40);
for (int i = 0; i <= cntcheats; i++)
{
2010-09-24 02:48:03 +02:00
cheatslst.SetValue(i, "%s", c.getCheatName(i).c_str());
cheatslst.SetName(i, "OFF");
}
HaltGui();
2010-09-24 02:48:03 +02:00
GuiWindow w(screenwidth, screenheight);
w.Append(&settingsbackground);
w.Append(&titleTxt);
w.Append(&backBtn);
w.Append(&createBtn);
w.Append(&chtBrowser);
mainWindow->SetState(STATE_DISABLED);
mainWindow->ChangeFocus(&w);
mainWindow->Append(&w);
ResumeGui();
2010-09-24 02:48:03 +02:00
while (!exit)
{
2010-09-24 02:48:03 +02:00
VIDEO_WaitVSync();
ret = chtBrowser.GetClickedOption();
2010-09-24 02:48:03 +02:00
if (ret != -1)
{
2010-09-24 02:48:03 +02:00
const char *strCheck = cheatslst.GetName(ret);
if (strncmp(strCheck, "ON", 2) == 0)
{
2010-09-24 02:48:03 +02:00
cheatslst.SetName(ret, "%s", "OFF");
}
2010-09-24 02:48:03 +02:00
else if (strncmp(strCheck, "OFF", 3) == 0)
{
2010-09-24 02:48:03 +02:00
cheatslst.SetName(ret, "%s", "ON");
}
}
2010-09-24 02:48:03 +02:00
if (createBtn.GetState() == STATE_CLICKED)
{
createBtn.ResetState();
2010-09-24 02:48:03 +02:00
if (cntcheats > 0)
{
int selectednrs[30];
int x = 0;
2010-09-24 02:48:03 +02:00
for (int i = 0; i <= cntcheats; i++)
{
2010-09-24 02:48:03 +02:00
const char *strCheck = cheatslst.GetName(i);
if (strncmp(strCheck, "ON", 2) == 0)
{
selectednrs[x] = i;
x++;
}
}
2010-09-24 02:48:03 +02:00
if (x == 0)
{
2010-09-24 02:48:03 +02:00
WindowPrompt(tr( "Error" ), tr( "No cheats were selected" ), tr( "OK" ));
}
else
{
2010-09-24 02:48:03 +02:00
subfoldercreate(Settings.Cheatcodespath);
string chtpath = Settings.Cheatcodespath;
string gctfname = chtpath + c.getGameID() + ".gct";
2010-09-24 02:48:03 +02:00
c.createGCT(selectednrs, x, gctfname.c_str());
WindowPrompt(tr( "GCT File created" ), NULL, tr( "OK" ));
exit = true;
break;
}
}
2010-09-24 02:48:03 +02:00
else WindowPrompt(tr( "Error" ), tr( "Could not create GCT file" ), tr( "OK" ));
}
2010-09-24 02:48:03 +02:00
if (backBtn.GetState() == STATE_CLICKED)
{
backBtn.ResetState();
exit = true;
break;
}
}
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->SetState(STATE_DEFAULT);
mainWindow->Remove(&w);
ResumeGui();
break;
}
return choice;
}