From 6fc073ffa74cf9be3f354212032052ac6ae9f3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 8 Mar 2020 16:41:31 +0100 Subject: [PATCH] UI: Simple joystick navigation. Fixes #10996. --- UI/ChatScreen.cpp | 3 +-- ext/native/ui/root.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/UI/ChatScreen.cpp b/UI/ChatScreen.cpp index bf82b6a3ac..21d4dfba5c 100644 --- a/UI/ChatScreen.cpp +++ b/UI/ChatScreen.cpp @@ -25,8 +25,7 @@ void ChatMenu::CreatePopupContents(UI::ViewGroup *parent) { chatEdit_ = bottom->Add(new TextEdit("", n->T("Chat Here"), new LinearLayoutParams(1.0))); #if defined(USING_WIN_UI) //freeze the ui when using ctrl + C hotkey need workaround - if (g_Config.bBypassOSKWithKeyboard && !g_Config.bFullScreen) - { + if (g_Config.bBypassOSKWithKeyboard && !g_Config.bFullScreen) { std::wstring titleText = ConvertUTF8ToWString(n->T("Chat")); std::wstring defaultText = ConvertUTF8ToWString(n->T("Chat Here")); std::wstring inputChars; diff --git a/ext/native/ui/root.cpp b/ext/native/ui/root.cpp index 1419d05c9e..b379511d6b 100644 --- a/ext/native/ui/root.cpp +++ b/ext/native/ui/root.cpp @@ -246,6 +246,65 @@ bool TouchEvent(const TouchInput &touch, ViewGroup *root) { } bool AxisEvent(const AxisInput &axis, ViewGroup *root) { + enum { + DIR_POS = 1, + DIR_NEG = 2, + }; + + static uint32_t x_state = 0; + static uint32_t y_state = 0; + + const float THRESHOLD = 0.75; + + // Cannot use the remapper since this is for the menu, so we provide our own + // axis->button emulation here. + auto GenerateKeyFromAxis = [&](uint32_t old, uint32_t cur, keycode_t neg_key, keycode_t pos_key) { + if (old == cur) + return; + if (old == DIR_POS) { + KeyEvent(KeyInput{ DEVICE_ID_KEYBOARD, pos_key, KEY_UP }, root); + } else if (old == DIR_NEG) { + KeyEvent(KeyInput{ DEVICE_ID_KEYBOARD, neg_key, KEY_UP }, root); + } + if (cur == DIR_POS) { + KeyEvent(KeyInput{ DEVICE_ID_KEYBOARD, pos_key, KEY_DOWN }, root); + } else if (cur == DIR_NEG) { + KeyEvent(KeyInput{ DEVICE_ID_KEYBOARD, neg_key, KEY_DOWN }, root); + } + }; + + switch (axis.deviceId) { + case DEVICE_ID_PAD_0: + case DEVICE_ID_PAD_1: + case DEVICE_ID_PAD_2: + case DEVICE_ID_PAD_3: + case DEVICE_ID_X360_0: + case DEVICE_ID_X360_1: + case DEVICE_ID_X360_2: + case DEVICE_ID_X360_3: + { + uint32_t dir = 0; + if (axis.axisId == JOYSTICK_AXIS_X) { + if (axis.value < -THRESHOLD) + dir = DIR_NEG; + else if (axis.value > THRESHOLD) + dir = DIR_POS; + GenerateKeyFromAxis(x_state, dir, NKCODE_DPAD_LEFT, NKCODE_DPAD_RIGHT); + x_state = dir; + } + if (axis.axisId == JOYSTICK_AXIS_Y) { + if (axis.value < -THRESHOLD) + dir = DIR_NEG; + else if (axis.value > THRESHOLD) + dir = DIR_POS; + // TODO: What do we do with devices that are reversed... ? + GenerateKeyFromAxis(y_state, dir, NKCODE_DPAD_DOWN, NKCODE_DPAD_UP); + y_state = dir; + } + break; + } + } + root->Axis(axis); return true; }