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.
This commit is contained in:
NeatNit 2018-10-06 19:25:43 +03:00
parent 5a4848f1c2
commit e93d94d90e
2 changed files with 22 additions and 25 deletions

View file

@ -216,9 +216,8 @@ void GRenderWindow::mousePressEvent(QMouseEvent* event) {
auto pos = event->pos(); auto pos = event->pos();
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
qreal pixelRatio = windowPixelRatio(); auto [x, y] = ScaleTouch(QPointF(pos));
this->TouchPressed(static_cast<unsigned>(pos.x() * pixelRatio), this->TouchPressed(x, y);
static_cast<unsigned>(pos.y() * pixelRatio));
} else if (event->button() == Qt::RightButton) { } else if (event->button() == Qt::RightButton) {
InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y()); InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y());
} }
@ -229,9 +228,8 @@ void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
return; // touch input is handled in TouchUpdateEvent return; // touch input is handled in TouchUpdateEvent
auto pos = event->pos(); auto pos = event->pos();
qreal pixelRatio = windowPixelRatio(); auto [x, y] = ScaleTouch(QPointF(pos));
this->TouchMoved(std::max(static_cast<unsigned>(pos.x() * pixelRatio), 0u), this->TouchMoved(x, y);
std::max(static_cast<unsigned>(pos.y() * pixelRatio), 0u));
InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y()); InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y());
} }
@ -245,39 +243,37 @@ void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
InputCommon::GetMotionEmu()->EndTilt(); InputCommon::GetMotionEmu()->EndTilt();
} }
void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) { std::pair<unsigned, unsigned> GRenderWindow::ScaleTouch(QPointF pos) {
auto points = event->touchPoints(); qreal pixel_ratio = windowPixelRatio();
auto tp = points.first(); // there should only be 1 point in TouchBegin return {static_cast<unsigned>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
auto pos = tp.pos(); static_cast<unsigned>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
}
// Copied from mousePressEvent: void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
qreal pixelRatio = windowPixelRatio(); auto pos = event->touchPoints().first().pos(); // there should only be 1 point in TouchBegin
this->TouchPressed(static_cast<unsigned>(pos.x() * pixelRatio),
static_cast<unsigned>(pos.y() * pixelRatio)); auto [x, y] = ScaleTouch(pos);
this->TouchPressed(x, y);
} }
void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) { void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
qreal x = 0.0; QPointF pos;
qreal y = 0.0;
int active_points = 0; int active_points = 0;
const auto points = event->touchPoints(); const auto points = event->touchPoints();
// average all active touch points
for (const auto tp : event->touchPoints()) { for (const auto tp : event->touchPoints()) {
if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) { if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) {
active_points++; active_points++;
pos += tp.pos();
x += tp.pos().x();
y += tp.pos().y();
} }
} }
x /= active_points; pos /= active_points;
y /= active_points;
// Copied from mouseMoveEvent: auto [x, y] = ScaleTouch(pos);
qreal pixelRatio = windowPixelRatio(); this->TouchMoved(x, y);
this->TouchMoved(std::max(static_cast<unsigned>(x * pixelRatio), 0u),
std::max(static_cast<unsigned>(y * pixelRatio), 0u));
} }
void GRenderWindow::TouchEndEvent() { void GRenderWindow::TouchEndEvent() {

View file

@ -151,6 +151,7 @@ signals:
void Closed(); void Closed();
private: private:
std::pair<unsigned, unsigned> ScaleTouch(QPointF pos);
void TouchBeginEvent(const QTouchEvent* event); void TouchBeginEvent(const QTouchEvent* event);
void TouchUpdateEvent(const QTouchEvent* event); void TouchUpdateEvent(const QTouchEvent* event);
void TouchEndEvent(); void TouchEndEvent();