From 0e03e6c96ae7b3e3e49d8b6831b7d71aa7917d01 Mon Sep 17 00:00:00 2001 From: Daryl Borth Date: Mon, 28 Sep 2020 16:46:30 -0600 Subject: [PATCH] add ability to change the player mapped to a connected controller --- source/input.cpp | 40 +++++++++------- source/input.h | 1 + source/menu.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 142 insertions(+), 21 deletions(-) diff --git a/source/input.cpp b/source/input.cpp index d449bfa..f88e55b 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -44,6 +44,7 @@ #define ANALOG_SENSITIVITY 30 int rumbleRequest[4] = {0,0,0,0}; +int playerMapping[4] = {0,1,2,3}; GuiTrigger userInput[4]; #ifdef HW_RVL @@ -517,7 +518,7 @@ static void UpdateCursorPosition (int chan, int &pos_x, int &pos_y) * Reads the changes (buttons pressed, etc) from a controller and reports * these changes to Snes9x ***************************************************************************/ -static void decodepad (int chan) +static void decodepad (int chan, int emuChan) { int i, offset; @@ -587,7 +588,7 @@ static void decodepad (int chan) #endif /*** Fix offset to pad ***/ - offset = ((chan + 1) << 4); + offset = ((emuChan + 1) << 4); /*** Report pressed buttons (gamepads) ***/ for (i = 0; i < MAXJP; i++) @@ -607,7 +608,7 @@ static void decodepad (int chan) } /*** Superscope ***/ - if (Settings.SuperScopeMaster && chan == 0) // report only once + if (Settings.SuperScopeMaster && emuChan == 0) // report only once { // buttons offset = 0x50; @@ -643,14 +644,14 @@ static void decodepad (int chan) } // pointer offset = 0x80; - UpdateCursorPosition(chan, cursor_x[0], cursor_y[0]); + UpdateCursorPosition(emuChan, cursor_x[0], cursor_y[0]); S9xReportPointer(offset, (u16) cursor_x[0], (u16) cursor_y[0]); } /*** Mouse ***/ - else if (Settings.MouseMaster && chan == 0) + else if (Settings.MouseMaster && emuChan == 0) { // buttons - offset = 0x60 + (2 * chan); + offset = 0x60 + (2 * emuChan); for (i = 0; i < 2; i++) { if (jp & btnmap[CTRL_MOUSE][CTRLR_GCPAD][i] @@ -667,15 +668,15 @@ static void decodepad (int chan) } // pointer offset = 0x81; - UpdateCursorPosition(chan, cursor_x[1 + chan], cursor_y[1 + chan]); - S9xReportPointer(offset + chan, (u16) cursor_x[1 + chan], - (u16) cursor_y[1 + chan]); + UpdateCursorPosition(emuChan, cursor_x[1 + emuChan], cursor_y[1 + emuChan]); + S9xReportPointer(offset + emuChan, (u16) cursor_x[1 + emuChan], + (u16) cursor_y[1 + emuChan]); } /*** Justifier ***/ - else if (Settings.JustifierMaster && chan < 2) + else if (Settings.JustifierMaster && emuChan < 2) { // buttons - offset = 0x70 + (3 * chan); + offset = 0x70 + (3 * emuChan); for (i = 0; i < 3; i++) { if (jp & btnmap[CTRL_JUST][CTRLR_GCPAD][i] @@ -692,9 +693,9 @@ static void decodepad (int chan) } // pointer offset = 0x83; - UpdateCursorPosition(chan, cursor_x[3 + chan], cursor_y[3 + chan]); - S9xReportPointer(offset + chan, (u16) cursor_x[3 + chan], - (u16) cursor_y[3 + chan]); + UpdateCursorPosition(emuChan, cursor_x[3 + emuChan], cursor_y[3 + emuChan]); + S9xReportPointer(offset + emuChan, (u16) cursor_x[3 + emuChan], + (u16) cursor_y[3 + emuChan]); } #ifdef HW_RVL @@ -736,7 +737,7 @@ bool MenuRequested() ***************************************************************************/ void ReportButtons () { - int i, j; + int i; UpdatePads(); @@ -762,10 +763,13 @@ void ReportButtons () if(MenuRequested()) ScreenshotRequested = 1; // go to the menu - j = (Settings.MultiPlayer5Master == true ? 4 : 2); + int numControllers = (Settings.MultiPlayer5Master == true ? 4 : 2); - for (i = 0; i < j; i++) - decodepad (i); + for (i = 0; i < 4; i++) { + if(playerMapping[i] < numControllers) { + decodepad (i, playerMapping[i]); + } + } } void SetControllers() diff --git a/source/input.h b/source/input.h index 2348045..f53bff8 100644 --- a/source/input.h +++ b/source/input.h @@ -25,6 +25,7 @@ extern u32 btnmap[4][6][12]; extern int rumbleRequest[4]; +extern int playerMapping[4]; void ResetControls(int cc = -1, int wc = -1); void ShutoffRumble(); diff --git a/source/menu.cpp b/source/menu.cpp index d33bd5e..43284a0 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -68,6 +68,9 @@ static GuiTrigger * trigA = NULL; static GuiTrigger * trig2 = NULL; static GuiButton * btnLogo = NULL; +#ifdef HW_RVL +static GuiButton * batteryBtn[4]; +#endif static GuiImageData * gameScreen = NULL; static GuiImage * gameScreenImg = NULL; static GuiImage * bgTopImg = NULL; @@ -1256,6 +1259,97 @@ static void ControllerWindow() delete(settingText); } +#ifdef HW_RVL +static int playerMappingChan = 0; + +static void PlayerMappingWindowUpdate(void * ptr, int dir) +{ + GuiButton * b = (GuiButton *)ptr; + if(b->GetState() == STATE_CLICKED) + { + playerMapping[playerMappingChan] += dir; + + if(playerMapping[playerMappingChan] > 3) + playerMapping[playerMappingChan] = 0; + if(playerMapping[playerMappingChan] < 0) + playerMapping[playerMappingChan] = 3; + + char playerNumber[20]; + sprintf(playerNumber, "Player %d", playerMapping[playerMappingChan]+1); + + settingText->SetText(playerNumber); + b->ResetState(); + } +} + +static void PlayerMappingWindowLeftClick(void * ptr) { PlayerMappingWindowUpdate(ptr, -1); } +static void PlayerMappingWindowRightClick(void * ptr) { PlayerMappingWindowUpdate(ptr, +1); } + +static void PlayerMappingWindow(int chan) +{ + playerMappingChan = chan; + + GuiWindow * w = new GuiWindow(300,250); + w->SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE); + + GuiTrigger trigLeft; + trigLeft.SetButtonOnlyInFocusTrigger(-1, WPAD_BUTTON_LEFT | WPAD_CLASSIC_BUTTON_LEFT, PAD_BUTTON_LEFT, WIIDRC_BUTTON_LEFT); + + GuiTrigger trigRight; + trigRight.SetButtonOnlyInFocusTrigger(-1, WPAD_BUTTON_RIGHT | WPAD_CLASSIC_BUTTON_RIGHT, PAD_BUTTON_RIGHT, WIIDRC_BUTTON_RIGHT); + + GuiImageData arrowLeft(button_arrow_left_png); + GuiImage arrowLeftImg(&arrowLeft); + GuiImageData arrowLeftOver(button_arrow_left_over_png); + GuiImage arrowLeftOverImg(&arrowLeftOver); + GuiButton arrowLeftBtn(arrowLeft.GetWidth(), arrowLeft.GetHeight()); + arrowLeftBtn.SetImage(&arrowLeftImg); + arrowLeftBtn.SetImageOver(&arrowLeftOverImg); + arrowLeftBtn.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); + arrowLeftBtn.SetTrigger(trigA); + arrowLeftBtn.SetTrigger(trig2); + arrowLeftBtn.SetTrigger(&trigLeft); + arrowLeftBtn.SetSelectable(false); + arrowLeftBtn.SetUpdateCallback(PlayerMappingWindowLeftClick); + + GuiImageData arrowRight(button_arrow_right_png); + GuiImage arrowRightImg(&arrowRight); + GuiImageData arrowRightOver(button_arrow_right_over_png); + GuiImage arrowRightOverImg(&arrowRightOver); + GuiButton arrowRightBtn(arrowRight.GetWidth(), arrowRight.GetHeight()); + arrowRightBtn.SetImage(&arrowRightImg); + arrowRightBtn.SetImageOver(&arrowRightOverImg); + arrowRightBtn.SetAlignment(ALIGN_RIGHT, ALIGN_MIDDLE); + arrowRightBtn.SetTrigger(trigA); + arrowRightBtn.SetTrigger(trig2); + arrowRightBtn.SetTrigger(&trigRight); + arrowRightBtn.SetSelectable(false); + arrowRightBtn.SetUpdateCallback(PlayerMappingWindowRightClick); + + char playerNumber[20]; + sprintf(playerNumber, "Player %d", playerMapping[playerMappingChan]+1); + + settingText = new GuiText(playerNumber, 22, (GXColor){0, 0, 0, 255}); + + int currentController = GCSettings.Controller; + + w->Append(&arrowLeftBtn); + w->Append(&arrowRightBtn); + w->Append(settingText); + + char title[50]; + sprintf(title, "Player Mapping - Controller %d", chan+1); + + int previousPlayerMapping = playerMapping[playerMappingChan]; + + if(!SettingWindow(title,w)) + playerMapping[playerMappingChan] = previousPlayerMapping; // undo changes + + delete(w); + delete(settingText); +} +#endif + /**************************************************************************** * MenuGame * @@ -1420,7 +1514,6 @@ static int MenuGame() GuiText * batteryTxt[4]; GuiImage * batteryImg[4]; GuiImage * batteryBarImg[4]; - GuiButton * batteryBtn[4]; for(i=0; i < 4; i++) { @@ -1441,11 +1534,14 @@ static int MenuGame() batteryBtn[i]->SetImage(batteryImg[i]); batteryBtn[i]->SetIcon(batteryBarImg[i]); batteryBtn[i]->SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM); - batteryBtn[i]->SetRumble(false); + batteryBtn[i]->SetTrigger(trigA); + batteryBtn[i]->SetSoundOver(&btnSoundOver); + batteryBtn[i]->SetSoundClick(&btnSoundClick); batteryBtn[i]->SetSelectable(false); + batteryBtn[i]->SetState(STATE_DISABLED); batteryBtn[i]->SetAlpha(150); } - + batteryBtn[0]->SetPosition(45, -65); batteryBtn[1]->SetPosition(135, -65); batteryBtn[2]->SetPosition(45, -40); @@ -1523,6 +1619,7 @@ static int MenuGame() if(newStatus == true) // controller connected { batteryBtn[i]->SetAlpha(255); + batteryBtn[i]->SetState(STATE_DEFAULT); batteryBarImg[i]->SetTile(newLevel); if(newLevel == 0) @@ -1533,6 +1630,7 @@ static int MenuGame() else // controller not connected { batteryBtn[i]->SetAlpha(150); + batteryBtn[i]->SetState(STATE_DISABLED); batteryBarImg[i]->SetTile(0); batteryImg[i]->SetImage(&battery); } @@ -1566,6 +1664,24 @@ static int MenuGame() { menu = MENU_GAMESETTINGS; } +#ifdef HW_RVL + else if(batteryBtn[0]->GetState() == STATE_CLICKED) + { + PlayerMappingWindow(0); + } + else if(batteryBtn[1]->GetState() == STATE_CLICKED) + { + PlayerMappingWindow(1); + } + else if(batteryBtn[2]->GetState() == STATE_CLICKED) + { + PlayerMappingWindow(2); + } + else if(batteryBtn[3]->GetState() == STATE_CLICKED) + { + PlayerMappingWindow(3); + } +#endif else if(mainmenuBtn.GetState() == STATE_CLICKED) { if (WindowPrompt("Quit Game", "Quit this game? Any unsaved progress will be lost.", "OK", "Cancel"))