From 011592754fcdcc482eeb21378d4dcd1710b67fb8 Mon Sep 17 00:00:00 2001 From: Lubos Date: Thu, 17 Nov 2022 12:19:17 +0100 Subject: [PATCH] OpenXR - Use mouse control only --- Common/UI/ViewGroup.cpp | 12 ++++- Common/VR/PPSSPPVR.cpp | 92 +++++++++++++++++++++++-------------- Common/VR/PPSSPPVR.h | 9 ++++ UI/ControlMappingScreen.cpp | 4 ++ UI/EmuScreen.cpp | 3 ++ UI/PauseScreen.cpp | 3 ++ 6 files changed, 87 insertions(+), 36 deletions(-) diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index f90f0eb1a6..556f664c9e 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -826,13 +826,21 @@ bool ScrollView::Key(const KeyInput &input) { if (visibility_ != V_VISIBLE) return ViewGroup::Key(input); + float scrollSpeed = 250; + switch (input.deviceId) { + case DEVICE_ID_XR_CONTROLLER_LEFT: + case DEVICE_ID_XR_CONTROLLER_RIGHT: + scrollSpeed = 50; + break; + } + if (input.flags & KEY_DOWN) { switch (input.keyCode) { case NKCODE_EXT_MOUSEWHEEL_UP: - ScrollRelative(-250); + ScrollRelative(-scrollSpeed); break; case NKCODE_EXT_MOUSEWHEEL_DOWN: - ScrollRelative(250); + ScrollRelative(scrollSpeed); break; } } diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index 13a9c16ed3..2c74f7536e 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -36,6 +36,7 @@ enum VRMirroring { VR_MIRRORING_COUNT }; +static VRAppMode appMode = VR_MENU_MODE; static int pspAxisDeviceId = 0; static std::map pspAxis; static std::map pspKeys; @@ -67,16 +68,6 @@ struct ButtonMapping { } }; -struct MouseActivator { - bool activate; - ovrButton ovr; - - MouseActivator(bool activate, ovrButton ovr) { - this->activate = activate; - this->ovr = ovr; - } -}; - static std::vector leftControllerMapping = { ButtonMapping(NKCODE_BUTTON_X, ovrButton_X), ButtonMapping(NKCODE_BUTTON_Y, ovrButton_Y), @@ -108,16 +99,8 @@ static std::vector controllerMapping[2] = { rightControllerMapping }; static bool controllerMotion[2][5] = {}; -static int mouseController = -1; -static bool mousePressed[] = {false, false}; - -static std::vector mouseActivators = { - MouseActivator(true, ovrButton_Trigger), - MouseActivator(false, ovrButton_Up), - MouseActivator(false, ovrButton_Down), - MouseActivator(false, ovrButton_Left), - MouseActivator(false, ovrButton_Right), -}; +static int mouseController = 1; +static bool mousePressed = false; /* ================================================================================ @@ -196,6 +179,18 @@ void GetVRResolutionPerEye(int* width, int* height) { } } +/* +================================================================================ + +VR input integration + +================================================================================ +*/ + +void SetVRAppMode(VRAppMode mode) { + appMode = mode; +} + void UpdateVRInput(bool(*NativeAxis)(const AxisInput &axis), bool(*NativeKey)(const KeyInput &key), bool(*NativeTouch)(const TouchInput &touch), bool haptics, float dp_xscale, float dp_yscale) { //axis @@ -245,16 +240,6 @@ void UpdateVRInput(bool(*NativeAxis)(const AxisInput &axis), bool(*NativeKey)(co } } - //enable or disable mouse - for (int j = 0; j < 2; j++) { - int status = IN_VRGetButtonState(j); - for (MouseActivator& m : mouseActivators) { - if (status & m.ovr) { - mouseController = m.activate ? j : -1; - } - } - } - //motion control if (g_Config.bEnableMotions) { for (int j = 0; j < 2; j++) { @@ -307,8 +292,22 @@ void UpdateVRInput(bool(*NativeAxis)(const AxisInput &axis), bool(*NativeKey)(co } } + //enable or disable mouse + for (int j = 0; j < 2; j++) { + bool pressed = IN_VRGetButtonState(j) & ovrButton_Trigger; + if (pressed) { + int lastController = mouseController; + mouseController = j; + + //prevent misclicks when changing the left/right controller + if (lastController != mouseController) { + mousePressed = true; + } + } + } + //mouse cursor - if (mouseController >= 0) { + if ((mouseController >= 0) && (appMode == VR_MENU_MODE)) { //get position on screen XrPosef pose = IN_VRGetPose(mouseController); XrVector3f angles = XrQuaternionf_ToEulerAngles(pose.orientation); @@ -331,15 +330,25 @@ void UpdateVRInput(bool(*NativeAxis)(const AxisInput &axis), bool(*NativeKey)(co touch.x = x * dp_xscale; touch.y = (height - y - 1) * dp_yscale; bool pressed = IN_VRGetButtonState(mouseController) & ovrButton_Trigger; - if (mousePressed[mouseController] != pressed) { - if (!pressed) { + if (mousePressed != pressed) { + if (pressed) { touch.flags = TOUCH_DOWN; NativeTouch(touch); touch.flags = TOUCH_UP; NativeTouch(touch); } - mousePressed[mouseController] = pressed; + mousePressed = pressed; } + + //mouse wheel emulation + keyInput.deviceId = controllerIds[mouseController]; + float scroll = -IN_VRGetJoystickState(mouseController).y; + keyInput.flags = scroll < -0.5f ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_MOUSEWHEEL_UP; + NativeKey(keyInput); + keyInput.flags = scroll > 0.5f ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_MOUSEWHEEL_DOWN; + NativeKey(keyInput); } else { VR_SetConfig(VR_CONFIG_MOUSE_SIZE, 0); } @@ -360,12 +369,27 @@ bool UpdateVRAxis(const AxisInput &axis) { } bool UpdateVRKeys(const KeyInput &key) { + //store key value std::vector nativeKeys; if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &nativeKeys)) { for (int& nativeKey : nativeKeys) { pspKeys[nativeKey] = key.flags & KEY_DOWN; } } + + //block keys in the menus + if (appMode == VR_MENU_MODE) { + switch (key.keyCode) { + case NKCODE_BACK: + case NKCODE_EXT_MOUSEWHEEL_UP: + case NKCODE_EXT_MOUSEWHEEL_DOWN: + return true; + default: + return false; + } + } + + //block keys by camera adjust return !pspKeys[VIRTKEY_VR_CAMERA_ADJUST]; } diff --git a/Common/VR/PPSSPPVR.h b/Common/VR/PPSSPPVR.h index b10902cff4..23a13ec0b4 100644 --- a/Common/VR/PPSSPPVR.h +++ b/Common/VR/PPSSPPVR.h @@ -17,11 +17,20 @@ enum VRCompatFlag { VR_COMPAT_MAX }; +enum VRAppMode { + VR_CONTROLL_MAPPING_MODE, + VR_GAME_MODE, + VR_MENU_MODE, +}; + // VR app flow integration bool IsVREnabled(); void InitVROnAndroid(void* vm, void* activity, const char* system, int version, const char* name); void EnterVR(bool firstStart, void* vulkanContext); void GetVRResolutionPerEye(int* width, int* height); + +// VR input integration +void SetVRAppMode(VRAppMode mode); void UpdateVRInput(bool(*NativeAxis)(const AxisInput &axis), bool(*NativeKey)(const KeyInput &key), bool(*NativeTouch)(const TouchInput &touch), bool haptics, float dp_xscale, float dp_yscale); bool UpdateVRAxis(const AxisInput &axis); diff --git a/UI/ControlMappingScreen.cpp b/UI/ControlMappingScreen.cpp index f6255ad4e3..1415cb53be 100644 --- a/UI/ControlMappingScreen.cpp +++ b/UI/ControlMappingScreen.cpp @@ -27,6 +27,7 @@ #include "Common/UI/Context.h" #include "Common/UI/View.h" #include "Common/UI/ViewGroup.h" +#include "Common/VR/PPSSPPVR.h" #include "Common/Log.h" #include "Common/Data/Color/RGBAUtil.h" @@ -279,6 +280,7 @@ void ControlMappingScreen::update() { } UIDialogScreenWithBackground::update(); + SetVRAppMode(VRAppMode::VR_MENU_MODE); } UI::EventReturn ControlMappingScreen::OnClearMapping(UI::EventParams ¶ms) { @@ -328,6 +330,7 @@ void KeyMappingNewKeyDialog::CreatePopupContents(UI::ViewGroup *parent) { std::string pspButtonName = KeyMap::GetPspButtonName(this->pspBtn_); parent->Add(new TextView(std::string(km->T("Map a new key for")) + " " + mc->T(pspButtonName), new LinearLayoutParams(Margins(10,0)))); + SetVRAppMode(VRAppMode::VR_CONTROLL_MAPPING_MODE); } bool KeyMappingNewKeyDialog::key(const KeyInput &key) { @@ -360,6 +363,7 @@ void KeyMappingNewMouseKeyDialog::CreatePopupContents(UI::ViewGroup *parent) { auto km = GetI18NCategory("KeyMapping"); parent->Add(new TextView(std::string(km->T("You can press ESC to cancel.")), new LinearLayoutParams(Margins(10, 0)))); + SetVRAppMode(VRAppMode::VR_CONTROLL_MAPPING_MODE); } bool KeyMappingNewMouseKeyDialog::key(const KeyInput &key) { diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 896c939d12..0b247144a6 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -31,6 +31,7 @@ using namespace std::placeholders; #include "Common/UI/Context.h" #include "Common/UI/Tween.h" #include "Common/UI/View.h" +#include "Common/VR/PPSSPPVR.h" #include "Common/Data/Text/I18n.h" #include "Common/Input/InputState.h" @@ -1475,6 +1476,8 @@ void EmuScreen::render() { screenManager()->getUIContext()->BeginFrame(); renderUI(); } + + SetVRAppMode(VRAppMode::VR_GAME_MODE); } bool EmuScreen::hasVisibleUI() { diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index 748fcdf86d..7a75cd8806 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -28,6 +28,7 @@ #include "Common/Data/Text/I18n.h" #include "Common/StringUtils.h" #include "Common/System/System.h" +#include "Common/VR/PPSSPPVR.h" #include "Core/Reporting.h" #include "Core/SaveState.h" @@ -355,6 +356,8 @@ void GamePauseScreen::update() { TriggerFinish(DR_CANCEL); finishNextFrame_ = false; } + + SetVRAppMode(VRAppMode::VR_MENU_MODE); } GamePauseScreen::~GamePauseScreen() {