Implement straight paths for map tiles and smart paths

This commit is contained in:
BigBahss 2020-08-21 12:02:48 -04:00 committed by garak
parent 6a9088c4ea
commit 5da761ea94
8 changed files with 89 additions and 7 deletions

View file

@ -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*);

View file

@ -8,6 +8,7 @@ class Settings
public:
Settings();
bool smartPathsEnabled;
bool straightPathsEnabled;
bool betterCursors;
QCursor mapCursor;
bool playerViewRectEnabled;

View file

@ -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();
};

View file

@ -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<QPoint> 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);

View file

@ -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);
}

View file

@ -3,6 +3,7 @@
Settings::Settings()
{
this->smartPathsEnabled = false;
this->straightPathsEnabled = false;
this->betterCursors = true;
this->playerViewRectEnabled = false;
this->cursorTileRectEnabled = true;

View file

@ -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) {

View file

@ -14,6 +14,19 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
int x = static_cast<int>(pos.x()) / 16;
int y = static_cast<int>(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<int>(pos.x()) / 16) - this->paint_tile_initial_x;
int yDiff = (static_cast<int>(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<int>(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);
}