From f3bd58f75ebc87306f9c0bfba9a277f1b85ddbd8 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Thu, 31 Oct 2024 22:10:25 +0100 Subject: [PATCH] do touchscreen input more properly --- src/frontend/qt_sdl/EmuInstance.h | 6 ++++++ src/frontend/qt_sdl/EmuInstanceInput.cpp | 16 ++++++++++++++++ src/frontend/qt_sdl/EmuThread.cpp | 5 +++++ src/frontend/qt_sdl/Screen.cpp | 21 +++++++-------------- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/frontend/qt_sdl/EmuInstance.h b/src/frontend/qt_sdl/EmuInstance.h index 658247f0..9ab12d93 100644 --- a/src/frontend/qt_sdl/EmuInstance.h +++ b/src/frontend/qt_sdl/EmuInstance.h @@ -147,6 +147,9 @@ public: int getJoystickID() { return joystickID; } SDL_Joystick* getJoystick() { return joystick; } + void touchScreen(int x, int y); + void releaseScreen(); + private: static int lastSep(const std::string& path); std::string getAssetPath(bool gba, const std::string& configpath, const std::string& ext, const std::string& file); @@ -328,6 +331,9 @@ private: melonDS::u32 inputMask; + bool isTouching; + melonDS::u16 touchX, touchY; + friend class EmuThread; friend class MainWindow; }; diff --git a/src/frontend/qt_sdl/EmuInstanceInput.cpp b/src/frontend/qt_sdl/EmuInstanceInput.cpp index bb06c242..aa1c529f 100644 --- a/src/frontend/qt_sdl/EmuInstanceInput.cpp +++ b/src/frontend/qt_sdl/EmuInstanceInput.cpp @@ -74,6 +74,10 @@ void EmuInstance::inputInit() hotkeyMask = 0; lastHotkeyMask = 0; + isTouching = false; + touchX = 0; + touchY = 0; + joystick = nullptr; controller = nullptr; hasRumble = false; @@ -353,3 +357,15 @@ void EmuInstance::inputProcess() hotkeyRelease = lastHotkeyMask & ~hotkeyMask; lastHotkeyMask = hotkeyMask; } + +void EmuInstance::touchScreen(int x, int y) +{ + touchX = x; + touchY = y; + isTouching = true; +} + +void EmuInstance::releaseScreen() +{ + isTouching = false; +} diff --git a/src/frontend/qt_sdl/EmuThread.cpp b/src/frontend/qt_sdl/EmuThread.cpp index f4ce8c94..f0207f1a 100644 --- a/src/frontend/qt_sdl/EmuThread.cpp +++ b/src/frontend/qt_sdl/EmuThread.cpp @@ -251,6 +251,11 @@ void EmuThread::run() // process input and hotkeys emuInstance->nds->SetKeyMask(emuInstance->inputMask); + if (emuInstance->isTouching) + emuInstance->nds->TouchScreen(emuInstance->touchX, emuInstance->touchY); + else + emuInstance->nds->ReleaseScreen(); + if (emuInstance->hotkeyPressed(HK_Lid)) { bool lid = !emuInstance->nds->IsLidClosed(); diff --git a/src/frontend/qt_sdl/Screen.cpp b/src/frontend/qt_sdl/Screen.cpp index 0d05a065..eab5feee 100644 --- a/src/frontend/qt_sdl/Screen.cpp +++ b/src/frontend/qt_sdl/Screen.cpp @@ -258,8 +258,7 @@ void ScreenPanel::mousePressEvent(QMouseEvent* event) if (layout.GetTouchCoords(x, y, false)) { touching = true; - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->TouchScreen(x, y); + emuInstance->touchScreen(x, y); } } @@ -272,8 +271,7 @@ void ScreenPanel::mouseReleaseEvent(QMouseEvent* event) if (touching) { touching = false; - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->ReleaseScreen(); + emuInstance->releaseScreen(); } } @@ -292,8 +290,7 @@ void ScreenPanel::mouseMoveEvent(QMouseEvent* event) if (layout.GetTouchCoords(x, y, true)) { - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->TouchScreen(x, y); + emuInstance->touchScreen(x, y); } } @@ -318,16 +315,14 @@ void ScreenPanel::tabletEvent(QTabletEvent* event) if (layout.GetTouchCoords(x, y, event->type()==QEvent::TabletMove)) { touching = true; - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->TouchScreen(x, y); + emuInstance->touchScreen(x, y); } } break; case QEvent::TabletRelease: if (touching) { - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->ReleaseScreen(); + emuInstance->releaseScreen(); touching = false; } break; @@ -365,16 +360,14 @@ void ScreenPanel::touchEvent(QTouchEvent* event) if (layout.GetTouchCoords(x, y, event->type()==QEvent::TouchUpdate)) { touching = true; - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->TouchScreen(x, y); + emuInstance->touchScreen(x, y); } } break; case QEvent::TouchEnd: if (touching) { - assert(emuInstance->getNDS() != nullptr); - emuInstance->getNDS()->ReleaseScreen(); + emuInstance->releaseScreen(); touching = false; } break;