Skip to content

Commit

Permalink
Give touch events an area.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobly0 committed Nov 6, 2018
1 parent 07b0566 commit e4545c9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
12 changes: 8 additions & 4 deletions gui/qt/keypad/arrowkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ void ArrowKey::paint(QPainter &painter) const {
}

bool ArrowKey::isUnder(const QPainterPath &area) const {
QVector2D offset{area.controlPointRect().center() - mOuter.center()};
qreal outerRadius = mOuter.width() * .7;
return offset.lengthSquared() <= outerRadius * outerRadius &&
(static_cast<int>((2 * M_2_PI * std::atan2(offset.y(), offset.x()) + 9.5) + mOffset) & 7) < 3;
QPainterPath key;
key.addEllipse(mOuter);
if (!key.intersects(area) || !area.elementCount()) {
return false;
}
QPointF center{area.isEmpty() ? area.elementAt(0) : area.controlPointRect().center()};
QVector2D offset{center - mOuter.center()};
return (static_cast<int>((2*M_2_PI * std::atan2(offset.y(), offset.x()) + 9.5) + mOffset) & 7) < 3;
}
6 changes: 2 additions & 4 deletions gui/qt/keypad/arrowkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ class ArrowKey : public Key {
ArrowKey(KeyConfig &config, const QRect &outer, const QRect &inner,
int offset, const QString &labelText, qreal gap = 2);

void paint(QPainter &) const override;

protected:
bool isUnder(const QPainterPath &) const override;
void paint(QPainter &painter) const override;
bool isUnder(const QPainterPath &area) const override;

private:
static qreal computeAngle(const QRect &, int);
Expand Down
7 changes: 1 addition & 6 deletions gui/qt/keypad/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ class Key {
painter.drawPath(mKeyShape);
}
}
bool isUnder(const QPointF &center, qreal radius = {}) const {
QPainterPath circle{center};
circle.addEllipse(center, radius, radius);
return isUnder(circle);
}
virtual bool isUnder(const QPainterPath &area) const = 0;
void pressOrRelease(bool isPress) {
if (isPress) {
press();
Expand All @@ -65,7 +61,6 @@ class Key {
}

protected:
virtual bool isUnder(const QPainterPath &area) const = 0;
QString mLabelText;
QPainterPath mKeyShape;

Expand Down
24 changes: 19 additions & 5 deletions gui/qt/keypad/keypadwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,13 @@ void KeypadWidget::updateKey(Key *key, bool wasSelected) {
}
}

void KeypadWidget::mouseUpdate(QPointF pos) {
pos = pos * mInverseTransform;
void KeypadWidget::mouseUpdate(const QPointF &pos) {
const QPainterPath area{pos * mInverseTransform};
for (uint8_t row = 0; row != sRows; ++row) {
for (uint8_t col = 0; col != sCols; ++col) {
if (Key *key = mKeys[row][col]) {
bool wasSelected = key->isSelected();
if (key->isUnder(pos)) {
if (key->isUnder(area)) {
if (!mClicked.contains(key->keycode())) {
mClicked.insert(key->keycode());
key->press();
Expand Down Expand Up @@ -396,8 +396,22 @@ void KeypadWidget::touchUpdate(const QList<QTouchEvent::TouchPoint> &points) {
if (point.state() == Qt::TouchPointStationary) {
continue;
}
if (point.state() != Qt::TouchPointReleased &&
key->isUnder(point.pos() * mInverseTransform)) {
QRectF ellipse;
ellipse.setSize(point.ellipseDiameters());
ellipse.moveCenter(point.pos());
ellipse = mInverseTransform.mapRect(ellipse);
if (ellipse.isEmpty()) {
ellipse += {4, 4, 4, 4};
}
QPainterPath area;
area.addEllipse(ellipse);
if (point.rotation()) {
area = area * QTransform::fromTranslate(ellipse.center().x(),
ellipse.center().y()).
rotate(point.rotation()).
translate(-ellipse.center().x(), -ellipse.center().y());
}
if (point.state() != Qt::TouchPointReleased && key->isUnder(area)) {
if (!mTouched.contains(point.id(), key->keycode())) {
mTouched.insert(point.id(), key->keycode());
key->press();
Expand Down
2 changes: 1 addition & 1 deletion gui/qt/keypad/keypadwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public slots:
void resizeEvent(QResizeEvent *event) override;
void paintEvent(QPaintEvent *event) override;
bool event(QEvent *event) override;
void mouseUpdate(QPointF pos);
void mouseUpdate(const QPointF &pos);
void mouseEnd(bool toggleHeld);
void mouseEvent(QMouseEvent *event);
void touchUpdate(const QList<QTouchEvent::TouchPoint> &points);
Expand Down
5 changes: 2 additions & 3 deletions gui/qt/keypad/rectkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ class RectKey : public Key {
Qt::Alignment secondAlign = Qt::AlignVCenter | Qt::AlignLeft,
Qt::Alignment alphaAlign = Qt::AlignVCenter | Qt::AlignRight);

void paint(QPainter &) const override;

protected:
void paint(QPainter &painter) const override;
bool isUnder(const QPainterPath &area) const override;

protected:
QColor mTextColor, mSecondColor, mAlphaColor;
Qt::Alignment mLabelAlign, mSecondAlign, mAlphaAlign;
QFont mLabelFont, mSecondFont, mAlphaFont;
Expand Down

0 comments on commit e4545c9

Please sign in to comment.