Qt: Handle mouse move events properly (#678)

This commit is contained in:
wheremyfoodat 2024-12-10 00:54:06 +02:00 committed by GitHub
parent 79d24cba11
commit 4ce0768ba1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 22 deletions

View file

@ -146,12 +146,15 @@ class MainWindow : public QMainWindow {
void closeEvent(QCloseEvent* event) override; void closeEvent(QCloseEvent* event) override;
void keyPressEvent(QKeyEvent* event) override; void keyPressEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override;
void mousePressEvent(QMouseEvent* event) override; void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override; void mouseReleaseEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void loadLuaScript(const std::string& code); void loadLuaScript(const std::string& code);
void reloadShader(const std::string& shader); void reloadShader(const std::string& shader);
void editCheat(u32 handle, const std::vector<uint8_t>& cheat, const std::function<void(u32)>& callback); void editCheat(u32 handle, const std::vector<uint8_t>& cheat, const std::function<void(u32)>& callback);
void handleScreenResize(u32 width, u32 height); void handleScreenResize(u32 width, u32 height);
void handleTouchscreenPress(QMouseEvent* event);
}; };

View file

@ -487,29 +487,14 @@ void MainWindow::keyReleaseEvent(QKeyEvent* event) {
void MainWindow::mousePressEvent(QMouseEvent* event) { void MainWindow::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::MouseButton::LeftButton) { if (event->button() == Qt::MouseButton::LeftButton) {
const QPointF clickPos = event->globalPosition(); // We handle actual mouse press & movement logic inside the mouseMoveEvent handler
const QPointF widgetPos = screen->mapFromGlobal(clickPos); handleTouchscreenPress(event);
}
}
// Press is inside the screen area void MainWindow::mouseMoveEvent(QMouseEvent* event) {
if (widgetPos.x() >= 0 && widgetPos.x() < screen->width() && widgetPos.y() >= 0 && widgetPos.y() < screen->height()) { if (event->buttons().testFlag(Qt::MouseButton::LeftButton)) {
// Go from widget positions to [0, 400) for x and [0, 480) for y handleTouchscreenPress(event);
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});
}
}
} }
} }
@ -519,6 +504,32 @@ void MainWindow::mouseReleaseEvent(QMouseEvent* event) {
} }
} }
void MainWindow::handleTouchscreenPress(QMouseEvent* event) {
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::loadLuaScript(const std::string& code) { void MainWindow::loadLuaScript(const std::string& code) {
EmulatorMessage message{.type = MessageType::LoadLuaScript}; EmulatorMessage message{.type = MessageType::LoadLuaScript};