diff --git a/include/ui/mappixmapitem.h b/include/ui/mappixmapitem.h index 16cf86f3..b54de69d 100644 --- a/include/ui/mappixmapitem.h +++ b/include/ui/mappixmapitem.h @@ -25,6 +25,7 @@ public: this->settings = settings; this->paintingMode = PaintMode::Metatiles; this->lockedAxis = MapPixmapItem::Axis::None; + this->prevStraightPathState = false; setAcceptHoverEvents(true); } MapPixmapItem::PaintMode paintingMode; @@ -36,6 +37,9 @@ public: int paint_tile_initial_x; int paint_tile_initial_y; bool straightPathMode; + bool prevStraightPathState; + int straight_path_initial_x; + int straight_path_initial_y; enum Axis { None = 0, X, diff --git a/src/ui/collisionpixmapitem.cpp b/src/ui/collisionpixmapitem.cpp index cfbae7af..52b025b3 100644 --- a/src/ui/collisionpixmapitem.cpp +++ b/src/ui/collisionpixmapitem.cpp @@ -9,23 +9,27 @@ void CollisionPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { setCursor(this->settings->mapCursor); } } + void CollisionPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { emit this->hoveredMapMovementPermissionCleared(); if (this->settings->betterCursors && this->paintingMode == MapPixmapItem::PaintMode::Metatiles){ unsetCursor(); } } + void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; - this->paint_tile_initial_x = x; - this->paint_tile_initial_y = y; + this->paint_tile_initial_x = this->straight_path_initial_x = x; + this->paint_tile_initial_y = this->straight_path_initial_y = y; emit mouseEvent(event, this); } + void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { emit mouseEvent(event, this); } + void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { this->lockedAxis = CollisionPixmapItem::Axis::None; emit mouseEvent(event, this); @@ -49,16 +53,13 @@ void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) { int y = static_cast(pos.y()) / 16; // Set straight paths on/off and snap to the dominant axis when on - bool straightPathsEnabled = event->modifiers() & Qt::ControlModifier; - if (this->settings->straightPathsEnabled || straightPathsEnabled) { - this->straightPathMode = true; - } else { - this->straightPathMode = false; - } - if (this->straightPathMode) { + if (event->modifiers() & Qt::ControlModifier) { this->lockNondominantAxis(event); x = this->adjustCoord(x, MapPixmapItem::Axis::X); y = this->adjustCoord(y, MapPixmapItem::Axis::Y); + } else { + this->prevStraightPathState = false; + this->lockedAxis = MapPixmapItem::Axis::None; } Block *block = map->getBlock(x, y); diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index f6a1e278..ae0caca0 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -20,6 +20,8 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { this->straightPathMode = true; } else { this->straightPathMode = false; + this->prevStraightPathState = false; + this->lockedAxis = MapPixmapItem::Axis::None; } if (this->straightPathMode) { this->lockNondominantAxis(event); @@ -290,10 +292,21 @@ void MapPixmapItem::lockNondominantAxis(QGraphicsSceneMouseEvent *event) { if (this->lockedAxis != MapPixmapItem::Axis::None) return; QPointF pos = event->pos(); - int xDiff = (static_cast(pos.x()) / 16) - this->paint_tile_initial_x; - int yDiff = (static_cast(pos.y()) / 16) - this->paint_tile_initial_y; - + 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; + } + // Only lock an axis when the current pos != initial and not after the mouse gets released + int xDiff = x - this->straight_path_initial_x; + int yDiff = y - this->straight_path_initial_y; if ((xDiff || yDiff) && event->type() != QEvent::GraphicsSceneMouseRelease) { if (abs(xDiff) < abs(yDiff)) this->lockedAxis = MapPixmapItem::Axis::X; @@ -305,9 +318,9 @@ void MapPixmapItem::lockNondominantAxis(QGraphicsSceneMouseEvent *event) { // Adjust the cooresponding coordinate when it is locked int MapPixmapItem::adjustCoord(int coord, MapPixmapItem::Axis axis) { if (axis == MapPixmapItem::Axis::X && this->lockedAxis == MapPixmapItem::Axis::X) - coord = this->paint_tile_initial_x; + coord = this->straight_path_initial_x; else if (axis == MapPixmapItem::Axis::Y && this->lockedAxis == MapPixmapItem::Axis::Y) - coord = this->paint_tile_initial_y; + coord = this->straight_path_initial_y; return coord; } @@ -751,8 +764,8 @@ void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; - this->paint_tile_initial_x = x; - this->paint_tile_initial_y = y; + this->paint_tile_initial_x = this->straight_path_initial_x = x; + this->paint_tile_initial_y = this->straight_path_initial_y = y; emit startPaint(event, this); emit mouseEvent(event, this); }