From d00d0a6a265b10e6ce8181e9c979af3bb029efc0 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 21 Mar 2023 20:10:40 +0000 Subject: [PATCH] Do-not-wrap-cursor-Do-not-switch-menu-controls Thanks @MuadDib! Co-Authored-By: petr666 <5103207+petr666@users.noreply.github.com> --- Projects/Android/jni/JKXR/VrCommon.h | 2 +- Projects/Android/jni/JKXR/VrInputCommon.cpp | 12 ++----- Projects/Android/jni/JKXR/VrInputDefault.cpp | 35 ++++++++++++++----- .../Android/jni/JKXR/VrInputWeaponAlign.cpp | 29 ++++++++++----- .../Android/jni/OpenJK/code/ui/ui_main.cpp | 18 +++++----- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/Projects/Android/jni/JKXR/VrCommon.h b/Projects/Android/jni/JKXR/VrCommon.h index 0a24e24..5f02a10 100644 --- a/Projects/Android/jni/JKXR/VrCommon.h +++ b/Projects/Android/jni/JKXR/VrCommon.h @@ -19,6 +19,6 @@ bool between(float min, float val, float max); void rotateAboutOrigin(float v1, float v2, float rotation, vec2_t out); void QuatToYawPitchRoll(XrQuaternionf q, vec3_t rotation, vec3_t out); void handleTrackedControllerButton(ovrInputStateTrackedRemote * trackedRemoteState, ovrInputStateTrackedRemote * prevTrackedRemoteState, uint32_t button, int key); -void interactWithTouchScreen(bool reset, ovrInputStateTrackedRemote *newState, ovrInputStateTrackedRemote *oldState); +void interactWithTouchScreen(float menuYaw, vec3_t controllerAngles); #endif //vrcommon_h \ No newline at end of file diff --git a/Projects/Android/jni/JKXR/VrInputCommon.cpp b/Projects/Android/jni/JKXR/VrInputCommon.cpp index 2fdcadd..0a5e42c 100644 --- a/Projects/Android/jni/JKXR/VrInputCommon.cpp +++ b/Projects/Android/jni/JKXR/VrInputCommon.cpp @@ -129,15 +129,9 @@ float clamp(float _min, float _val, float _max) return fmax(fmin(_val, _max), _min); } -void interactWithTouchScreen(bool reset, ovrInputStateTrackedRemote *newState, ovrInputStateTrackedRemote *oldState) { - - static float centerYaw = 0; - if (reset || Q_isnan(centerYaw) || fabs(sinf(DEG2RAD(vr.weaponangles[ANGLES_ADJUSTED][YAW]-centerYaw))) > 0.9f) - { - centerYaw = vr.weaponangles[ANGLES_ADJUSTED][YAW]; - } - float cursorX = -sinf(DEG2RAD(vr.weaponangles[ANGLES_ADJUSTED][YAW]-centerYaw)) + 0.5f; - float cursorY = (float)(vr.weaponangles[ANGLES_ADJUSTED][PITCH] / 90.0) + 0.5f; +void interactWithTouchScreen(float menuYaw, vec3_t controllerAngles) { + float cursorX = -sinf(DEG2RAD(controllerAngles[YAW] - menuYaw)) + 0.5f; + float cursorY = (float)(controllerAngles[PITCH] / 90.0) + 0.5f; PortableMouseAbs(cursorX, cursorY); } diff --git a/Projects/Android/jni/JKXR/VrInputDefault.cpp b/Projects/Android/jni/JKXR/VrInputDefault.cpp index efdd05c..e818215 100644 --- a/Projects/Android/jni/JKXR/VrInputDefault.cpp +++ b/Projects/Android/jni/JKXR/VrInputDefault.cpp @@ -132,25 +132,42 @@ void HandleInput_Default( ovrInputStateTrackedRemote *pDominantTrackedRemoteNew, handleTrackedControllerButton(&leftTrackedRemoteState_new, &leftTrackedRemoteState_old, xrButton_Enter, A_ESCAPE); handleTrackedControllerButton(&rightTrackedRemoteState_new, &rightTrackedRemoteState_old, xrButton_Enter, A_ESCAPE); - static bool resetCursor = qtrue; + static float menuYaw = 0; + static bool switchedMenuControls = qfalse; if (VR_UseScreenLayer() && !vr.misc_camera /*bit of a fiddle, but if we are in a misc camera, we are in the game and shouldn't be in here*/) { - interactWithTouchScreen(resetCursor, pDominantTrackedRemoteNew, pDominantTrackedRemoteOld); - resetCursor = qfalse; + bool controlsLeftHanded = vr_control_scheme->integer >= 10; + if ((controlsLeftHanded && !switchedMenuControls) || (!controlsLeftHanded && switchedMenuControls)) { + interactWithTouchScreen(menuYaw, vr.offhandangles[ANGLES_DEFAULT]); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, offButton1, A_MOUSE1); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, offButton2, A_ESCAPE); + if ((pDominantTrackedRemoteNew->Buttons & xrButton_Trigger) != (pDominantTrackedRemoteOld->Buttons & xrButton_Trigger) && (pDominantTrackedRemoteNew->Buttons & xrButton_Trigger)) { + switchedMenuControls = !switchedMenuControls; + } + } else { + interactWithTouchScreen(menuYaw, vr.weaponangles[ANGLES_DEFAULT]); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton1, A_MOUSE1); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton2, A_ESCAPE); + if ((pOffTrackedRemoteNew->Buttons & xrButton_Trigger) != (pOffTrackedRemoteOld->Buttons & xrButton_Trigger) && (pOffTrackedRemoteNew->Buttons & xrButton_Trigger)) { + switchedMenuControls = !switchedMenuControls; + } + } - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton1, A_MOUSE1); - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton2, A_ESCAPE); - - //To skip flatscreen cinematic + //To skip flatscreen cinematic use thumb of any controller if ((pDominantTrackedRemoteNew->Buttons & primaryThumb) != (pDominantTrackedRemoteOld->Buttons & primaryThumb)) { sendButtonAction("+use", (pDominantTrackedRemoteNew->Buttons & primaryThumb)); } + if ((pOffTrackedRemoteNew->Buttons & secondaryThumb) != + (pOffTrackedRemoteOld->Buttons & secondaryThumb)) { + sendButtonAction("+use", (pOffTrackedRemoteNew->Buttons & secondaryThumb)); + } } else { - resetCursor = qtrue; + menuYaw = vr.hmdorientation[YAW]; float distance = sqrtf(powf(pOff->Pose.position.x - pWeapon->Pose.position.x, 2) + powf(pOff->Pose.position.y - pWeapon->Pose.position.y, 2) + diff --git a/Projects/Android/jni/JKXR/VrInputWeaponAlign.cpp b/Projects/Android/jni/JKXR/VrInputWeaponAlign.cpp index 2bdc661..de8fd71 100644 --- a/Projects/Android/jni/JKXR/VrInputWeaponAlign.cpp +++ b/Projects/Android/jni/JKXR/VrInputWeaponAlign.cpp @@ -69,19 +69,32 @@ void HandleInput_WeaponAlign( ovrInputStateTrackedRemote *pDominantTrackedRemote //Menu button handleTrackedControllerButton(&leftTrackedRemoteState_new, &leftTrackedRemoteState_old, xrButton_Enter, A_ESCAPE); - static bool resetCursor = qtrue; + static float menuYaw = 0; + static bool switchedMenuControls = qfalse; if (VR_UseScreenLayer() ) { - interactWithTouchScreen(resetCursor, pDominantTrackedRemoteNew, pDominantTrackedRemoteOld); - resetCursor = qfalse; - - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton1, A_MOUSE1); - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); - handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton2, A_ESCAPE); + bool controlsLeftHanded = vr_control_scheme->integer >= 10; + if ((controlsLeftHanded && !switchedMenuControls) || (!controlsLeftHanded && switchedMenuControls)) { + interactWithTouchScreen(menuYaw, vr.offhandangles[ANGLES_DEFAULT]); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, offButton1, A_MOUSE1); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); + handleTrackedControllerButton(pOffTrackedRemoteNew, pOffTrackedRemoteOld, offButton2, A_ESCAPE); + if ((pDominantTrackedRemoteNew->Buttons & xrButton_Trigger) != (pDominantTrackedRemoteOld->Buttons & xrButton_Trigger) && (pDominantTrackedRemoteNew->Buttons & xrButton_Trigger)) { + switchedMenuControls = !switchedMenuControls; + } + } else { + interactWithTouchScreen(menuYaw, vr.weaponangles[ANGLES_DEFAULT]); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton1, A_MOUSE1); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, xrButton_Trigger, A_MOUSE1); + handleTrackedControllerButton(pDominantTrackedRemoteNew, pDominantTrackedRemoteOld, domButton2, A_ESCAPE); + if ((pOffTrackedRemoteNew->Buttons & xrButton_Trigger) != (pOffTrackedRemoteOld->Buttons & xrButton_Trigger) && (pOffTrackedRemoteNew->Buttons & xrButton_Trigger)) { + switchedMenuControls = !switchedMenuControls; + } + } } else { - resetCursor = qtrue; + menuYaw = vr.hmdorientation[YAW]; //dominant hand stuff first { diff --git a/Projects/Android/jni/OpenJK/code/ui/ui_main.cpp b/Projects/Android/jni/OpenJK/code/ui/ui_main.cpp index 3d4e14f..9a5672c 100644 --- a/Projects/Android/jni/OpenJK/code/ui/ui_main.cpp +++ b/Projects/Android/jni/OpenJK/code/ui/ui_main.cpp @@ -4114,27 +4114,29 @@ UI_MouseEventAbs ================= */ //JLFMOUSE CALLED EACH FRAME IN UI +const int cursorSize = 48; void _UI_MouseEventAbs( int x, int y ) { // update mouse screen position + // allow to hide cursor beyond the screen edges uiInfo.uiDC.cursorx = x; - if (uiInfo.uiDC.cursorx < 0) + if (uiInfo.uiDC.cursorx < -cursorSize) { - uiInfo.uiDC.cursorx = 0; + uiInfo.uiDC.cursorx = -cursorSize; } - else if (uiInfo.uiDC.cursorx > SCREEN_WIDTH) + else if (uiInfo.uiDC.cursorx > SCREEN_WIDTH + cursorSize) { - uiInfo.uiDC.cursorx = SCREEN_WIDTH; + uiInfo.uiDC.cursorx = SCREEN_WIDTH + cursorSize; } uiInfo.uiDC.cursory = y; - if (uiInfo.uiDC.cursory < 0) + if (uiInfo.uiDC.cursory < -cursorSize) { - uiInfo.uiDC.cursory = 0; + uiInfo.uiDC.cursory = -cursorSize; } - else if (uiInfo.uiDC.cursory > SCREEN_HEIGHT) + else if (uiInfo.uiDC.cursory > SCREEN_HEIGHT + cursorSize) { - uiInfo.uiDC.cursory = SCREEN_HEIGHT; + uiInfo.uiDC.cursory = SCREEN_HEIGHT + cursorSize; } if (Menu_Count() > 0)