Skip to content

Commit

Permalink
Input: Add support for gamepad snapturn (fixes #77)
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jan 24, 2024
1 parent 167a1af commit b79d00d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/mods/VR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ void VR::on_xinput_get_state(uint32_t* retval, uint32_t user_index, XINPUT_STATE
if (*retval == ERROR_SUCCESS) {
// Once here for normal gamepads, and once for the spoofed gamepad at the end
update_imgui_state_from_xinput_state(*state, false);
gamepad_snapturn(*state);
}

const auto now = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -3125,6 +3126,33 @@ void VR::recenter_view() {
set_rotation_offset(new_rotation_offset);
}

void VR::gamepad_snapturn(XINPUT_STATE& state) {
if (!m_snapturn->value()) {
return;
}

if (!is_hmd_active()) {
return;
}

const auto stick_axis = (float)state.Gamepad.sThumbRX / (float)std::numeric_limits<SHORT>::max();

if (!m_was_snapturn_run_on_input) {
if (glm::abs(stick_axis) > m_snapturn_joystick_deadzone->value()) {
m_snapturn_left = stick_axis < 0.0f;
m_snapturn_on_frame = true;
m_was_snapturn_run_on_input = true;
state.Gamepad.sThumbRX = 0;
}
} else {
if (glm::abs(stick_axis) < m_snapturn_joystick_deadzone->value()) {
m_was_snapturn_run_on_input = false;
} else {
state.Gamepad.sThumbRX = 0;
}
}
}

void VR::process_snapturn() {
if (!m_snapturn_on_frame) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/mods/VR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ class VR : public Mod {
const ModToggle::Ptr m_swap_controllers{ ModToggle::create(generate_name("SwapControllerInputs"), false) };

// Snap turn settings and globals
void gamepad_snapturn(XINPUT_STATE& state);
void process_snapturn();

const ModToggle::Ptr m_snapturn{ ModToggle::create(generate_name("SnapTurn"), false) };
Expand Down

0 comments on commit b79d00d

Please sign in to comment.