From 2821035b1dce157184e855fbe82816c029c7117c Mon Sep 17 00:00:00 2001 From: iota97 Date: Mon, 30 Aug 2021 12:26:13 +0200 Subject: [PATCH] Support virutal button in touch gesture --- UI/GameSettingsScreen.cpp | 15 ++++++- UI/GamepadEmu.cpp | 26 +++++------- UI/GamepadEmu.h | 87 +++++++++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 39 deletions(-) diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 71dc7dd267..e6618d486e 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -729,10 +729,12 @@ void GameSettingsScreen::CreateViews() { static const char *touchControlStyles[] = {"Classic", "Thin borders", "Glowing borders"}; View *style = controlsSettings->Add(new PopupMultiChoice(&g_Config.iTouchButtonStyle, co->T("Button style"), touchControlStyles, 0, ARRAY_SIZE(touchControlStyles), co->GetName(), screenManager())); style->SetEnabledPtr(&g_Config.bShowTouchControls); - controlsSettings->Add(new Choice(co->T("Gesture mapping")))->OnClick.Add([=](EventParams &e) { + Choice *gesture = controlsSettings->Add(new Choice(co->T("Gesture mapping"))); + gesture->OnClick.Add([=](EventParams &e) { screenManager()->push(new GestureMappingScreen()); return UI::EVENT_DONE; }); + gesture->SetEnabledPtr(&g_Config.bShowTouchControls); } controlsSettings->Add(new ItemHeader(co->T("Keyboard", "Keyboard Control Settings"))); @@ -2007,14 +2009,23 @@ void GestureMappingScreen::CreateViews() { LinearLayout *vert = rightPanel->Add(new LinearLayout(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, FILL_PARENT))); vert->SetSpacing(0); - static const char *gestureButton[] = {"None", "L", "R", "Square", "Triangle", "Circle", "Cross", "D-pad up", "D-pad down", "D-pad left", "D-pad right", "Start", "Select"}; + static const char *gestureButton[ARRAY_SIZE(GestureKey::keyList)+1]; + gestureButton[0] = "None"; + for (int i = 1; i < ARRAY_SIZE(gestureButton); ++i) { + gestureButton[i] = GestureKey::keyList[i-1].n; + } + vert->Add(new CheckBox(&g_Config.bGestureControlEnabled, co->T("Enable gesture control"))); + + vert->Add(new ItemHeader(co->T("Swipe"))); vert->Add(new PopupMultiChoice(&g_Config.iSwipeUp, mc->T("Swipe Up"), gestureButton, 0, ARRAY_SIZE(gestureButton), mc->GetName(), screenManager()))->SetEnabledPtr(&g_Config.bGestureControlEnabled); vert->Add(new PopupMultiChoice(&g_Config.iSwipeDown, mc->T("Swipe Down"), gestureButton, 0, ARRAY_SIZE(gestureButton), mc->GetName(), screenManager()))->SetEnabledPtr(&g_Config.bGestureControlEnabled); vert->Add(new PopupMultiChoice(&g_Config.iSwipeLeft, mc->T("Swipe Left"), gestureButton, 0, ARRAY_SIZE(gestureButton), mc->GetName(), screenManager()))->SetEnabledPtr(&g_Config.bGestureControlEnabled); vert->Add(new PopupMultiChoice(&g_Config.iSwipeRight, mc->T("Swipe Right"), gestureButton, 0, ARRAY_SIZE(gestureButton), mc->GetName(), screenManager()))->SetEnabledPtr(&g_Config.bGestureControlEnabled); vert->Add(new PopupSliderChoiceFloat(&g_Config.fSwipeSensitivity, 0.01f, 1.0f, co->T("Swipe sensitivity"), 0.01f, screenManager(), "x"))->SetEnabledPtr(&g_Config.bGestureControlEnabled); vert->Add(new PopupSliderChoiceFloat(&g_Config.fSwipeSmoothing, 0.0f, 0.95f, co->T("Swipe smoothing"), 0.05f, screenManager(), "x"))->SetEnabledPtr(&g_Config.bGestureControlEnabled); + + vert->Add(new ItemHeader(co->T("Double tap"))); vert->Add(new PopupMultiChoice(&g_Config.iDoubleTapGesture, mc->T("Double tap button"), gestureButton, 0, ARRAY_SIZE(gestureButton), mc->GetName(), screenManager()))->SetEnabledPtr(&g_Config.bGestureControlEnabled); } diff --git a/UI/GamepadEmu.cpp b/UI/GamepadEmu.cpp index b1f01c485a..b707882e8d 100644 --- a/UI/GamepadEmu.cpp +++ b/UI/GamepadEmu.cpp @@ -827,15 +827,13 @@ UI::ViewGroup *CreatePadLayout(float xres, float yres, bool *pause, bool showPau addComboKey(g_Config.CustomKey9, "Custom 10 button", g_Config.touchCombo9); if (g_Config.bGestureControlEnabled) - root->Add(new GestureGamepad()); + root->Add(new GestureGamepad(controllMapper)); return root; } void GestureGamepad::Touch(const TouchInput &input) { - static const int button[16] = {CTRL_LTRIGGER, CTRL_RTRIGGER, CTRL_SQUARE, CTRL_TRIANGLE, CTRL_CIRCLE, CTRL_CROSS, CTRL_UP, CTRL_DOWN, CTRL_LEFT, CTRL_RIGHT, CTRL_START, CTRL_SELECT}; - if (usedPointerMask & (1 << input.id)) { if (input.id == dragPointerId_) dragPointerId_ = -1; @@ -856,7 +854,7 @@ void GestureGamepad::Touch(const TouchInput &input) { const float now = time_now_d(); if (now - lastTapRelease_ < 0.3f && !haveDoubleTapped_) { if (g_Config.iDoubleTapGesture != 0 ) - __CtrlButtonDown(button[g_Config.iDoubleTapGesture-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iDoubleTapGesture-1].c, KEY_DOWN); haveDoubleTapped_ = true; } @@ -879,7 +877,7 @@ void GestureGamepad::Touch(const TouchInput &input) { if (haveDoubleTapped_) { if (g_Config.iDoubleTapGesture != 0) - __CtrlButtonUp(button[g_Config.iDoubleTapGesture-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iDoubleTapGesture-1].c, KEY_UP); haveDoubleTapped_ = false; } } @@ -887,44 +885,42 @@ void GestureGamepad::Touch(const TouchInput &input) { } void GestureGamepad::Update() { - static const int button[16] = {CTRL_LTRIGGER, CTRL_RTRIGGER, CTRL_SQUARE, CTRL_TRIANGLE, CTRL_CIRCLE, CTRL_CROSS, CTRL_UP, CTRL_DOWN, CTRL_LEFT, CTRL_RIGHT, CTRL_START, CTRL_SELECT}; - const float th = 1.0f; float dx = deltaX_ * g_dpi_scale_x * g_Config.fSwipeSensitivity; float dy = deltaY_ * g_dpi_scale_y * g_Config.fSwipeSensitivity; if (g_Config.iSwipeRight != 0) { if (dx > th) { - __CtrlButtonDown(button[g_Config.iSwipeRight-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeRight-1].c, KEY_DOWN); swipeRightReleased_ = false; } else if (!swipeRightReleased_) { - __CtrlButtonUp(button[g_Config.iSwipeRight-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeRight-1].c, KEY_UP); swipeRightReleased_ = true; } } if (g_Config.iSwipeLeft != 0) { if (dx < -th) { - __CtrlButtonDown(button[g_Config.iSwipeLeft-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeLeft-1].c, KEY_DOWN); swipeLeftReleased_ = false; } else if (!swipeLeftReleased_) { - __CtrlButtonUp(button[g_Config.iSwipeLeft-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeLeft-1].c, KEY_UP); swipeLeftReleased_ = true; } } if (g_Config.iSwipeUp != 0) { if (dy < -th) { - __CtrlButtonDown(button[g_Config.iSwipeUp-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeUp-1].c, KEY_DOWN); swipeUpReleased_ = false; } else if (!swipeUpReleased_) { - __CtrlButtonUp(button[g_Config.iSwipeUp-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeUp-1].c, KEY_UP); swipeUpReleased_ = true; } } if (g_Config.iSwipeDown != 0) { if (dy > th) { - __CtrlButtonDown(button[g_Config.iSwipeDown-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeDown-1].c, KEY_DOWN); swipeDownReleased_ = false; } else if (!swipeDownReleased_) { - __CtrlButtonUp(button[g_Config.iSwipeDown-1]); + controllMapper_->pspKey(GestureKey::keyList[g_Config.iSwipeDown-1].c, KEY_UP); swipeDownReleased_ = true; } } diff --git a/UI/GamepadEmu.h b/UI/GamepadEmu.h index 7dc4261724..cd7f2658e6 100644 --- a/UI/GamepadEmu.h +++ b/UI/GamepadEmu.h @@ -184,6 +184,30 @@ private: bool invertedContextDimension_; // Swap width and height }; +class GestureGamepad : public UI::View { +public: + GestureGamepad(ControlMapper* controllMapper) : controllMapper_(controllMapper) {}; + + void Touch(const TouchInput &input) override; + void Update() override; + +protected: + + float lastX_ = 0.0f; + float lastY_ = 0.0f; + float deltaX_ = 0.0f; + float deltaY_ = 0.0f; + float lastTapRelease_ = 0.0f; + float lastTouchDown_ = 0.0f; + int dragPointerId_ = -1; + bool swipeLeftReleased_ = true; + bool swipeRightReleased_ = true; + bool swipeUpReleased_ = true; + bool swipeDownReleased_ = true; + bool haveDoubleTapped_ = false; + ControlMapper* controllMapper_; +}; + // Just edit this to add new image, shape or button function namespace CustomKey { // Image list @@ -289,25 +313,44 @@ namespace CustomKey { static_assert(ARRAY_SIZE(comboKeyList) <= 64, "Too many key for a uint64_t bit mask"); }; -class GestureGamepad : public UI::View { -public: - GestureGamepad() {}; - - void Touch(const TouchInput &input) override; - void Update() override; - -protected: - - float lastX_ = 0.0f; - float lastY_ = 0.0f; - float deltaX_ = 0.0f; - float deltaY_ = 0.0f; - float lastTapRelease_ = 0.0f; - float lastTouchDown_ = 0.0f; - int dragPointerId_ = -1; - bool swipeLeftReleased_ = true; - bool swipeRightReleased_ = true; - bool swipeUpReleased_ = true; - bool swipeDownReleased_ = true; - bool haveDoubleTapped_ = false; -}; +// Gesture key only have virtual button that can work without constant press +namespace GestureKey { + struct key { + const char* n; // UI name + uint32_t c; // Key code + }; + static const key keyList[] = { + { "Square", CTRL_SQUARE }, + { "Triangle", CTRL_TRIANGLE }, + { "Circle", CTRL_CIRCLE }, + { "Cross", CTRL_CROSS }, + { "Up", CTRL_UP }, + { "Down", CTRL_DOWN }, + { "Left", CTRL_LEFT }, + { "Right", CTRL_RIGHT }, + { "Start", CTRL_START }, + { "Select", CTRL_SELECT }, + { "L", CTRL_LTRIGGER }, + { "R", CTRL_RTRIGGER }, + { "An.Up", VIRTKEY_AXIS_Y_MAX }, + { "An.Down", VIRTKEY_AXIS_Y_MIN }, + { "An.Left", VIRTKEY_AXIS_X_MIN }, + { "An.Right", VIRTKEY_AXIS_X_MAX }, + { "SpeedToggle", VIRTKEY_SPEED_TOGGLE }, + { "Rewind", VIRTKEY_REWIND }, + { "Save State", VIRTKEY_SAVE_STATE }, + { "Load State", VIRTKEY_LOAD_STATE }, + { "Next Slot", VIRTKEY_NEXT_SLOT }, + { "Toggle Fullscreen", VIRTKEY_TOGGLE_FULLSCREEN }, + { "Texture Dumping", VIRTKEY_TEXTURE_DUMP }, + { "Texture Replacement", VIRTKEY_TEXTURE_REPLACE }, + { "Screenshot", VIRTKEY_SCREENSHOT }, + { "Mute toggle", VIRTKEY_MUTE_TOGGLE }, + { "OpenChat", VIRTKEY_OPENCHAT }, + { "Pause", VIRTKEY_PAUSE }, + { "DevMenu", VIRTKEY_DEVMENU }, +#ifndef MOBILE_DEVICE + { "Record", VIRTKEY_RECORD }, +#endif + }; +}