diff --git a/include/editor.h b/include/editor.h index 25a96a5c..c9f7bcea 100644 --- a/include/editor.h +++ b/include/editor.h @@ -181,6 +181,7 @@ private slots: void onMapStartPaint(QGraphicsSceneMouseEvent *event, MapPixmapItem *item); void onMapEndPaint(QGraphicsSceneMouseEvent *event, MapPixmapItem *item); void setSmartPathCursorMode(QGraphicsSceneMouseEvent *event); + void setStraightPathCursorMode(QGraphicsSceneMouseEvent *event); void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item); void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item); void onConnectionMoved(MapConnection*); diff --git a/include/settings.h b/include/settings.h index 5b989ed5..76074fe3 100644 --- a/include/settings.h +++ b/include/settings.h @@ -8,6 +8,7 @@ class Settings public: Settings(); bool smartPathsEnabled; + bool straightPathsEnabled; bool betterCursors; QCursor mapCursor; bool playerViewRectEnabled; diff --git a/include/ui/cursortilerect.h b/include/ui/cursortilerect.h index dfe8ed95..d25f17c3 100644 --- a/include/ui/cursortilerect.h +++ b/include/ui/cursortilerect.h @@ -52,6 +52,8 @@ public: void stopRightClickSelectionAnchor(); void setSmartPathMode(); bool getSmartPathMode() { return this->smartPathMode; } + void setStraightPathMode(); + bool getStraightPathMode() { return this->straightPathMode; } void setSingleTileMode(); void stopSingleTileMode(); void setNormalPathMode(); @@ -66,6 +68,7 @@ private: bool anchored; bool rightClickSelectionAnchored; bool smartPathMode; + bool straightPathMode; bool singleTileMode; int anchorCoordX; int anchorCoordY; @@ -73,6 +76,7 @@ private: int selectionHeight; QRgb color; bool smartPathInEffect(); + bool straightPathInEffect(); }; diff --git a/include/ui/mappixmapitem.h b/include/ui/mappixmapitem.h index 10c6b79e..16cf86f3 100644 --- a/include/ui/mappixmapitem.h +++ b/include/ui/mappixmapitem.h @@ -24,6 +24,7 @@ public: this->metatileSelector = metatileSelector; this->settings = settings; this->paintingMode = PaintMode::Metatiles; + this->lockedAxis = MapPixmapItem::Axis::None; setAcceptHoverEvents(true); } MapPixmapItem::PaintMode paintingMode; @@ -34,6 +35,13 @@ public: bool right_click; int paint_tile_initial_x; int paint_tile_initial_y; + bool straightPathMode; + enum Axis { + None = 0, + X, + Y + }; + MapPixmapItem::Axis lockedAxis; QPoint selection_origin; QList selection; virtual void paint(QGraphicsSceneMouseEvent*); @@ -64,6 +72,8 @@ public: virtual void draw(bool ignoreCache = false); void updateMetatileSelection(QGraphicsSceneMouseEvent *event); void paintNormal(int x, int y, bool fromScriptCall = false); + void lockNondominantAxis(QGraphicsSceneMouseEvent *event); + int adjustCoord(int coord, MapPixmapItem::Axis axis); private: void paintSmartPath(int x, int y, bool fromScriptCall = false); diff --git a/src/editor.cpp b/src/editor.cpp index f58040df..449abad6 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -938,7 +938,7 @@ void Editor::onHoveredMapMovementPermissionCleared() { } } -QString Editor::getMovementPermissionText(uint16_t collision, uint16_t elevation){ +QString Editor::getMovementPermissionText(uint16_t collision, uint16_t elevation) { QString message; if (collision == 0 && elevation == 0) { message = "Collision: Transition between elevations"; @@ -1009,24 +1009,31 @@ void Editor::onMapEndPaint(QGraphicsSceneMouseEvent *, MapPixmapItem *item) { this->cursorMapTileRect->stopAnchor(); } -void Editor::setSmartPathCursorMode(QGraphicsSceneMouseEvent *event) -{ +void Editor::setSmartPathCursorMode(QGraphicsSceneMouseEvent *event) { bool shiftPressed = event->modifiers() & Qt::ShiftModifier; if (settings->smartPathsEnabled) { if (!shiftPressed) { - this->cursorMapTileRect->setSmartPathMode(); + this->cursorMapTileRect->setSmartPathMode(true); } else { - this->cursorMapTileRect->setNormalPathMode(); + this->cursorMapTileRect->setSmartPathMode(false); } } else { if (shiftPressed) { - this->cursorMapTileRect->setSmartPathMode(); + this->cursorMapTileRect->setSmartPathMode(true); } else { - this->cursorMapTileRect->setNormalPathMode(); + this->cursorMapTileRect->setSmartPathMode(false); } } } +void Editor::setStraightPathCursorMode(QGraphicsSceneMouseEvent *event) { + if (event->modifiers() & Qt::ControlModifier) { + this->cursorMapTileRect->setStraightPathMode(true); + } else { + this->cursorMapTileRect->setStraightPathMode(false); + } +} + void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) { // TODO: add event tab object painting tool buttons stuff here if (item->paintingMode == MapPixmapItem::PaintMode::Disabled) { @@ -1048,6 +1055,12 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item item->floodFill(event); } } else { + this->setStraightPathCursorMode(event); + if (this->cursorMapTileRect->getStraightPathMode()) { + item->lockNondominantAxis(event); + x = item->adjustCoord(x, MapPixmapItem::Axis::X); + y = item->adjustCoord(y, MapPixmapItem::Axis::Y); + } this->setSmartPathCursorMode(event); item->paint(event); } diff --git a/src/settings.cpp b/src/settings.cpp index 7da1fcbc..dec44f06 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -3,6 +3,7 @@ Settings::Settings() { this->smartPathsEnabled = false; + this->straightPathsEnabled = false; this->betterCursors = true; this->playerViewRectEnabled = false; this->cursorTileRectEnabled = true; diff --git a/src/ui/cursortilerect.cpp b/src/ui/cursortilerect.cpp index e0c6c9d2..0f51f41c 100644 --- a/src/ui/cursortilerect.cpp +++ b/src/ui/cursortilerect.cpp @@ -9,6 +9,7 @@ CursorTileRect::CursorTileRect(bool *enabled, QRgb color) this->width = 16; this->height = 16; this->smartPathMode = false; + this->straightPathMode = false; this->singleTileMode = false; this->anchored = false; this->rightClickSelectionAnchored = false; @@ -62,6 +63,11 @@ void CursorTileRect::setSmartPathMode() this->smartPathMode = true; } +void CursorTileRect::setStraightPathMode() +{ + this->straightPathMode = true; +} + void CursorTileRect::setSingleTileMode() { this->singleTileMode = true; @@ -75,6 +81,7 @@ void CursorTileRect::stopSingleTileMode() void CursorTileRect::setNormalPathMode() { this->smartPathMode = false; + this->straightPathMode = false; } bool CursorTileRect::smartPathInEffect() @@ -82,6 +89,11 @@ bool CursorTileRect::smartPathInEffect() return !this->rightClickSelectionAnchored && this->smartPathMode && this->selectionHeight == 3 && this->selectionWidth == 3; } +bool CursorTileRect::straightPathInEffect() +{ + return !this->rightClickSelectionAnchored && this->straightPathMode; +} + void CursorTileRect::updateLocation(int coordX, int coordY) { if (!this->singleTileMode) { diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index 4f077adc..b67d4fca 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -14,6 +14,19 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { int x = static_cast(pos.x()) / 16; 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) { + this->lockNondominantAxis(event); + x = this->adjustCoord(x, MapPixmapItem::Axis::X); + y = this->adjustCoord(y, MapPixmapItem::Axis::Y); + } + // Paint onto the map. bool shiftPressed = event->modifiers() & Qt::ShiftModifier; QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions(); @@ -259,6 +272,32 @@ 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; + + 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; + + // Only lock an axis when the current pos != initial and not after the mouse gets released + if ((xDiff || yDiff) && event->type() != QEvent::GraphicsSceneMouseRelease) { + if (abs(xDiff) < abs(yDiff)) + this->lockedAxis = MapPixmapItem::Axis::X; + else + this->lockedAxis = MapPixmapItem::Axis::Y; + } +} + +// 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; + else if (axis == MapPixmapItem::Axis::Y && this->lockedAxis == MapPixmapItem::Axis::Y) + coord = this->paint_tile_initial_y; + return coord; +} + void MapPixmapItem::updateMetatileSelection(QGraphicsSceneMouseEvent *event) { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; @@ -711,6 +750,7 @@ void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { emit mouseEvent(event, this); } void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + this->lockedAxis = MapPixmapItem::Axis::None; emit endPaint(event, this); emit mouseEvent(event, this); }