From 939ec2520db688d453905b7f56ebd407bbe221d0 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Mon, 7 Sep 2020 03:13:37 -0400 Subject: [PATCH] Simplify lockNondominantAxis() and add comment --- src/ui/mappixmapitem.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index 67db49b7..593e071e 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -280,26 +280,25 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) { } void MapPixmapItem::lockNondominantAxis(QGraphicsSceneMouseEvent *event) { - // Return if an axis is already locked - if (this->lockedAxis != MapPixmapItem::Axis::None) return; + /* Return if an axis is already locked, or if the mouse has been released. The mouse release check is necessary + * because MapPixmapItem::mouseReleaseEvent seems to get called before this function, which would unlock the axis + * and then get immediately re-locked here until the next ctrl-click. */ + if (this->lockedAxis != MapPixmapItem::Axis::None || event->type() == QEvent::GraphicsSceneMouseRelease) + return; QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; - if (event->modifiers() & Qt::ControlModifier) { - if (!this->prevStraightPathState) { - this->prevStraightPathState = true; - this->straight_path_initial_x = x; - this->straight_path_initial_y = y; - } - } else { - this->prevStraightPathState = false; + if (!this->prevStraightPathState) { + this->prevStraightPathState = true; + this->straight_path_initial_x = x; + this->straight_path_initial_y = y; } - // Only lock an axis when the current pos != initial and not after the mouse gets released + // Only lock an axis when the current position != initial int xDiff = x - this->straight_path_initial_x; int yDiff = y - this->straight_path_initial_y; - if ((xDiff || yDiff) && event->type() != QEvent::GraphicsSceneMouseRelease) { + if (xDiff || yDiff) { if (abs(xDiff) < abs(yDiff)) { this->lockedAxis = MapPixmapItem::Axis::X; } else {