From e93d94d90e15969bb28617a1fcd3182efc5bd99a Mon Sep 17 00:00:00 2001 From: NeatNit Date: Sat, 6 Oct 2018 19:25:43 +0300 Subject: [PATCH] DRY - High-DPI scaled touch put in separate function also fixes a bug where if you start touching (with either mouse or touchscreen) and drag the mouse to the LEFT of the emulator window, the touch point jumps to the RIGHT side of the touchscreen; draggin to above the window would make it jump to the bottom. --- src/citra_qt/bootmanager.cpp | 46 ++++++++++++++++-------------------- src/citra_qt/bootmanager.h | 1 + 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp index 475af2c32..0ae917907 100644 --- a/src/citra_qt/bootmanager.cpp +++ b/src/citra_qt/bootmanager.cpp @@ -216,9 +216,8 @@ void GRenderWindow::mousePressEvent(QMouseEvent* event) { auto pos = event->pos(); if (event->button() == Qt::LeftButton) { - qreal pixelRatio = windowPixelRatio(); - this->TouchPressed(static_cast(pos.x() * pixelRatio), - static_cast(pos.y() * pixelRatio)); + auto [x, y] = ScaleTouch(QPointF(pos)); + this->TouchPressed(x, y); } else if (event->button() == Qt::RightButton) { InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y()); } @@ -229,9 +228,8 @@ void GRenderWindow::mouseMoveEvent(QMouseEvent* event) { return; // touch input is handled in TouchUpdateEvent auto pos = event->pos(); - qreal pixelRatio = windowPixelRatio(); - this->TouchMoved(std::max(static_cast(pos.x() * pixelRatio), 0u), - std::max(static_cast(pos.y() * pixelRatio), 0u)); + auto [x, y] = ScaleTouch(QPointF(pos)); + this->TouchMoved(x, y); InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y()); } @@ -245,39 +243,37 @@ void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) { InputCommon::GetMotionEmu()->EndTilt(); } -void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) { - auto points = event->touchPoints(); - auto tp = points.first(); // there should only be 1 point in TouchBegin - auto pos = tp.pos(); +std::pair GRenderWindow::ScaleTouch(QPointF pos) { + qreal pixel_ratio = windowPixelRatio(); + return {static_cast(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})), + static_cast(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))}; +} - // Copied from mousePressEvent: - qreal pixelRatio = windowPixelRatio(); - this->TouchPressed(static_cast(pos.x() * pixelRatio), - static_cast(pos.y() * pixelRatio)); +void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) { + auto pos = event->touchPoints().first().pos(); // there should only be 1 point in TouchBegin + + auto [x, y] = ScaleTouch(pos); + + this->TouchPressed(x, y); } void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) { - qreal x = 0.0; - qreal y = 0.0; + QPointF pos; int active_points = 0; const auto points = event->touchPoints(); + // average all active touch points for (const auto tp : event->touchPoints()) { if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) { active_points++; - - x += tp.pos().x(); - y += tp.pos().y(); + pos += tp.pos(); } } - x /= active_points; - y /= active_points; + pos /= active_points; - // Copied from mouseMoveEvent: - qreal pixelRatio = windowPixelRatio(); - this->TouchMoved(std::max(static_cast(x * pixelRatio), 0u), - std::max(static_cast(y * pixelRatio), 0u)); + auto [x, y] = ScaleTouch(pos); + this->TouchMoved(x, y); } void GRenderWindow::TouchEndEvent() { diff --git a/src/citra_qt/bootmanager.h b/src/citra_qt/bootmanager.h index 51181c0b9..56f733392 100644 --- a/src/citra_qt/bootmanager.h +++ b/src/citra_qt/bootmanager.h @@ -151,6 +151,7 @@ signals: void Closed(); private: + std::pair ScaleTouch(QPointF pos); void TouchBeginEvent(const QTouchEvent* event); void TouchUpdateEvent(const QTouchEvent* event); void TouchEndEvent();