From b6d7abdcd4a8feb38fb149d3f987b609e1a61b29 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 22 Aug 2021 01:41:32 +0200 Subject: [PATCH] Execute gamepad keybinds --- src/Components/Modules/Gamepad.cpp | 90 +++++++++++++++++++++++++++--- src/Components/Modules/Gamepad.hpp | 1 + 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/Gamepad.cpp b/src/Components/Modules/Gamepad.cpp index d6d02195..f725992e 100644 --- a/src/Components/Modules/Gamepad.cpp +++ b/src/Components/Modules/Gamepad.cpp @@ -597,14 +597,22 @@ namespace Components Game::gaGlobs[gamePadIndex].axesValues[physicalAxis] = value; } + void Gamepad::UI_GamepadKeyEvent(int gamePadIndex, int key, bool down) + { + + } + void Gamepad::CL_GamepadButtonEvent(const int gamePadIndex, const int key, const Game::GamePadButtonEvent buttonEvent, unsigned time, Game::GamePadButton button) { assert(gamePadIndex < Game::MAX_GAMEPADS); - auto& keyState = Game::playerKeys[gamePadIndex]; - keyState.keys[key].down = buttonEvent == Game::GPAD_BUTTON_PRESSED || buttonEvent == Game::GPAD_BUTTON_UPDATE; + const auto pressed = buttonEvent == Game::GPAD_BUTTON_PRESSED; + const auto pressedOrUpdated = pressed || buttonEvent == Game::GPAD_BUTTON_UPDATE; - if(buttonEvent == Game::GPAD_BUTTON_PRESSED) + auto& keyState = Game::playerKeys[gamePadIndex]; + keyState.keys[key].down = pressedOrUpdated; + + if(pressed) { if (++keyState.keys[key].repeats == 1) keyState.anyKeyDown++; @@ -615,6 +623,72 @@ namespace Components if (--keyState.anyKeyDown < 0) keyState.anyKeyDown = 0; } + + if(Game::Key_IsKeyCatcherActive(gamePadIndex, Game::KEYCATCH_LOCATION_SELECTION) && pressedOrUpdated) + { + if(key == Game::K_BUTTON_B || keyState.keys[key].binding && strcmp(keyState.keys[key].binding, "+actionslot 4") == 0) + { + keyState.locSelInputState = Game::LOC_SEL_INPUT_CANCEL; + } + else if(key == Game::K_BUTTON_A || keyState.keys[key].binding && strcmp(keyState.keys[key].binding, "+attack") == 0) + { + keyState.locSelInputState = Game::LOC_SEL_INPUT_CONFIRM; + } + return; + } + + keyState.locSelInputState = Game::LOC_SEL_INPUT_NONE; + + const auto* keyBinding = keyState.keys[key].binding; + + if (!keyBinding) + return; + + char cmd[1024]; + if(pressedOrUpdated) + { + if (Game::Key_IsKeyCatcherActive(gamePadIndex, Game::KEYCATCH_UI)) + { + UI_GamepadKeyEvent(gamePadIndex, key, pressedOrUpdated); + return; + } + + if(keyBinding[0] == '+') + { + float floatValue; + if (button) + floatValue = GPad_GetButton(gamePadIndex, button); + else + floatValue = 0.0f; + + sprintf_s(cmd, "%s %i %i %0.3f\n", keyBinding, key, time, floatValue); + Game::Cbuf_AddText(gamePadIndex, cmd); + } + else + { + Game::Cbuf_AddText(gamePadIndex, keyBinding); + Game::Cbuf_AddText(gamePadIndex, "\n"); + } + } + else + { + if (keyBinding[0] == '+') + { + float floatValue; + if (button) + floatValue = GPad_GetButton(gamePadIndex, button); + else + floatValue = 0.0f; + + sprintf_s(cmd, "-%s %i %i %0.3f\n", &keyBinding[1], key, time, floatValue); + Game::Cbuf_AddText(gamePadIndex, cmd); + } + + if (Game::Key_IsKeyCatcherActive(gamePadIndex, Game::KEYCATCH_UI)) + { + UI_GamepadKeyEvent(gamePadIndex, key, pressedOrUpdated); + } + } } void Gamepad::CL_GamepadButtonEventForPort(const int gamePadIndex, const int key, const Game::GamePadButtonEvent buttonEvent, const unsigned time, const Game::GamePadButton button) @@ -1009,7 +1083,7 @@ namespace Components return; // use the xinput state when creating a usercmd - Utils::Hook(0x5A6DB9, CL_CreateCmdStub, HOOK_JUMP).install()->quick(); + //Utils::Hook(0x5A6DB9, CL_CreateCmdStub, HOOK_JUMP).install()->quick(); // package the forward and right move components in the move buttons Utils::Hook(0x60E38D, MSG_WriteDeltaUsercmdKeyStub, HOOK_JUMP).install()->quick(); @@ -1031,11 +1105,11 @@ namespace Components Utils::Hook(0x475E9E, IN_Frame_Hk, HOOK_CALL).install()->quick(); - Utils::Hook(0x5A617D, CL_GetMouseMovementStub, HOOK_CALL).install()->quick(); - Utils::Hook(0x5A6816, CL_GetMouseMovementStub, HOOK_CALL).install()->quick(); - Utils::Hook(0x5A6829, unk_CheckKeyHook, HOOK_CALL).install()->quick(); + //Utils::Hook(0x5A617D, CL_GetMouseMovementStub, HOOK_CALL).install()->quick(); + //Utils::Hook(0x5A6816, CL_GetMouseMovementStub, HOOK_CALL).install()->quick(); + //Utils::Hook(0x5A6829, unk_CheckKeyHook, HOOK_CALL).install()->quick(); - Scheduler::OnFrame(MenuNavigate); + //Scheduler::OnFrame(MenuNavigate); xpadSensitivity = Dvar::Register("xpad_sensitivity", 1.9f, 0.1f, 10.0f, Game::DVAR_FLAG_SAVED, "View sensitivity for XInput-compatible gamepads"); xpadEarlyTime = Dvar::Register("xpad_early_time", 130, 0, 1000, Game::DVAR_FLAG_SAVED, "Time (in milliseconds) of reduced view sensitivity"); diff --git a/src/Components/Modules/Gamepad.hpp b/src/Components/Modules/Gamepad.hpp index 8215d014..6c0fd096 100644 --- a/src/Components/Modules/Gamepad.hpp +++ b/src/Components/Modules/Gamepad.hpp @@ -224,6 +224,7 @@ namespace Components static void GamepadStickTo01(SHORT value, SHORT deadzone, float& output01); static void CL_GamepadResetMenuScrollTime(int gamePadIndex, int key, bool down, unsigned int time); + static void UI_GamepadKeyEvent(int gamePadIndex, int key, bool down); static void CL_GamepadEvent(int gamePadIndex, Game::GamepadPhysicalAxis physicalAxis, float value); static void CL_GamepadButtonEvent(int gamePadIndex, int key, Game::GamePadButtonEvent buttonEvent, unsigned time, Game::GamePadButton button); static void CL_GamepadButtonEventForPort(int gamePadIndex, int key, Game::GamePadButtonEvent buttonEvent, unsigned int time, Game::GamePadButton button);