porymap/src/ui/mappixmapitem.cpp

721 lines
29 KiB
C++
Raw Normal View History

2018-09-27 00:30:05 +01:00
#include "mappixmapitem.h"
#include "metatile.h"
#include "log.h"
2021-12-10 21:09:06 +00:00
#include "scripting.h"
2018-09-27 00:30:05 +01:00
2020-07-29 20:51:04 +01:00
#include "editcommands.h"
2018-09-27 00:30:05 +01:00
#define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
actionId_++;
2018-09-27 00:30:05 +01:00
} else {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2018-09-27 00:30:05 +01:00
// Set straight paths on/off and snap to the dominant axis when on
if (event->modifiers() & Qt::ControlModifier) {
this->lockNondominantAxis(event);
pos = this->adjustCoords(pos);
2020-08-23 14:25:03 +01:00
} else {
this->prevStraightPathState = false;
this->lockedAxis = MapPixmapItem::Axis::None;
}
2018-09-27 00:30:05 +01:00
// Paint onto the map.
bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
2018-09-27 00:30:05 +01:00
QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions();
if (settings->smartPathsEnabled) {
if (!shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) {
paintSmartPath(pos.x(), pos.y());
} else {
paintNormal(pos.x(), pos.y());
}
2018-09-27 00:30:05 +01:00
} else {
if (shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) {
paintSmartPath(pos.x(), pos.y());
} else {
paintNormal(pos.x(), pos.y());
}
2018-09-27 00:30:05 +01:00
}
}
}
}
void MapPixmapItem::shift(QGraphicsSceneMouseEvent *event) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
actionId_++;
2018-09-27 00:30:05 +01:00
} else {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2018-09-27 00:30:05 +01:00
2020-08-21 17:15:25 +01:00
// Set straight paths on/off and snap to the dominant axis when on
if (event->modifiers() & Qt::ControlModifier) {
2020-08-21 17:15:25 +01:00
this->lockNondominantAxis(event);
pos = this->adjustCoords(pos);
2020-08-23 14:25:03 +01:00
} else {
this->prevStraightPathState = false;
this->lockedAxis = MapPixmapItem::Axis::None;
2020-08-21 17:15:25 +01:00
}
2018-09-27 00:30:05 +01:00
if (event->type() == QEvent::GraphicsSceneMousePress) {
selection_origin = QPoint(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
selection.clear();
} else if (event->type() == QEvent::GraphicsSceneMouseMove) {
if (pos.x() != selection_origin.x() || pos.y() != selection_origin.y()) {
int xDelta = pos.x() - selection_origin.x();
int yDelta = pos.y() - selection_origin.y();
2020-05-01 00:30:24 +01:00
this->shift(xDelta, yDelta);
selection_origin = QPoint(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
selection.clear();
draw();
}
}
}
}
}
void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) {
Blockdata oldMetatiles = map->layout->blockdata;
2021-02-14 21:56:23 +00:00
2020-05-01 00:30:24 +01:00
for (int i = 0; i < map->getWidth(); i++)
for (int j = 0; j < map->getHeight(); j++) {
int destX = i + xDelta;
int destY = j + yDelta;
if (destX < 0)
do { destX += map->getWidth(); } while (destX < 0);
if (destY < 0)
do { destY += map->getHeight(); } while (destY < 0);
destX %= map->getWidth();
destY %= map->getHeight();
int blockIndex = j * map->getWidth() + i;
2021-02-14 21:56:23 +00:00
Block srcBlock = oldMetatiles.at(blockIndex);
2020-05-01 00:30:24 +01:00
map->setBlock(destX, destY, srcBlock);
}
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new ShiftMetatiles(map, oldMetatiles, map->layout->blockdata, actionId_));
2021-12-26 17:13:00 +00:00
Scripting::cb_MapShifted(xDelta, yDelta);
}
2020-05-01 00:30:24 +01:00
}
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
2020-05-01 00:30:24 +01:00
int initialX = fromScriptCall ? x : this->paint_tile_initial_x;
int initialY = fromScriptCall ? y : this->paint_tile_initial_y;
2018-09-27 00:30:05 +01:00
// Snap the selected position to the top-left of the block boundary.
// This allows painting via dragging the mouse to tile the painted region.
int xDiff = x - initialX;
int yDiff = y - initialY;
if (xDiff < 0 && xDiff % selection.dimensions.x() != 0) xDiff -= selection.dimensions.x();
if (yDiff < 0 && yDiff % selection.dimensions.y() != 0) yDiff -= selection.dimensions.y();
2018-09-27 00:30:05 +01:00
x = initialX + (xDiff / selection.dimensions.x()) * selection.dimensions.x();
y = initialY + (yDiff / selection.dimensions.y()) * selection.dimensions.y();
2018-09-27 00:30:05 +01:00
2020-07-29 20:51:04 +01:00
// for edit history
2021-02-14 21:34:17 +00:00
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
2020-07-29 20:51:04 +01:00
for (int i = 0; i < selection.dimensions.x() && i + x < map->getWidth(); i++)
for (int j = 0; j < selection.dimensions.y() && j + y < map->getHeight(); j++) {
2018-09-27 00:30:05 +01:00
int actualX = i + x;
int actualY = j + y;
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(actualX, actualY, &block)) {
int index = j * selection.dimensions.x() + i;
MetatileSelectionItem item = selection.metatileItems.at(index);
if (!item.enabled)
continue;
block.metatileId = item.metatileId;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
CollisionSelectionItem collisionItem = selection.collisionItems.at(index);
block.collision = collisionItem.collision;
block.elevation = collisionItem.elevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(actualX, actualY, block, !fromScriptCall);
2018-09-27 00:30:05 +01:00
}
}
2020-07-29 20:51:04 +01:00
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new PaintMetatile(map, oldMetatiles, map->layout->blockdata, actionId_));
}
2018-09-27 00:30:05 +01:00
}
// These are tile offsets from the top-left tile in the 3x3 smart path selection.
// Each entry is for one possibility from the marching squares value for a tile.
// (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
QList<int> MapPixmapItem::smartPathTable = QList<int>({
4, // 0000
4, // 0001
4, // 0010
6, // 0011
4, // 0100
4, // 0101
0, // 0110
3, // 0111
4, // 1000
8, // 1001
4, // 1010
7, // 1011
2, // 1100
5, // 1101
1, // 1110
4, // 1111
});
#define IS_SMART_PATH_TILE(block) (selectedMetatiles->contains(block.metatileId))
2018-09-27 00:30:05 +01:00
bool isSmartPathTile(QList<MetatileSelectionItem> metatileItems, uint16_t metatileId) {
for (int i = 0; i < metatileItems.length(); i++) {
if (metatileItems.at(i).metatileId == metatileId) {
return true;
}
}
return false;
}
2020-05-03 16:48:48 +01:00
void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
2018-09-27 00:30:05 +01:00
// Smart path should never be enabled without a 3x3 block selection.
if (selection.dimensions.x() != 3 || selection.dimensions.y() != 3) return;
2018-09-27 00:30:05 +01:00
// Shift to the middle tile of the smart path selection.
uint16_t openTile = selection.metatileItems.at(4).metatileId;
uint16_t openTileCollision = 0;
uint16_t openTileElevation = 0;
bool setCollisions = false;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
openTileCollision = selection.collisionItems.at(4).collision;
openTileElevation = selection.collisionItems.at(4).elevation;
setCollisions = true;
}
2018-09-27 00:30:05 +01:00
2020-07-29 20:51:04 +01:00
// for edit history
2021-02-14 21:34:17 +00:00
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
2020-07-29 20:51:04 +01:00
2018-09-27 00:30:05 +01:00
// Fill the region with the open tile.
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++) {
2022-02-08 22:40:07 +00:00
if (!map->isWithinBounds(x + i, y + j))
2018-09-27 00:30:05 +01:00
continue;
int actualX = i + x;
int actualY = j + y;
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(actualX, actualY, &block)) {
block.metatileId = openTile;
if (setCollisions) {
2021-02-13 21:16:52 +00:00
block.collision = openTileCollision;
block.elevation = openTileElevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(actualX, actualY, block, !fromScriptCall);
2018-09-27 00:30:05 +01:00
}
}
// Go back and resolve the edge tiles
for (int i = -1; i <= 2; i++)
for (int j = -1; j <= 2; j++) {
2022-02-08 22:40:07 +00:00
if (!map->isWithinBounds(x + i, y + j))
2018-09-27 00:30:05 +01:00
continue;
// Ignore the corners, which can't possible be affected by the smart path.
if ((i == -1 && j == -1) || (i == 2 && j == -1) ||
(i == -1 && j == 2) || (i == 2 && j == 2))
continue;
// Ignore tiles that aren't part of the smart path set.
int actualX = i + x;
int actualY = j + y;
2021-02-13 21:16:52 +00:00
Block block;
if (!map->getBlock(actualX, actualY, &block) || !isSmartPathTile(selection.metatileItems, block.metatileId)) {
2018-09-27 00:30:05 +01:00
continue;
}
int id = 0;
2021-02-13 21:16:52 +00:00
Block top;
Block right;
Block bottom;
Block left;
2018-09-27 00:30:05 +01:00
// Get marching squares value, to determine which tile to use.
if (map->getBlock(actualX, actualY - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId))
2018-09-27 00:30:05 +01:00
id += 1;
if (map->getBlock(actualX + 1, actualY, &right) && isSmartPathTile(selection.metatileItems, right.metatileId))
2018-09-27 00:30:05 +01:00
id += 2;
if (map->getBlock(actualX, actualY + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId))
2018-09-27 00:30:05 +01:00
id += 4;
if (map->getBlock(actualX - 1, actualY, &left) && isSmartPathTile(selection.metatileItems, left.metatileId))
2018-09-27 00:30:05 +01:00
id += 8;
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId;
if (setCollisions) {
CollisionSelectionItem collisionItem = selection.collisionItems.at(smartPathTable[id]);
block.collision = collisionItem.collision;
block.elevation = collisionItem.elevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(actualX, actualY, block, !fromScriptCall);
2018-09-27 00:30:05 +01:00
}
2020-07-29 20:51:04 +01:00
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new PaintMetatile(map, oldMetatiles, map->layout->blockdata, actionId_));
}
2018-09-27 00:30:05 +01:00
}
void MapPixmapItem::lockNondominantAxis(QGraphicsSceneMouseEvent *event) {
/* Return if an axis is already locked, or if the mouse has been released. The mouse release check is necessary
* because MapPixmapItem::mouseReleaseEvent seems to get called before this function, which would unlock the axis
* and then get immediately re-locked here until the next ctrl-click. */
if (this->lockedAxis != MapPixmapItem::Axis::None || event->type() == QEvent::GraphicsSceneMouseRelease)
return;
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
if (!this->prevStraightPathState) {
this->prevStraightPathState = true;
this->straight_path_initial_x = pos.x();
this->straight_path_initial_y = pos.y();
}
// Only lock an axis when the current position != initial
int xDiff = pos.x() - this->straight_path_initial_x;
int yDiff = pos.y() - this->straight_path_initial_y;
if (xDiff || yDiff) {
if (abs(xDiff) < abs(yDiff)) {
this->lockedAxis = MapPixmapItem::Axis::X;
} else {
this->lockedAxis = MapPixmapItem::Axis::Y;
}
}
}
// Adjust the cooresponding coordinate when it is locked
QPoint MapPixmapItem::adjustCoords(QPoint pos) {
if (this->lockedAxis == MapPixmapItem::Axis::X) {
pos.setX(this->straight_path_initial_x);
} else if (this->lockedAxis == MapPixmapItem::Axis::Y) {
pos.setY(this->straight_path_initial_y);
}
return pos;
}
2018-09-27 00:30:05 +01:00
void MapPixmapItem::updateMetatileSelection(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
// Update/apply copied metatiles.
if (event->type() == QEvent::GraphicsSceneMousePress) {
selection_origin = QPoint(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
selection.clear();
selection.append(QPoint(pos.x(), pos.y()));
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(pos.x(), pos.y(), &block)) {
this->metatileSelector->selectFromMap(block.metatileId, block.collision, block.elevation);
2021-02-13 21:16:52 +00:00
}
2018-09-27 00:30:05 +01:00
} else if (event->type() == QEvent::GraphicsSceneMouseMove) {
int x1 = selection_origin.x();
int y1 = selection_origin.y();
int x2 = pos.x();
int y2 = pos.y();
2018-09-27 00:30:05 +01:00
if (x1 > x2) SWAP(x1, x2);
if (y1 > y2) SWAP(y1, y2);
selection.clear();
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
selection.append(QPoint(x, y));
}
}
QList<uint16_t> metatiles;
QList<QPair<uint16_t, uint16_t>> collisions;
2018-09-27 00:30:05 +01:00
for (QPoint point : selection) {
int x = point.x();
int y = point.y();
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(x, y, &block)) {
metatiles.append(block.metatileId);
2021-02-13 21:16:52 +00:00
}
int blockIndex = y * map->getWidth() + x;
2021-02-14 21:34:17 +00:00
block = map->layout->blockdata.at(blockIndex);
2021-02-13 21:16:52 +00:00
auto collision = block.collision;
auto elevation = block.elevation;
collisions.append(QPair<uint16_t, uint16_t>(collision, elevation));
2018-09-27 00:30:05 +01:00
}
this->metatileSelector->setExternalSelection(x2 - x1 + 1, y2 - y1 + 1, metatiles, collisions);
2018-09-27 00:30:05 +01:00
}
}
void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
actionId_++;
2018-09-27 00:30:05 +01:00
} else {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2021-02-13 21:16:52 +00:00
Block block;
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
int metatileId = selection.metatileItems.first().metatileId;
if (selection.metatileItems.count() > 1 || (map->getBlock(pos.x(), pos.y(), &block) && block.metatileId != metatileId)) {
2018-09-27 00:30:05 +01:00
bool smartPathsEnabled = event->modifiers() & Qt::ShiftModifier;
if ((this->settings->smartPathsEnabled || smartPathsEnabled) && selection.dimensions.x() == 3 && selection.dimensions.y() == 3)
this->floodFillSmartPath(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
else
this->floodFill(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
}
}
}
}
2019-01-04 23:06:27 +00:00
void MapPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
actionId_++;
2019-01-04 23:06:27 +00:00
} else {
QPoint initialPos = Metatile::coordFromPixmapCoord(event->pos());
this->magicFill(initialPos.x(), initialPos.y());
2020-05-01 00:30:24 +01:00
}
}
}
void MapPixmapItem::magicFill(int x, int y, uint16_t metatileId, bool fromScriptCall) {
QPoint selectionDimensions(1, 1);
QList<MetatileSelectionItem> selectedMetatiles = QList<MetatileSelectionItem>({MetatileSelectionItem{ true, metatileId }});
this->magicFill(x, y, selectionDimensions, selectedMetatiles, QList<CollisionSelectionItem>(), fromScriptCall);
2020-05-01 00:30:24 +01:00
}
void MapPixmapItem::magicFill(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
this->magicFill(x, y, selection.dimensions, selection.metatileItems, selection.collisionItems, fromScriptCall);
2020-05-01 00:30:24 +01:00
}
void MapPixmapItem::magicFill(
int initialX,
int initialY,
QPoint selectionDimensions,
QList<MetatileSelectionItem> selectedMetatiles,
QList<CollisionSelectionItem> selectedCollisions,
2020-05-01 00:30:24 +01:00
bool fromScriptCall) {
2021-02-13 21:16:52 +00:00
Block block;
if (map->getBlock(initialX, initialY, &block)) {
if (selectedMetatiles.length() == 1 && selectedMetatiles.at(0).metatileId == block.metatileId) {
return;
}
2021-02-14 21:34:17 +00:00
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
2020-07-29 20:51:04 +01:00
bool setCollisions = selectedCollisions.length() == selectedMetatiles.length();
uint16_t metatileId = block.metatileId;
2020-05-01 00:30:24 +01:00
for (int y = 0; y < map->getHeight(); y++) {
for (int x = 0; x < map->getWidth(); x++) {
if (map->getBlock(x, y, &block) && block.metatileId == metatileId) {
2020-05-01 00:30:24 +01:00
int xDiff = x - initialX;
int yDiff = y - initialY;
int i = xDiff % selectionDimensions.x();
int j = yDiff % selectionDimensions.y();
if (i < 0) i = selectionDimensions.x() + i;
if (j < 0) j = selectionDimensions.y() + j;
int index = j * selectionDimensions.x() + i;
block.metatileId = selectedMetatiles.at(index).metatileId;
2020-05-01 00:30:24 +01:00
if (setCollisions) {
CollisionSelectionItem item = selectedCollisions.at(index);
block.collision = item.collision;
block.elevation = item.elevation;
2019-01-04 23:06:27 +00:00
}
2021-02-13 21:16:52 +00:00
map->setBlock(x, y, block, !fromScriptCall);
2019-01-04 23:06:27 +00:00
}
}
}
2020-07-29 20:51:04 +01:00
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new MagicFillMetatile(map, oldMetatiles, map->layout->blockdata, actionId_));
}
2019-01-04 23:06:27 +00:00
}
}
2020-05-01 00:30:24 +01:00
void MapPixmapItem::floodFill(int initialX, int initialY, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
this->floodFill(initialX, initialY, selection.dimensions, selection.metatileItems, selection.collisionItems, fromScriptCall);
2020-05-01 00:30:24 +01:00
}
void MapPixmapItem::floodFill(int initialX, int initialY, uint16_t metatileId, bool fromScriptCall) {
QPoint selectionDimensions(1, 1);
QList<MetatileSelectionItem> selectedMetatiles = QList<MetatileSelectionItem>({MetatileSelectionItem{true, metatileId}});
this->floodFill(initialX, initialY, selectionDimensions, selectedMetatiles, QList<CollisionSelectionItem>(), fromScriptCall);
2020-05-01 00:30:24 +01:00
}
void MapPixmapItem::floodFill(
int initialX,
int initialY,
QPoint selectionDimensions,
QList<MetatileSelectionItem> selectedMetatiles,
QList<CollisionSelectionItem> selectedCollisions,
2020-05-01 00:30:24 +01:00
bool fromScriptCall) {
bool setCollisions = selectedCollisions.length() == selectedMetatiles.length();
2021-02-14 21:34:17 +00:00
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
2020-07-29 20:51:04 +01:00
2021-02-13 21:16:52 +00:00
QSet<int> visited;
2018-09-27 00:30:05 +01:00
QList<QPoint> todo;
todo.append(QPoint(initialX, initialY));
while (todo.length()) {
QPoint point = todo.takeAt(0);
int x = point.x();
int y = point.y();
2021-02-13 21:16:52 +00:00
Block block;
if (!map->getBlock(x, y, &block)) {
2018-09-27 00:30:05 +01:00
continue;
}
2021-02-13 21:16:52 +00:00
visited.insert(x + y * map->getWidth());
2018-09-27 00:30:05 +01:00
int xDiff = x - initialX;
int yDiff = y - initialY;
int i = xDiff % selectionDimensions.x();
int j = yDiff % selectionDimensions.y();
if (i < 0) i = selectionDimensions.x() + i;
if (j < 0) j = selectionDimensions.y() + j;
int index = j * selectionDimensions.x() + i;
uint16_t metatileId = selectedMetatiles.at(index).metatileId;
uint16_t old_metatileId = block.metatileId;
if (selectedMetatiles.count() != 1 || old_metatileId != metatileId) {
block.metatileId = metatileId;
if (setCollisions) {
CollisionSelectionItem item = selectedCollisions.at(index);
block.collision = item.collision;
block.elevation = item.elevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(x, y, block, !fromScriptCall);
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x + 1 + y * map->getWidth()) && map->getBlock(x + 1, y, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x + 1, y));
2021-02-13 21:16:52 +00:00
visited.insert(x + 1 + y * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x - 1 + y * map->getWidth()) && map->getBlock(x - 1, y, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x - 1, y));
2021-02-13 21:16:52 +00:00
visited.insert(x - 1 + y * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x + (y + 1) * map->getWidth()) && map->getBlock(x, y + 1, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y + 1));
2021-02-13 21:16:52 +00:00
visited.insert(x + (y + 1) * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x + (y - 1) * map->getWidth()) && map->getBlock(x, y - 1, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y - 1));
2021-02-13 21:16:52 +00:00
visited.insert(x + (y - 1) * map->getWidth());
2018-09-27 00:30:05 +01:00
}
}
2019-01-05 00:07:44 +00:00
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new BucketFillMetatile(map, oldMetatiles, map->layout->blockdata, actionId_));
}
2018-09-27 00:30:05 +01:00
}
2020-05-03 16:48:48 +01:00
void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
2018-09-27 00:30:05 +01:00
// Smart path should never be enabled without a 3x3 block selection.
if (selection.dimensions.x() != 3 || selection.dimensions.y() != 3) return;
2018-09-27 00:30:05 +01:00
// Shift to the middle tile of the smart path selection.
uint16_t openTile = selection.metatileItems.at(4).metatileId;
uint16_t openTileCollision = 0;
uint16_t openTileElevation = 0;
bool setCollisions = false;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
CollisionSelectionItem item = selection.collisionItems.at(4);
openTileCollision = item.collision;
openTileElevation = item.elevation;
setCollisions = true;
}
2018-09-27 00:30:05 +01:00
2021-02-14 21:34:17 +00:00
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
2020-07-29 20:51:04 +01:00
2018-09-27 00:30:05 +01:00
// Flood fill the region with the open tile.
QList<QPoint> todo;
todo.append(QPoint(initialX, initialY));
while (todo.length()) {
QPoint point = todo.takeAt(0);
int x = point.x();
int y = point.y();
2021-02-13 21:16:52 +00:00
Block block;
if (!map->getBlock(x, y, &block)) {
2018-09-27 00:30:05 +01:00
continue;
}
uint16_t old_metatileId = block.metatileId;
if (old_metatileId == openTile) {
2018-09-27 00:30:05 +01:00
continue;
}
block.metatileId = openTile;
if (setCollisions) {
2021-02-13 21:16:52 +00:00
block.collision = openTileCollision;
block.elevation = openTileElevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(x, y, block, !fromScriptCall);
if (map->getBlock(x + 1, y, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x + 1, y));
}
if (map->getBlock(x - 1, y, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x - 1, y));
}
if (map->getBlock(x, y + 1, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y + 1));
}
if (map->getBlock(x, y - 1, &block) && block.metatileId == old_metatileId) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y - 1));
}
}
// Go back and resolve the flood-filled edge tiles.
// Mark tiles as visited while we go.
2021-02-13 21:16:52 +00:00
QSet<int> visited;
2018-09-27 00:30:05 +01:00
todo.append(QPoint(initialX, initialY));
while (todo.length()) {
QPoint point = todo.takeAt(0);
int x = point.x();
int y = point.y();
2021-02-13 21:16:52 +00:00
Block block;
if (!map->getBlock(x, y, &block)) {
2018-09-27 00:30:05 +01:00
continue;
}
2021-02-13 21:16:52 +00:00
visited.insert(x + y * map->getWidth());
2018-09-27 00:30:05 +01:00
int id = 0;
2021-02-13 21:16:52 +00:00
Block top;
Block right;
Block bottom;
Block left;
2018-09-27 00:30:05 +01:00
// Get marching squares value, to determine which tile to use.
if (map->getBlock(x, y - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId))
2018-09-27 00:30:05 +01:00
id += 1;
if (map->getBlock(x + 1, y, &right) && isSmartPathTile(selection.metatileItems, right.metatileId))
2018-09-27 00:30:05 +01:00
id += 2;
if (map->getBlock(x, y + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId))
2018-09-27 00:30:05 +01:00
id += 4;
if (map->getBlock(x - 1, y, &left) && isSmartPathTile(selection.metatileItems, left.metatileId))
2018-09-27 00:30:05 +01:00
id += 8;
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId;
if (setCollisions) {
CollisionSelectionItem item = selection.collisionItems.at(smartPathTable[id]);
block.collision = item.collision;
block.elevation = item.elevation;
}
2021-02-13 21:16:52 +00:00
map->setBlock(x, y, block, !fromScriptCall);
2018-09-27 00:30:05 +01:00
// Visit neighbors if they are smart-path tiles, and don't revisit any.
if (!visited.contains(x + 1 + y * map->getWidth()) && map->getBlock(x + 1, y, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x + 1, y));
2021-02-13 21:16:52 +00:00
visited.insert(x + 1 + y * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x - 1 + y * map->getWidth()) && map->getBlock(x - 1, y, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x - 1, y));
2021-02-13 21:16:52 +00:00
visited.insert(x - 1 + y * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x + (y + 1) * map->getWidth()) && map->getBlock(x, y + 1, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y + 1));
2021-02-13 21:16:52 +00:00
visited.insert(x + (y + 1) * map->getWidth());
2018-09-27 00:30:05 +01:00
}
if (!visited.contains(x + (y - 1) * map->getWidth()) && map->getBlock(x, y - 1, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) {
2018-09-27 00:30:05 +01:00
todo.append(QPoint(x, y - 1));
2021-02-13 21:16:52 +00:00
visited.insert(x + (y - 1) * map->getWidth());
2018-09-27 00:30:05 +01:00
}
}
2021-02-14 21:56:23 +00:00
if (!fromScriptCall && map->layout->blockdata != oldMetatiles) {
map->editHistory.push(new BucketFillMetatile(map, oldMetatiles, map->layout->blockdata, actionId_));
}
2018-09-27 00:30:05 +01:00
}
void MapPixmapItem::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->metatileSelector->selectFromMap(block.metatileId, block.collision, block.elevation);
2018-09-27 00:30:05 +01:00
}
}
void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2018-09-27 00:30:05 +01:00
if (event->type() == QEvent::GraphicsSceneMousePress) {
selection_origin = QPoint(pos.x(), pos.y());
2018-09-27 00:30:05 +01:00
selection.clear();
} else if (event->type() == QEvent::GraphicsSceneMouseMove) {
if (event->buttons() & Qt::LeftButton) {
selection.clear();
selection.append(QPoint(pos.x(), pos.y()));
2018-09-27 00:30:05 +01:00
}
} else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
if (!selection.isEmpty()) {
QPoint pos = selection.last();
int x1 = selection_origin.x();
int y1 = selection_origin.y();
int x2 = pos.x();
int y2 = pos.y();
if (x1 > x2) SWAP(x1, x2);
if (y1 > y2) SWAP(y1, y2);
selection.clear();
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
selection.append(QPoint(x, y));
}
}
logInfo(QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2));
2018-09-27 00:30:05 +01:00
}
}
}
void MapPixmapItem::draw(bool ignoreCache) {
if (map) {
2020-07-29 20:51:04 +01:00
map->setMapItem(this);
2018-09-27 00:30:05 +01:00
setPixmap(map->render(ignoreCache));
}
}
void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
2020-10-24 12:45:08 +01:00
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
if (pos != this->metatilePos) {
this->metatilePos = pos;
emit this->hoveredMapMetatileChanged(pos);
}
if (this->settings->betterCursors && this->paintingMode != MapPixmapItem::PaintMode::Disabled) {
2018-09-27 00:30:05 +01:00
setCursor(this->settings->mapCursor);
}
}
2021-12-13 16:41:46 +00:00
void MapPixmapItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event) {
this->has_mouse = true;
2021-12-13 16:41:46 +00:00
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
emit this->hoveredMapMetatileChanged(pos);
}
2018-09-27 00:30:05 +01:00
void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
emit this->hoveredMapMetatileCleared();
if (this->settings->betterCursors && this->paintingMode != MapPixmapItem::PaintMode::Disabled) {
2018-09-27 00:30:05 +01:00
unsetCursor();
}
this->has_mouse = false;
2018-09-27 00:30:05 +01:00
}
void MapPixmapItem::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();
emit startPaint(event, this);
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}
void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
2020-10-24 12:45:08 +01:00
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
if (pos != this->metatilePos) {
this->metatilePos = pos;
emit this->hoveredMapMetatileChanged(pos);
}
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}
void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
this->lockedAxis = MapPixmapItem::Axis::None;
emit endPaint(event, this);
2018-09-27 00:30:05 +01:00
emit mouseEvent(event, this);
}