Skip to content

Commit

Permalink
Add snapturn feature and related gui settings
Browse files Browse the repository at this point in the history
  • Loading branch information
narknon committed Nov 30, 2023
1 parent 611b75d commit 09af905
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/mods/VR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,54 @@ void VR::on_xinput_get_state(uint32_t* retval, uint32_t user_index, XINPUT_STATE
}
}
}

// Determine if snapturn should be run on frame
if (m_snapturn->value()) {
DPadMethod dpad_method = get_dpad_method();
const auto snapturn_deadzone = get_snapturn_js_deadzone();
float stick_axis{};

if (!m_was_snapturn_run_on_input) {
if (dpad_method == RIGHT_JOYSTICK) {
stick_axis = get_left_stick_axis().x;
if (abs(stick_axis) >= snapturn_deadzone) {
if (stick_axis < 0) {
m_snapturn_left = true;
}
m_snapturn_on_frame = true;
m_was_snapturn_run_on_input = true;
}
}
else {
stick_axis = get_right_stick_axis().x;
if (abs(stick_axis) >= snapturn_deadzone && !(dpad_method == DPadMethod::LEFT_TOUCH && is_action_active_any_joystick(m_action_thumbrest_touch_left))) {
if (stick_axis < 0) {
m_snapturn_left = true;
}
m_snapturn_on_frame = true;
m_was_snapturn_run_on_input = true;
}
}
}
else {
if (dpad_method == RIGHT_JOYSTICK) {
if (abs(get_left_stick_axis().x) < snapturn_deadzone) {
m_was_snapturn_run_on_input = false;
} else {
state->Gamepad.sThumbLY = 0;
state->Gamepad.sThumbLX = 0;
}
}
else {
if (abs(get_right_stick_axis().x) < snapturn_deadzone) {
m_was_snapturn_run_on_input = false;
} else {
state->Gamepad.sThumbRY = 0;
state->Gamepad.sThumbRX = 0;
}
}
}
}

// Do it again after all the VR buttons have been spoofed
update_imgui_state_from_xinput_state(*state, true);
Expand Down Expand Up @@ -2060,6 +2108,17 @@ void VR::on_draw_sidebar_entry(std::string_view name) {
ImGui::TreePop();
}

ImGui::SetNextItemOpen(true, ImGuiCond_::ImGuiCond_Once);
if (ImGui::TreeNode("Snap Turn")) {
m_snapturn->draw("Enabled");
ImGui::TextWrapped("Set Snap Turn Rotation Angle in Degrees.");
m_snapturn_angle->draw("Angle");
ImGui::TextWrapped("Set Snap Turn Joystick Deadzone.");
m_snapturn_joystick_deadzone->draw("Deadzone");

ImGui::TreePop();
}

ImGui::SetNextItemOpen(true, ImGuiCond_::ImGuiCond_Once);
if (ImGui::TreeNode("Movement Orientation")) {
m_movement_orientation->draw("Type");
Expand Down Expand Up @@ -2099,10 +2158,9 @@ void VR::on_draw_sidebar_entry(std::string_view name) {
load_camera(i);
}
}

ImGui::TreePop();
}

ImGui::SetNextItemOpen(true, ImGuiCond_::ImGuiCond_Once);
if (ImGui::TreeNode("Decoupled Pitch")) {
m_decoupled_pitch->draw("Enabled");
Expand Down
23 changes: 23 additions & 0 deletions src/mods/VR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ class VR : public Mod {
return (DPadMethod)m_dpad_shifting_method->value();
}

bool is_snapturn_enabled() const {
return m_snapturn->value();
}

float get_snapturn_js_deadzone() const {
return m_snapturn_joystick_deadzone->value();
}

int get_snapturn_angle() const {
return m_snapturn_angle->value();
}

bool should_skip_post_init_properties() const {
return m_compatibility_skip_pip->value();
}
Expand Down Expand Up @@ -731,6 +743,14 @@ class VR : public Mod {
const ModToggle::Ptr m_roomscale_movement{ ModToggle::create(generate_name("RoomscaleMovement"), false) };
const ModToggle::Ptr m_roomscale_movement_actor_rotation{ ModToggle::create(generate_name("RoomscaleMovementActorRotation"), false) };

// Snap turn settings and globals
const ModToggle::Ptr m_snapturn{ ModToggle::create(generate_name("SnapTurn"), false) };
const ModSlider::Ptr m_snapturn_joystick_deadzone{ ModSlider::create(generate_name("SnapturnJoystickDeadzone"), 0.01f, 0.9f, 0.1f) };
const ModInt32::Ptr m_snapturn_angle{ ModInt32::create(generate_name("SnapturnTurnAngle"), 45) };
static inline bool m_snapturn_on_frame{false};
static inline bool m_snapturn_left{false};
static inline bool m_was_snapturn_run_on_input{false};

// Aim method and movement orientation are not the same thing, but they can both have the same options
const ModCombo::Ptr m_aim_method{ ModCombo::create(generate_name("AimMethod"), s_aim_method_names, AimMethod::GAME) };
const ModCombo::Ptr m_movement_orientation{ ModCombo::create(generate_name("MovementOrientation"), s_aim_method_names, AimMethod::GAME) };
Expand Down Expand Up @@ -803,6 +823,9 @@ class VR : public Mod {
*m_2d_screen_mode,
*m_roomscale_movement,
*m_roomscale_movement_actor_rotation,
*m_snapturn,
*m_snapturn_joystick_deadzone,
*m_snapturn_angle,
*m_aim_method,
*m_movement_orientation,
*m_aim_speed,
Expand Down
17 changes: 17 additions & 0 deletions src/mods/vr/FFakeStereoRenderingHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4134,6 +4134,23 @@ __forceinline void FFakeStereoRenderingHook::calculate_stereo_view_offset(
}
}
}

// Process snapturn
if (vr->m_snapturn_on_frame) {
const auto world = sdk::UEngine::get()->get_world();
if (const auto controller = sdk::UGameplayStatics::get()->get_player_controller(world, 0); controller != nullptr) {
auto controller_rot = controller->get_control_rotation();
auto turn_degrees = vr->get_snapturn_angle();

if (vr->m_snapturn_left) {
turn_degrees = -turn_degrees;
vr->m_snapturn_left = false;
}
controller_rot.y += turn_degrees;
controller->set_control_rotation(controller_rot);
}
vr->m_snapturn_on_frame = false;
}
}

if (!is_full_pass) {
Expand Down

0 comments on commit 09af905

Please sign in to comment.