Add mutitouch support for touch screens

This commit is contained in:
german 2021-01-02 22:04:50 -06:00
parent f452fbfc88
commit df1070a735
10 changed files with 138 additions and 86 deletions

View file

@ -394,7 +394,7 @@ void GRenderWindow::mousePressEvent(QMouseEvent* event) {
input_subsystem->GetMouse()->PressButton(x, y, event->button());
if (event->button() == Qt::LeftButton) {
this->TouchPressed(x, y);
this->TouchPressed(x, y, 0);
}
emit MouseActivity();
@ -409,7 +409,7 @@ void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
auto pos = event->pos();
const auto [x, y] = ScaleTouch(pos);
input_subsystem->GetMouse()->MouseMove(x, y);
this->TouchMoved(x, y);
this->TouchMoved(x, y, 0);
emit MouseActivity();
}
@ -423,36 +423,71 @@ void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
input_subsystem->GetMouse()->ReleaseButton(event->button());
if (event->button() == Qt::LeftButton) {
this->TouchReleased();
this->TouchReleased(0);
}
}
void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
// TouchBegin always has exactly one touch point, so take the .first()
const auto [x, y] = ScaleTouch(event->touchPoints().first().pos());
this->TouchPressed(x, y);
QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
for (const auto& touch_point : touch_points) {
if (!TouchUpdate(touch_point)) {
TouchStart(touch_point);
}
}
}
void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
QPointF pos;
int active_points = 0;
// average all active touch points
for (const auto& tp : event->touchPoints()) {
if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) {
active_points++;
pos += tp.pos();
QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
for (const auto& touch_point : touch_points) {
if (!TouchUpdate(touch_point)) {
TouchStart(touch_point);
}
}
// Release all inactive points
for (std::size_t id = 0; id < touch_ids.size(); ++id) {
if (!TouchExist(touch_ids[id], touch_points)) {
touch_ids[id] = 0;
this->TouchReleased(id + 1);
}
}
pos /= active_points;
const auto [x, y] = ScaleTouch(pos);
this->TouchMoved(x, y);
}
void GRenderWindow::TouchEndEvent() {
this->TouchReleased();
for (std::size_t id = 0; id < touch_ids.size(); ++id) {
if (touch_ids[id] != 0) {
touch_ids[id] = 0;
this->TouchReleased(id + 1);
}
}
}
bool GRenderWindow::TouchStart(const QTouchEvent::TouchPoint& touch_point) {
for (std::size_t id = 0; id < touch_ids.size(); ++id) {
if (touch_ids[id] == 0) {
touch_ids[id] = touch_point.id() + 1;
const auto [x, y] = ScaleTouch(touch_point.pos());
this->TouchPressed(x, y, id + 1);
return true;
}
}
return false;
}
bool GRenderWindow::TouchUpdate(const QTouchEvent::TouchPoint& touch_point) {
for (std::size_t id = 0; id < touch_ids.size(); ++id) {
if (touch_ids[id] == touch_point.id() + 1) {
const auto [x, y] = ScaleTouch(touch_point.pos());
this->TouchMoved(x, y, id + 1);
return true;
}
}
return false;
}
bool GRenderWindow::TouchExist(std::size_t id,
const QList<QTouchEvent::TouchPoint>& touch_points) const {
return std::any_of(touch_points.begin(), touch_points.end(),
[id](const auto& point) { return id == point.id() + 1; });
}
bool GRenderWindow::event(QEvent* event) {