diff --git a/CREDITS b/CREDITS index b9a9aad..4ad351c 100644 --- a/CREDITS +++ b/CREDITS @@ -4,6 +4,7 @@ - HurricanePootis - AUR maintainer - lunairekitty - Additional CLI creation messages - partyvan - Distance mapping support, initial big-endian support +- mikesk8r - Touchscreen support ## Artists - [pastacrylic](https://linktr.ee/pastacrylic) - Splashscreen diff --git a/src/gui/widgets/QMareTextureWidget.cpp b/src/gui/widgets/QMareTextureWidget.cpp index 797c46b..e9751a5 100644 --- a/src/gui/widgets/QMareTextureWidget.cpp +++ b/src/gui/widgets/QMareTextureWidget.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,7 @@ namespace { QMareTextureWidget::QMareTextureWidget(QWidget* parent) : QWidget{parent} { this->setContextMenuPolicy(Qt::CustomContextMenu); + this->setAttribute(Qt::WA_AcceptTouchEvents); auto* contextMenu = new QMenu{this}; @@ -238,7 +240,7 @@ uint8_t QMareTextureWidget::getCurrentMip() const { } void QMareTextureWidget::setCurrentMip(uint8_t mip) { - this->currentMip = std::clamp(mip, 0, this->vtf.getMipCount() - 1); + this->currentMip = qBound(0, mip, this->vtf.getMipCount() - 1); this->reloadCurrentTexture(); } @@ -247,7 +249,7 @@ uint16_t QMareTextureWidget::getCurrentFrame() const { } void QMareTextureWidget::setCurrentFrame(uint16_t frame) { - this->currentFrame = std::clamp(frame, 0, this->vtf.getFrameCount() - 1); + this->currentFrame = qBound(0, frame, this->vtf.getFrameCount() - 1); this->reloadCurrentTexture(); } @@ -256,7 +258,7 @@ uint8_t QMareTextureWidget::getCurrentFace() const { } void QMareTextureWidget::setCurrentFace(uint8_t face) { - this->currentFace = std::clamp(face, 0, this->vtf.getFaceCount() - 1); + this->currentFace = qBound(0, face, this->vtf.getFaceCount() - 1); this->reloadCurrentTexture(); } @@ -265,7 +267,7 @@ uint16_t QMareTextureWidget::getCurrentDepth() const { } void QMareTextureWidget::setCurrentDepth(uint16_t depth) { - this->currentDepth = std::clamp(depth, 0, this->vtf.getDepth() - 1); + this->currentDepth = qBound(0, depth, this->vtf.getDepth() - 1); this->reloadCurrentTexture(); } @@ -328,6 +330,31 @@ QMareTextureWidget::operator bool() const { return !this->path.isEmpty() && !this->textureCurrent.isNull(); } +bool QMareTextureWidget::event(QEvent* e) { + if ( + QTouchEvent* touch; + (e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd) && ((touch = dynamic_cast(e))) + ) { + if (const auto& points = touch->points(); points.length() >= 2) { + const auto point0 = points[0].position(); + const auto point1 = points[1].position(); + const auto rawDistance = static_cast(qSqrt(qPow(point1.x() - point0.x(), 2) + qPow(point1.y() - point0.y(), 2))); + + if (e->type() == QEvent::TouchBegin) { + this->previousDistance = rawDistance; + } + const auto distance = qBound(0.5f, rawDistance / this->previousDistance, 5.f); + this->previousDistance = rawDistance; + this->textureZoom *= distance; + + this->update(); + e->accept(); + return true; + } + } + return this->QWidget::event(e); +} + void QMareTextureWidget::mouseMoveEvent(QMouseEvent* e) { if (!(e->buttons() & Qt::LeftButton || e->buttons() & Qt::MiddleButton)) { return; diff --git a/src/gui/widgets/QMareTextureWidget.h b/src/gui/widgets/QMareTextureWidget.h index d7ed11c..87839b2 100644 --- a/src/gui/widgets/QMareTextureWidget.h +++ b/src/gui/widgets/QMareTextureWidget.h @@ -3,6 +3,7 @@ #include #include +class QEvent; class QMouseEvent; class QPaintEvent; class QResizeEvent; @@ -73,6 +74,8 @@ class QMareTextureWidget : public QWidget { explicit operator bool() const; protected: + bool event(QEvent* e) override; + void mouseMoveEvent(QMouseEvent* e) override; void mousePressEvent(QMouseEvent* e) override; @@ -91,6 +94,7 @@ class QMareTextureWidget : public QWidget { QImage textureCurrent; QPointF textureOffset; float textureZoom = 1.f; + float previousDistance = 1.f; uint8_t currentMip = 0; uint8_t currentFace = 0; uint16_t currentFrame = 0;