porymap/src/ui/collisionpixmapitem.cpp

141 lines
5.4 KiB
C++
Raw Normal View History

2018-09-27 00:30:05 +01:00
#include "collisionpixmapitem.h"
#include "editcommands.h"
#include "metatile.h"
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
if (pos != this->previousPos) {
this->previousPos = pos;
emit this->hoveredMapMovementPermissionChanged(pos.x(), pos.y());
}
if (this->settings->betterCursors && this->paintingMode == MapPixmapItem::PaintMode::Metatiles) {
2018-09-27 00:30:05 +01:00
setCursor(this->settings->mapCursor);
}
}
void CollisionPixmapItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
emit this->hoveredMapMovementPermissionChanged(pos.x(), pos.y());
}
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
emit this->hoveredMapMovementPermissionCleared();
if (this->settings->betterCursors && this->paintingMode == MapPixmapItem::PaintMode::Metatiles){
2018-09-27 00:30:05 +01:00
unsetCursor();
}
}
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
this->paint_tile_initial_x = this->straight_path_initial_x = pos.x();
this->paint_tile_initial_y = this->straight_path_initial_y = pos.y();
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
if (pos != this->previousPos) {
this->previousPos = pos;
emit this->hoveredMapMovementPermissionChanged(pos.x(), pos.y());
}
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
2020-08-21 17:11:06 +01:00
this->lockedAxis = CollisionPixmapItem::Axis::None;
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}
void CollisionPixmapItem::draw(bool ignoreCache) {
if (map) {
map->setCollisionItem(this);
setPixmap(map->renderCollision(*this->opacity, ignoreCache));
2018-09-27 00:30:05 +01:00
}
}
void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
actionId_++;
} else if (map) {
2021-02-14 21:34:17 +00:00
Blockdata oldCollision = map->layout->blockdata;
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2020-08-21 17:11:06 +01:00
// Set straight paths on/off and snap to the dominant axis when on
if (event->modifiers() & Qt::ControlModifier) {
2020-08-21 17:11:06 +01:00
this->lockNondominantAxis(event);
pos = this->adjustCoords(pos);
} else {
this->prevStraightPathState = false;
this->lockedAxis = MapPixmapItem::Axis::None;
2020-08-21 17:11:06 +01:00
}
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
block.collision = this->movementPermissionsSelector->getSelectedCollision();
block.elevation = this->movementPermissionsSelector->getSelectedElevation();
map->setBlock(pos.x(), pos.y(), block, true);
2018-09-27 00:30:05 +01:00
}
2021-02-14 21:56:23 +00:00
if (map->layout->blockdata != oldCollision) {
map->editHistory.push(new PaintCollision(map, oldCollision, map->layout->blockdata, actionId_));
}
2018-09-27 00:30:05 +01:00
}
}
void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
this->actionId_++;
} else if (map) {
2021-02-14 21:34:17 +00:00
Blockdata oldCollision = map->layout->blockdata;
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2018-09-27 00:30:05 +01:00
uint16_t collision = this->movementPermissionsSelector->getSelectedCollision();
uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation();
map->floodFillCollisionElevation(pos.x(), pos.y(), collision, elevation);
2021-02-14 21:56:23 +00:00
if (map->layout->blockdata != oldCollision) {
map->editHistory.push(new BucketFillCollision(map, oldCollision, map->layout->blockdata));
}
2018-09-27 00:30:05 +01:00
}
}
void CollisionPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
this->actionId_++;
} else if (map) {
2021-02-14 21:34:17 +00:00
Blockdata oldCollision = map->layout->blockdata;
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
uint16_t collision = this->movementPermissionsSelector->getSelectedCollision();
uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation();
map->magicFillCollisionElevation(pos.x(), pos.y(), collision, elevation);
2021-02-14 21:56:23 +00:00
if (map->layout->blockdata != oldCollision) {
map->editHistory.push(new MagicFillCollision(map, oldCollision, map->layout->blockdata));
}
}
}
2018-09-27 00:30:05 +01:00
void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
this->movementPermissionsSelector->select(block.collision, block.elevation);
2018-09-27 00:30:05 +01:00
}
}
void CollisionPixmapItem::updateMovementPermissionSelection(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2018-09-27 00:30:05 +01:00
// Snap point to within map bounds.
if (pos.x() < 0) pos.setX(0);
if (pos.x() >= map->getWidth()) pos.setX(map->getWidth() - 1);
if (pos.y() < 0) pos.setY(0);
if (pos.y() >= map->getHeight()) pos.setY(map->getHeight() - 1);
2018-09-27 00:30:05 +01:00
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
this->movementPermissionsSelector->select(block.collision, block.elevation);
}
2018-09-27 00:30:05 +01:00
}