Skip to content

Commit 7645c3a

Browse files
committed
gui: add touch support for texture widget
1 parent a52abba commit 7645c3a

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

CREDITS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- HurricanePootis - AUR maintainer
55
- lunairekitty - Additional CLI creation messages
66
- partyvan - Initial big-endian support
7+
- mikesk8r - Touchscreen support
78

89
## Artists
910
- [pastacrylic](https://linktr.ee/pastacrylic) - Splashscreen

src/gui/widgets/QMareTextureWidget.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QApplication>
77
#include <QClipboard>
88
#include <QFileDialog>
9+
#include <QEvent>
910
#include <QMenu>
1011
#include <QMessageBox>
1112
#include <QMouseEvent>
@@ -42,6 +43,7 @@ namespace {
4243

4344
QMareTextureWidget::QMareTextureWidget(QWidget* parent) : QWidget{parent} {
4445
this->setContextMenuPolicy(Qt::CustomContextMenu);
46+
this->setAttribute(Qt::WA_AcceptTouchEvents);
4547

4648
auto* contextMenu = new QMenu{this};
4749

@@ -231,7 +233,7 @@ uint8_t QMareTextureWidget::getCurrentMip() const {
231233
}
232234

233235
void QMareTextureWidget::setCurrentMip(uint8_t mip) {
234-
this->currentMip = std::clamp<uint8_t>(mip, 0, this->vtf.getMipCount() - 1);
236+
this->currentMip = qBound<uint8_t>(0, mip, this->vtf.getMipCount() - 1);
235237
this->reloadCurrentTexture();
236238
}
237239

@@ -240,7 +242,7 @@ uint16_t QMareTextureWidget::getCurrentFrame() const {
240242
}
241243

242244
void QMareTextureWidget::setCurrentFrame(uint16_t frame) {
243-
this->currentFrame = std::clamp<uint16_t>(frame, 0, this->vtf.getFrameCount() - 1);
245+
this->currentFrame = qBound<uint16_t>(0, frame, this->vtf.getFrameCount() - 1);
244246
this->reloadCurrentTexture();
245247
}
246248

@@ -249,7 +251,7 @@ uint8_t QMareTextureWidget::getCurrentFace() const {
249251
}
250252

251253
void QMareTextureWidget::setCurrentFace(uint8_t face) {
252-
this->currentFace = std::clamp<uint8_t>(face, 0, this->vtf.getFaceCount() - 1);
254+
this->currentFace = qBound<uint8_t>(0, face, this->vtf.getFaceCount() - 1);
253255
this->reloadCurrentTexture();
254256
}
255257

@@ -258,7 +260,7 @@ uint16_t QMareTextureWidget::getCurrentDepth() const {
258260
}
259261

260262
void QMareTextureWidget::setCurrentDepth(uint16_t depth) {
261-
this->currentDepth = std::clamp<uint16_t>(depth, 0, this->vtf.getDepth() - 1);
263+
this->currentDepth = qBound<uint16_t>(0, depth, this->vtf.getDepth() - 1);
262264
this->reloadCurrentTexture();
263265
}
264266

@@ -320,6 +322,31 @@ QMareTextureWidget::operator bool() const {
320322
return !this->path.isEmpty() && !this->textureCurrent.isNull();
321323
}
322324

325+
bool QMareTextureWidget::event(QEvent* e) {
326+
if (
327+
QTouchEvent* touch;
328+
(e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd) && ((touch = dynamic_cast<QTouchEvent*>(e)))
329+
) {
330+
if (const auto& points = touch->points(); points.length() >= 2) {
331+
const auto point0 = points[0].position();
332+
const auto point1 = points[1].position();
333+
const auto rawDistance = static_cast<float>(qSqrt(qPow(point1.x() - point0.x(), 2) + qPow(point1.y() - point0.y(), 2)));
334+
335+
if (e->type() == QEvent::TouchBegin) {
336+
this->previousDistance = rawDistance;
337+
}
338+
const auto distance = qBound(0.5f, rawDistance / this->previousDistance, 5.f);
339+
this->previousDistance = rawDistance;
340+
this->textureZoom *= distance;
341+
342+
this->update();
343+
e->accept();
344+
return true;
345+
}
346+
}
347+
return this->QWidget::event(e);
348+
}
349+
323350
void QMareTextureWidget::mouseMoveEvent(QMouseEvent* e) {
324351
if (!(e->buttons() & Qt::LeftButton || e->buttons() & Qt::MiddleButton)) {
325352
return;

src/gui/widgets/QMareTextureWidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <QWidget>
44
#include <vtfpp/VTF.h>
55

6+
class QEvent;
67
class QMouseEvent;
78
class QPaintEvent;
89
class QResizeEvent;
@@ -73,6 +74,8 @@ class QMareTextureWidget : public QWidget {
7374
explicit operator bool() const;
7475

7576
protected:
77+
bool event(QEvent* e) override;
78+
7679
void mouseMoveEvent(QMouseEvent* e) override;
7780

7881
void mousePressEvent(QMouseEvent* e) override;
@@ -91,6 +94,7 @@ class QMareTextureWidget : public QWidget {
9194
QImage textureCurrent;
9295
QPointF textureOffset;
9396
float textureZoom = 1.f;
97+
float previousDistance = 1.f;
9498
uint8_t currentMip = 0;
9599
uint8_t currentFace = 0;
96100
uint16_t currentFrame = 0;

0 commit comments

Comments
 (0)