Skip to content

Commit

Permalink
Fixed mouse handling issue when zooming while painting
Browse files Browse the repository at this point in the history
This also reverts part of e73245b. The
drift was the result of rounding rather than of not taking the viewport
position into account, which is not necessary.

Closes #3863
  • Loading branch information
bjorn committed Jan 12, 2024
1 parent 5a72b8b commit d1c1fb8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* tmxrasterizer: Added --hide-object and --show-object arguments (by Lars Luz, #3819)
* tmxviewer: Added support for viewing JSON maps (#3866)
* Windows: Fixed the support for WebP images (updated to Qt 6.5.3)
* Fixed mouse handling issue when zooming while painting (#3863)

### Tiled 1.10.2 (4 August 2023)

Expand Down
51 changes: 36 additions & 15 deletions src/tiled/mapscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ void MapScene::setPainterScale(qreal painterScale)
mapItem->mapDocument()->renderer()->setPainterScale(painterScale);
}

void MapScene::setSuppressMouseMoveEvents(bool suppress)
{
mSuppressMouseMoveEvents = suppress;

if (!suppress && mMouseMoveEventSuppressed) {
// Replay the last mouse move event
toolMouseMoved(mLastMousePos, mLastModifiers);
mMouseMoveEventSuppressed = false;
}
}

/**
* Returns the bounding rect of the map. This can be different from the
* sceneRect() when multiple maps are displayed.
Expand Down Expand Up @@ -178,12 +189,12 @@ void MapScene::setSelectedTool(AbstractTool *tool)
if (!mSelectedTool)
return; // Tool deactivated itself upon activation

mCurrentModifiers = QApplication::keyboardModifiers();
mSelectedTool->modifiersChanged(mCurrentModifiers);
mToolModifiers = QApplication::keyboardModifiers();
mSelectedTool->modifiersChanged(mToolModifiers);

if (mUnderMouse) {
mSelectedTool->mouseEntered();
mSelectedTool->mouseMoved(mLastMousePos, mCurrentModifiers);
mSelectedTool->mouseMoved(mLastMousePos, mToolModifiers);
}
}
}
Expand Down Expand Up @@ -469,7 +480,12 @@ void MapScene::keyPressEvent(QKeyEvent *event)
void MapScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
mLastMousePos = mouseEvent->scenePos();
mLastModifiers = mouseEvent->modifiers();

if (mSuppressMouseMoveEvents) {
mMouseMoveEventSuppressed = true;
return;
}
if (!mMapDocument)
return;

Expand All @@ -482,17 +498,8 @@ void MapScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
// if (mouseEvent->isAccepted())
// return;

if (mSelectedTool) {
const Qt::KeyboardModifiers newModifiers = mouseEvent->modifiers();

if (newModifiers != mCurrentModifiers) {
mSelectedTool->modifiersChanged(newModifiers);
mCurrentModifiers = newModifiers;
}

mSelectedTool->mouseMoved(mouseEvent->scenePos(), mCurrentModifiers);
if (toolMouseMoved(mLastMousePos, mLastModifiers))
mouseEvent->accept();
}
}

void MapScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
Expand Down Expand Up @@ -621,9 +628,9 @@ bool MapScene::eventFilter(QObject *, QEvent *event)
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
const Qt::KeyboardModifiers newModifiers = keyEvent->modifiers();

if (mSelectedTool && newModifiers != mCurrentModifiers) {
if (mSelectedTool && newModifiers != mToolModifiers) {
mSelectedTool->modifiersChanged(newModifiers);
mCurrentModifiers = newModifiers;
mToolModifiers = newModifiers;
}
}
break;
Expand All @@ -634,4 +641,18 @@ bool MapScene::eventFilter(QObject *, QEvent *event)
return false;
}

bool MapScene::toolMouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers)
{
if (!mSelectedTool)
return false;

if (mToolModifiers != modifiers) {
mToolModifiers = modifiers;
mSelectedTool->modifiersChanged(modifiers);
}

mSelectedTool->mouseMoved(pos, modifiers);
return true;
}

#include "moc_mapscene.cpp"
8 changes: 7 additions & 1 deletion src/tiled/mapscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MapScene : public QGraphicsScene
void setShowTileCollisionShapes(bool enabled);
void setParallaxEnabled(bool enabled);
void setPainterScale(qreal painterScale);
void setSuppressMouseMoveEvents(bool suppress);

QRectF mapBoundingRect() const;

Expand Down Expand Up @@ -127,6 +128,8 @@ class MapScene : public QGraphicsScene

bool eventFilter(QObject *object, QEvent *event) override;

bool toolMouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers);

MapDocument *mMapDocument = nullptr;
QHash<Map*, MapItem*> mMapItems;
AbstractTool *mSelectedTool = nullptr;
Expand All @@ -135,8 +138,11 @@ class MapScene : public QGraphicsScene
bool mShowTileCollisionShapes = false;
bool mParallaxEnabled = true;
bool mWorldsEnabled = true;
bool mSuppressMouseMoveEvents = false;
bool mMouseMoveEventSuppressed = false;
Session::CallbackIterator mEnableWorldsCallback;
Qt::KeyboardModifiers mCurrentModifiers = Qt::NoModifier;
Qt::KeyboardModifiers mToolModifiers = Qt::NoModifier;
Qt::KeyboardModifiers mLastModifiers = Qt::NoModifier;
QPointF mLastMousePos;
QRectF mViewRect;
QColor mDefaultBackgroundColor;
Expand Down
17 changes: 11 additions & 6 deletions src/tiled/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void MapView::updateSceneRect(const QRectF &sceneRect, const QTransform &transfo

void MapView::updateViewRect()
{
const QRectF viewRect = mapToScene(viewport()->geometry()).boundingRect();
const QRectF viewRect = mapToScene(viewport()->rect()).boundingRect();
if (mViewRect == viewRect)
return;

Expand Down Expand Up @@ -397,21 +397,21 @@ void MapView::forceCenterOn(QPointF pos)

const qreal width = viewport()->width();
const qreal height = viewport()->height();
const QPointF viewPoint = transform().map(pos) - viewport()->pos();
const QPointF viewPoint = transform().map(pos);

if (hScroll) {
if (isRightToLeft()) {
qint64 horizontal = 0;
horizontal += hBar->minimum();
horizontal += hBar->maximum();
horizontal -= int(viewPoint.x() - width / 2.0);
horizontal -= qRound(viewPoint.x() - width / 2.0);
hBar->forceSetValue(static_cast<int>(horizontal));
} else {
hBar->forceSetValue(int(viewPoint.x() - width / 2.0));
hBar->forceSetValue(qRound(viewPoint.x() - width / 2.0));
}
}
if (vScroll)
vBar->forceSetValue(int(viewPoint.y() - height / 2.0));
vBar->forceSetValue(qRound(viewPoint.y() - height / 2.0));
}

/**
Expand Down Expand Up @@ -583,8 +583,13 @@ void MapView::wheelEvent(QWheelEvent *event)
// No automatic anchoring since we'll do it manually
setTransformationAnchor(QGraphicsView::NoAnchor);

// Mouse move events need to be suppressed while zooming, otherwise
// the tools will get several mouse move events in various stages of
// the zooming process.
mapScene()->setSuppressMouseMoveEvents(true);
mZoomable->handleWheelDelta(event->angleDelta().y());
adjustCenterFromMousePosition(mLastMousePos);
mapScene()->setSuppressMouseMoveEvents(false);

// Restore the centering anchor
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
Expand Down Expand Up @@ -640,7 +645,7 @@ void MapView::adjustCenterFromMousePosition(QPoint mousePos)
{
// Place the last known mouse scene pos below the mouse again
const QWidget *view = viewport();
const QPointF viewCenterScenePos = mapToScene(view->geometry().center());
const QPointF viewCenterScenePos = viewportTransform().inverted().map(QRectF(view->rect()).center());
const QPointF mouseScenePos = mapToScene(view->mapFromGlobal(mousePos));
const QPointF diff = viewCenterScenePos - mouseScenePos;
QGraphicsView::centerOn(mLastMouseScenePos + diff);
Expand Down

0 comments on commit d1c1fb8

Please sign in to comment.