From 45cc82e925ad812cab9cb9ed7035c8acf4f78667 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Sun, 17 Nov 2024 16:58:01 +0100 Subject: [PATCH] okay so maybe the gravity rush fix might have slightly broken the joystick halfmode and key toggle --- src/input/input_handler.cpp | 87 ++++++++++++++++++++++--------------- src/input/input_handler.h | 7 +-- src/sdl_window.cpp | 6 ++- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/input/input_handler.cpp b/src/input/input_handler.cpp index 769716fd8..fac61ec3b 100644 --- a/src/input/input_handler.cpp +++ b/src/input/input_handler.cpp @@ -37,6 +37,17 @@ What structs are needed? InputBinding(key1, key2, key3) ControllerOutput(button, axis) - we only need a const array of these, and one of the attr-s is always 0 BindingConnection(inputBinding (member), controllerOutput (ref to the array element)) + +Things to always test before pushing like a dumbass: +Button outputs +Axis outputs +Input hierarchy +Multi key inputs +Mouse to joystick +Key toggle +Joystick halfmode + +Don't be an idiot and test only the changed part expecting everything else to not be broken */ // Flags and values for varying purposes @@ -51,6 +62,11 @@ std::list toggled_keys; std::list connections = std::list(); ControllerOutput output_array[] = { + // Important: these have to be the first, or else they will update in the wrong order + ControllerOutput(LEFTJOYSTICK_HALFMODE), + ControllerOutput(RIGHTJOYSTICK_HALFMODE), + ControllerOutput(KEY_TOGGLE), + // Button mappings ControllerOutput(OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TRIANGLE), ControllerOutput(OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CIRCLE), @@ -70,15 +86,13 @@ ControllerOutput output_array[] = { ControllerOutput(OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_RIGHT), // Axis mappings - ControllerOutput(0, Input::Axis::LeftX), ControllerOutput(0, Input::Axis::LeftY), - ControllerOutput(0, Input::Axis::RightX), ControllerOutput(0, Input::Axis::RightY), - ControllerOutput(0, Input::Axis::TriggerLeft), ControllerOutput(0, Input::Axis::TriggerRight), - - ControllerOutput(LEFTJOYSTICK_HALFMODE), ControllerOutput(RIGHTJOYSTICK_HALFMODE), - ControllerOutput(KEY_TOGGLE), - - // End marker to signify the end of the array - ControllerOutput(0, Input::Axis::AxisMax)}; + ControllerOutput(0, Axis::LeftX), + ControllerOutput(0, Axis::LeftY), + ControllerOutput(0, Axis::RightX), + ControllerOutput(0, Axis::RightY), + ControllerOutput(0, Axis::TriggerLeft), + ControllerOutput(0, Axis::TriggerRight), +}; // We had to go through 3 files of indirection just to update a flag void ToggleMouseEnabled() { @@ -304,24 +318,14 @@ void ToggleKeyInList(u32 key) { void ControllerOutput::ResetUpdate() { state_changed = false; - if (button != 0) { - switch (button) { - case KEY_TOGGLE: - break; - default: // everything else - new_button_state = false; - break; - } - } else if (axis != Axis::AxisMax) { - new_param = 0; - } else { - LOG_DEBUG(Input, "Controller output with no values detected!"); - } + new_button_state = false; + new_param = 0; } void ControllerOutput::AddUpdate(bool pressed, u32 param) { state_changed = true; if (button != 0) { new_button_state |= pressed; + new_param = param; } else if (axis != Axis::AxisMax) { float multiplier = 1.0; switch (axis) { @@ -338,9 +342,6 @@ void ControllerOutput::AddUpdate(bool pressed, u32 param) { } } void ControllerOutput::FinalizeUpdate() { - if (!state_changed || (old_button_state == new_button_state && old_param == new_param)) { - return; - } old_button_state = new_button_state; old_param = new_param; float touchpad_x = 0; @@ -355,12 +356,15 @@ void ControllerOutput::FinalizeUpdate() { break; case LEFTJOYSTICK_HALFMODE: leftjoystick_halfmode = new_button_state; + // LOG_DEBUG(Input, "This is when we set the halfmode flag to {}", + // leftjoystick_halfmode); break; case RIGHTJOYSTICK_HALFMODE: rightjoystick_halfmode = new_button_state; break; case KEY_TOGGLE: if (new_button_state) { + // LOG_DEBUG(Input, "Toggling a button..."); ToggleKeyInList(new_param); } break; @@ -374,6 +378,8 @@ void ControllerOutput::FinalizeUpdate() { case Axis::LeftX: case Axis::LeftY: multiplier = leftjoystick_halfmode ? 0.5 : 1.0; + // LOG_DEBUG(Input, "This is where we use the halfmode flag that is {}", + // leftjoystick_halfmode); break; case Axis::RightX: case Axis::RightY: @@ -392,7 +398,9 @@ void ControllerOutput::FinalizeUpdate() { } } -void UpdatePressedKeys(u32 value, bool is_pressed) { +// Updates the list of pressed keys with the given input. +// Returns whether the list was updated or not. +bool UpdatePressedKeys(u32 value, bool is_pressed) { if (is_pressed) { // Find the correct position for insertion to maintain order auto it = @@ -402,6 +410,7 @@ void UpdatePressedKeys(u32 value, bool is_pressed) { // Insert only if 'value' is not already in the list if (it == pressed_keys.end() || it->first != value) { pressed_keys.insert(it, {value, false}); + return true; } } else { // Remove 'value' from the list if it's not pressed @@ -409,16 +418,24 @@ void UpdatePressedKeys(u32 value, bool is_pressed) { std::find_if(pressed_keys.begin(), pressed_keys.end(), [value](const std::pair& pk) { return pk.first == value; }); if (it != pressed_keys.end()) { - pressed_keys.erase(it); // Remove the key entirely from the list + pressed_keys.erase(it); + return true; } } + return false; } - // Check if a given binding's all keys are currently active. bool IsInputActive(const InputBinding& i) { - // Extract keys from InputBinding and ignore unused (0) keys + // Extract keys from InputBinding and ignore unused (0) or virtually pressed keys std::list input_keys = {i.key1, i.key2, i.key3}; input_keys.remove(0); + for (auto key = input_keys.begin(); key != input_keys.end();) { + if (std::find(toggled_keys.begin(), toggled_keys.end(), *key) != toggled_keys.end()) { + key = input_keys.erase(key); // Use the returned iterator + } else { + ++key; // Increment only if no erase happened + } + } // Iterator for pressed_keys, starting from the beginning auto pressed_it = pressed_keys.begin(); @@ -443,11 +460,8 @@ bool IsInputActive(const InputBinding& i) { } ++pressed_it; } - - // If not found in pressed_keys, check in toggled_keys - if (!key_found && - std::find(toggled_keys.begin(), toggled_keys.end(), key) == toggled_keys.end()) { - return false; // Key not found in either list + if (!key_found) { + return false; } } @@ -467,12 +481,14 @@ bool IsInputActive(const InputBinding& i) { } void ActivateOutputsFromInputs() { - + // LOG_DEBUG(Input, "Start of an input frame..."); for (auto& it : pressed_keys) { it.second = false; } // this is the cleanest looking code I've ever written, too bad it is not working + // (i left the last part in by accident, then it turnd out to still be true even after I thought + // everything is good) (but now it really should be fine) for (auto& it : output_array) { it.ResetUpdate(); } @@ -520,7 +536,6 @@ void UpdateMouse(GameController* controller) { controller->Axis(0, axis_y, GetAxis(-0x80, 0x80, 0)); } } - Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) { auto* data = (GameController*)param; UpdateMouse(data); diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 7ad5ca1f1..a7ea5514f 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -263,7 +263,7 @@ public: u32 button; Axis axis; - int old_param, new_param; + s32 old_param, new_param; bool old_button_state, new_button_state, state_changed; ControllerOutput(const u32 b, Axis a = Axis::AxisMax) { @@ -322,8 +322,9 @@ public: // Check if the 3 key input is currently active. bool IsInputActive(const InputBinding& i); -// Add/remove the input that generated the event to/from the held keys container. -void UpdatePressedKeys(u32 button, bool is_pressed); +// Updates the list of pressed keys with the given input. +// Returns whether the list was updated or not. +bool UpdatePressedKeys(u32 button, bool is_pressed); void ActivateOutputsFromInputs(); diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index d20016f5a..a38bf60ad 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -196,10 +196,12 @@ void WindowSDL::OnKeyboardMouseInput(const SDL_Event* event) { } // add/remove it from the list - Input::UpdatePressedKeys(input_id, input_down); + bool inputs_changed = Input::UpdatePressedKeys(input_id, input_down); // update bindings - Input::ActivateOutputsFromInputs(); + if (inputs_changed) { + Input::ActivateOutputsFromInputs(); + } } void WindowSDL::OnGamepadEvent(const SDL_Event* event) {