From c539bae44fe190fe75b27e47f8c36f93ba848ec2 Mon Sep 17 00:00:00 2001 From: Josh Bodner <30329717+jbodner09@users.noreply.github.com> Date: Sun, 9 Oct 2022 23:28:19 -0700 Subject: [PATCH] Add support for extra buttons to be used as walk speed modifiers (#449) --- libultraship/InputEditor.cpp | 113 +++++++++--------- libultraship/InputEditor.h | 3 +- libultraship/KeyboardController.cpp | 3 +- .../Lib/Fast3D/U64/PR/ultra64/controller.h | 2 + libultraship/SDLController.cpp | 2 + libultraship/UltraController.h | 2 + 6 files changed, 69 insertions(+), 56 deletions(-) diff --git a/libultraship/InputEditor.cpp b/libultraship/InputEditor.cpp index 156eafae312..5122e3d62df 100644 --- a/libultraship/InputEditor.cpp +++ b/libultraship/InputEditor.cpp @@ -20,12 +20,12 @@ namespace Ship { return controlDeck->GetPhysicalDeviceFromVirtualSlot(slot); } - void InputEditor::DrawButton(const char* label, int32_t n64Btn) { - const std::shared_ptr backend = GetControllerPerSlot(CurrentPort); + void InputEditor::DrawButton(const char* label, int32_t n64Btn, int32_t currentPort, int32_t* btnReading) { + const std::shared_ptr backend = GetControllerPerSlot(currentPort); float size = 40; - bool readingMode = BtnReading == n64Btn; - bool disabled = (BtnReading != -1 && !readingMode) || !backend->Connected() || backend->GetGuid() == "Auto"; + bool readingMode = *btnReading == n64Btn; + bool disabled = (*btnReading != -1 && !readingMode) || !backend->Connected() || backend->GetGuid() == "Auto"; ImVec2 len = ImGui::CalcTextSize(label); ImVec2 pos = ImGui::GetCursorPos(); ImGui::SetCursorPosY(pos.y + len.y / 4); @@ -43,18 +43,18 @@ namespace Ship { const int32_t btn = backend->ReadRawPress(); if(btn != -1) { - backend->SetButtonMapping(CurrentPort, n64Btn, btn); - BtnReading = -1; + backend->SetButtonMapping(currentPort, n64Btn, btn); + *btnReading = -1; // avoid immediately triggering another button during gamepad nav ImGui::SetKeyboardFocusHere(0); } } - const std::string BtnName = backend->GetButtonName(CurrentPort, n64Btn); + const std::string BtnName = backend->GetButtonName(currentPort, n64Btn); if (ImGui::Button(StringHelper::Sprintf("%s##HBTNID_%d", readingMode ? "Press a Key..." : BtnName.c_str(), n64Btn).c_str())) { - BtnReading = n64Btn; + *btnReading = n64Btn; backend->ClearRawPress(); } @@ -64,6 +64,30 @@ namespace Ship { } } + void InputEditor::DrawControllerSelect(int32_t currentPort) { + auto controlDeck = Ship::Window::GetInstance()->GetControlDeck(); + std::string ControllerName = controlDeck->GetPhysicalDeviceFromVirtualSlot(currentPort)->GetControllerName(); + + if (ImGui::BeginCombo("##ControllerEntries", ControllerName.c_str())) { + for (uint8_t i = 0; i < controlDeck->GetNumPhysicalDevices(); i++) { + std::string DeviceName = controlDeck->GetPhysicalDevice(i)->GetControllerName(); + if (DeviceName != "Keyboard" && DeviceName != "Auto") { + DeviceName += "##" + std::to_string(i); + } + if (ImGui::Selectable(DeviceName.c_str(), i == controlDeck->GetVirtualDevice(currentPort))) { + controlDeck->SetPhysicalDevice(currentPort, i); + } + } + ImGui::EndCombo(); + } + + ImGui::SameLine(); + + if (ImGui::Button("Refresh")) { + controlDeck->ScanPhysicalDevices(); + } + } + void InputEditor::DrawVirtualStick(const char* label, ImVec2 stick) { ImGui::SetCursorPos(ImVec2(ImGui::GetCursorPos().x + 5, ImGui::GetCursorPos().y)); ImGui::BeginChild(label, ImVec2(68, 75), false); @@ -85,38 +109,19 @@ namespace Ship { } void InputEditor::DrawControllerSchema() { - auto controlDeck = Ship::Window::GetInstance()->GetControlDeck(); - auto Backend = controlDeck->GetPhysicalDeviceFromVirtualSlot(CurrentPort); + auto Backend = Ship::Window::GetInstance()->GetControlDeck()->GetPhysicalDeviceFromVirtualSlot(CurrentPort); auto profile = Backend->getProfile(CurrentPort); - bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected(); - std::string ControllerName = Backend->GetControllerName(); - - if (ImGui::BeginCombo("##ControllerEntries", ControllerName.c_str())) { - for (uint8_t i = 0; i < controlDeck->GetNumPhysicalDevices(); i++) { - std::string DeviceName = controlDeck->GetPhysicalDevice(i)->GetControllerName(); - if (DeviceName != "Keyboard" && DeviceName != "Auto") { - DeviceName += "##"+std::to_string(i); - } - if (ImGui::Selectable(DeviceName.c_str(), i == controlDeck->GetVirtualDevice(CurrentPort))) { - controlDeck->SetPhysicalDevice(CurrentPort, i); - } - } - ImGui::EndCombo(); - } - - ImGui::SameLine(); - - if(ImGui::Button("Refresh")) { - controlDeck->ScanPhysicalDevices(); - } - + bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected(); + + DrawControllerSelect(CurrentPort); + SohImGui::BeginGroupPanel("Buttons", ImVec2(150, 20)); - DrawButton("A", BTN_A); - DrawButton("B", BTN_B); - DrawButton("L", BTN_L); - DrawButton("R", BTN_R); - DrawButton("Z", BTN_Z); - DrawButton("START", BTN_START); + DrawButton("A", BTN_A, CurrentPort, &BtnReading); + DrawButton("B", BTN_B, CurrentPort, &BtnReading); + DrawButton("L", BTN_L, CurrentPort, &BtnReading); + DrawButton("R", BTN_R, CurrentPort, &BtnReading); + DrawButton("Z", BTN_Z, CurrentPort, &BtnReading); + DrawButton("START", BTN_START, CurrentPort, &BtnReading); SEPARATION(); #ifdef __SWITCH__ SohImGui::EndGroupPanel(IsKeyboard ? 7.0f : 56.0f); @@ -125,10 +130,10 @@ namespace Ship { #endif ImGui::SameLine(); SohImGui::BeginGroupPanel("Digital Pad", ImVec2(150, 20)); - DrawButton("Up", BTN_DUP); - DrawButton("Down", BTN_DDOWN); - DrawButton("Left", BTN_DLEFT); - DrawButton("Right", BTN_DRIGHT); + DrawButton("Up", BTN_DUP, CurrentPort, &BtnReading); + DrawButton("Down", BTN_DDOWN, CurrentPort, &BtnReading); + DrawButton("Left", BTN_DLEFT, CurrentPort, &BtnReading); + DrawButton("Right", BTN_DRIGHT, CurrentPort, &BtnReading); SEPARATION(); #ifdef __SWITCH__ SohImGui::EndGroupPanel(IsKeyboard ? 53.0f : 122.0f); @@ -137,10 +142,10 @@ namespace Ship { #endif ImGui::SameLine(); SohImGui::BeginGroupPanel("Analog Stick", ImVec2(150, 20)); - DrawButton("Up", BTN_STICKUP); - DrawButton("Down", BTN_STICKDOWN); - DrawButton("Left", BTN_STICKLEFT); - DrawButton("Right", BTN_STICKRIGHT); + DrawButton("Up", BTN_STICKUP, CurrentPort, &BtnReading); + DrawButton("Down", BTN_STICKDOWN, CurrentPort, &BtnReading); + DrawButton("Left", BTN_STICKLEFT, CurrentPort, &BtnReading); + DrawButton("Right", BTN_STICKRIGHT, CurrentPort, &BtnReading); if (!IsKeyboard) { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 8); @@ -181,10 +186,10 @@ namespace Ship { if (!IsKeyboard) { ImGui::SameLine(); SohImGui::BeginGroupPanel("Right Stick", ImVec2(150, 20)); - DrawButton("Up", BTN_VSTICKUP); - DrawButton("Down", BTN_VSTICKDOWN); - DrawButton("Left", BTN_VSTICKLEFT); - DrawButton("Right", BTN_VSTICKRIGHT); + DrawButton("Up", BTN_VSTICKUP, CurrentPort, &BtnReading); + DrawButton("Down", BTN_VSTICKDOWN, CurrentPort, &BtnReading); + DrawButton("Left", BTN_VSTICKLEFT, CurrentPort, &BtnReading); + DrawButton("Right", BTN_VSTICKRIGHT, CurrentPort, &BtnReading); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 8); // 2 is the SDL value for right stick X axis @@ -282,10 +287,10 @@ namespace Ship { const ImVec2 cursor = ImGui::GetCursorPos(); SohImGui::BeginGroupPanel("C-Buttons", ImVec2(158, 20)); - DrawButton("Up", BTN_CUP); - DrawButton("Down", BTN_CDOWN); - DrawButton("Left", BTN_CLEFT); - DrawButton("Right", BTN_CRIGHT); + DrawButton("Up", BTN_CUP, CurrentPort, &BtnReading); + DrawButton("Down", BTN_CDOWN, CurrentPort, &BtnReading); + DrawButton("Left", BTN_CLEFT, CurrentPort, &BtnReading); + DrawButton("Right", BTN_CRIGHT, CurrentPort, &BtnReading); ImGui::Dummy(ImVec2(0, 5)); SohImGui::EndGroupPanel(); diff --git a/libultraship/InputEditor.h b/libultraship/InputEditor.h index 50b059e2d00..23944814c32 100644 --- a/libultraship/InputEditor.h +++ b/libultraship/InputEditor.h @@ -11,7 +11,8 @@ namespace Ship { bool Opened = false; public: void Init(); - void DrawButton(const char* label, int32_t n64Btn); + void DrawButton(const char* label, int32_t n64Btn, int32_t currentPort, int32_t* btnReading); + void DrawControllerSelect(int32_t currentPort); void DrawVirtualStick(const char* label, ImVec2 stick); void DrawControllerSchema(); void DrawHud(); diff --git a/libultraship/KeyboardController.cpp b/libultraship/KeyboardController.cpp index 88fb0f89790..18225918fb4 100644 --- a/libultraship/KeyboardController.cpp +++ b/libultraship/KeyboardController.cpp @@ -74,7 +74,6 @@ namespace Ship { return strlen(name) == 0 ? "Unknown" : name; } - void KeyboardController::CreateDefaultBinding(int32_t virtualSlot) { auto profile = getProfile(virtualSlot); profile->Mappings[0x14D] = BTN_CRIGHT; @@ -95,6 +94,8 @@ namespace Ship { profile->Mappings[0x01E] = BTN_STICKLEFT; profile->Mappings[0x01F] = BTN_STICKDOWN; profile->Mappings[0x011] = BTN_STICKUP; + profile->Mappings[0x02A] = BTN_MODIFIER1; + profile->Mappings[0x036] = BTN_MODIFIER2; } const std::string KeyboardController::GetControllerName() { diff --git a/libultraship/Lib/Fast3D/U64/PR/ultra64/controller.h b/libultraship/Lib/Fast3D/U64/PR/ultra64/controller.h index fbcbae6748e..a72bab37fe6 100644 --- a/libultraship/Lib/Fast3D/U64/PR/ultra64/controller.h +++ b/libultraship/Lib/Fast3D/U64/PR/ultra64/controller.h @@ -87,6 +87,8 @@ #define BTN_CUP 0x0008 #define BTN_R 0x0010 #define BTN_L 0x0020 +#define BTN_MODIFIER1 0x0040 +#define BTN_MODIFIER2 0x0080 #define BTN_DRIGHT 0x0100 #define BTN_DLEFT 0x0200 #define BTN_DDOWN 0x0400 diff --git a/libultraship/SDLController.cpp b/libultraship/SDLController.cpp index bc4500de1e5..075831bab7c 100644 --- a/libultraship/SDLController.cpp +++ b/libultraship/SDLController.cpp @@ -448,6 +448,8 @@ namespace Ship { profile->Mappings[SDL_CONTROLLER_BUTTON_START] = BTN_START; profile->Mappings[SDL_CONTROLLER_BUTTON_B] = BTN_B; profile->Mappings[SDL_CONTROLLER_BUTTON_A] = BTN_A; + profile->Mappings[SDL_CONTROLLER_BUTTON_LEFTSTICK] = BTN_MODIFIER1; + profile->Mappings[SDL_CONTROLLER_BUTTON_RIGHTSTICK] = BTN_MODIFIER2; for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { profile->AxisDeadzones[i] = 16.0f; diff --git a/libultraship/UltraController.h b/libultraship/UltraController.h index cac60612641..ca65f5dc483 100644 --- a/libultraship/UltraController.h +++ b/libultraship/UltraController.h @@ -89,6 +89,8 @@ #define BTN_CUP 0x00008 #define BTN_R 0x00010 #define BTN_L 0x00020 +#define BTN_MODIFIER1 0x00040 +#define BTN_MODIFIER2 0x00080 #define BTN_DRIGHT 0x00100 #define BTN_DLEFT 0x00200 #define BTN_DDOWN 0x00400