Fix collision straight paths and general improvement

This commit is contained in:
BigBahss 2020-08-23 08:37:14 -04:00 committed by garak
parent b8bbfe60e8
commit 27c9fec0b4
3 changed files with 34 additions and 16 deletions

View file

@ -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,

View file

@ -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<int>(pos.x()) / 16;
int y = static_cast<int>(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<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) {
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);

View file

@ -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<int>(pos.x()) / 16) - this->paint_tile_initial_x;
int yDiff = (static_cast<int>(pos.y()) / 16) - this->paint_tile_initial_y;
int x = static_cast<int>(pos.x()) / 16;
int y = static_cast<int>(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<int>(pos.x()) / 16;
int y = static_cast<int>(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);
}