[Qt] Add touchscreen

This commit is contained in:
wheremyfoodat 2024-01-29 21:07:21 +02:00
parent eabd22423c
commit 9cc3fc0c4c
2 changed files with 49 additions and 0 deletions

View file

@ -3,6 +3,7 @@
#include <QDesktopServices>
#include <QFileDialog>
#include <QString>
#include <cmath>
#include <cstdio>
#include <fstream>
@ -277,6 +278,10 @@ void MainWindow::dispatchMessage(const EmulatorMessage& message) {
case MessageType::ReleaseKey: emu->getServiceManager().getHID().releaseKey(message.key.key); break;
case MessageType::SetCirclePadX: emu->getServiceManager().getHID().setCirclepadX(message.circlepad.value); break;
case MessageType::SetCirclePadY: emu->getServiceManager().getHID().setCirclepadY(message.circlepad.value); break;
case MessageType::PressTouchscreen:
emu->getServiceManager().getHID().setTouchScreenPress(message.touchscreen.x, message.touchscreen.y);
break;
case MessageType::ReleaseTouchscreen: emu->getServiceManager().getHID().releaseTouchScreen(); break;
}
}
@ -357,6 +362,40 @@ void MainWindow::keyReleaseEvent(QKeyEvent* event) {
}
}
void MainWindow::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::MouseButton::LeftButton) {
const QPointF clickPos = event->globalPosition();
const QPointF widgetPos = screen.mapFromGlobal(clickPos);
// Press is inside the screen area
if (widgetPos.x() >= 0 && widgetPos.x() < screen.width() && widgetPos.y() >= 0 && widgetPos.y() < screen.height()) {
// Go from widget positions to [0, 400) for x and [0, 480) for y
uint x = (uint)std::round(widgetPos.x() / screen.width() * 400.f);
uint y = (uint)std::round(widgetPos.y() / screen.height() * 480.f);
// Check if touch falls in the touch screen area
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {
// Convert to 3DS coordinates
u16 x_converted = static_cast<u16>(x) - 40;
u16 y_converted = static_cast<u16>(y) - 240;
EmulatorMessage message{.type = MessageType::PressTouchscreen};
message.touchscreen.x = x_converted;
message.touchscreen.y = y_converted;
sendMessage(message);
} else {
sendMessage(EmulatorMessage{.type = MessageType::ReleaseTouchscreen});
}
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent* event) {
if (event->button() == Qt::MouseButton::LeftButton) {
sendMessage(EmulatorMessage{.type = MessageType::ReleaseTouchscreen});
}
}
void MainWindow::loadLuaScript(const std::string& code) {
EmulatorMessage message{.type = MessageType::LoadLuaScript};