2018-09-27 00:30:05 +01:00
|
|
|
#include "mappixmapitem.h"
|
2020-10-02 20:32:22 +01:00
|
|
|
#include "metatile.h"
|
2018-12-20 23:30:35 +00:00
|
|
|
#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) {
|
2020-08-01 01:30:35 +01:00
|
|
|
actionId_++;
|
2018-09-27 00:30:05 +01:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2018-09-27 00:30:05 +01:00
|
|
|
|
2020-08-21 17:02:48 +01:00
|
|
|
// Set straight paths on/off and snap to the dominant axis when on
|
2020-08-25 21:32:40 +01:00
|
|
|
if (event->modifiers() & Qt::ControlModifier) {
|
2020-08-21 17:02:48 +01:00
|
|
|
this->lockNondominantAxis(event);
|
2020-10-02 20:32:22 +01:00
|
|
|
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:02:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:30:05 +01:00
|
|
|
// Paint onto the map.
|
2020-08-29 23:48:51 +01:00
|
|
|
bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
|
2018-09-27 00:30:05 +01:00
|
|
|
QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions();
|
2020-08-29 23:48:51 +01:00
|
|
|
if (settings->smartPathsEnabled) {
|
|
|
|
if (!shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) {
|
2020-10-02 20:32:22 +01:00
|
|
|
paintSmartPath(pos.x(), pos.y());
|
2020-08-29 23:48:51 +01:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
paintNormal(pos.x(), pos.y());
|
2020-08-29 23:48:51 +01:00
|
|
|
}
|
2018-09-27 00:30:05 +01:00
|
|
|
} else {
|
2020-08-29 23:48:51 +01:00
|
|
|
if (shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) {
|
2020-10-02 20:32:22 +01:00
|
|
|
paintSmartPath(pos.x(), pos.y());
|
2020-08-29 23:48:51 +01:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
paintNormal(pos.x(), pos.y());
|
2020-08-29 23:48:51 +01:00
|
|
|
}
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapPixmapItem::shift(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if (map) {
|
|
|
|
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
|
2020-08-01 01:30:35 +01:00
|
|
|
actionId_++;
|
2018-09-27 00:30:05 +01:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
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
|
2020-08-25 21:32:40 +01:00
|
|
|
if (event->modifiers() & Qt::ControlModifier) {
|
2020-08-21 17:15:25 +01:00
|
|
|
this->lockNondominantAxis(event);
|
2020-10-02 20:32:22 +01:00
|
|
|
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) {
|
2020-10-02 20:32:22 +01:00
|
|
|
selection_origin = QPoint(pos.x(), pos.y());
|
2018-09-27 00:30:05 +01:00
|
|
|
selection.clear();
|
|
|
|
} else if (event->type() == QEvent::GraphicsSceneMouseMove) {
|
2020-10-02 20:32:22 +01:00
|
|
|
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);
|
2020-10-02 20:32:22 +01:00
|
|
|
selection_origin = QPoint(pos.x(), pos.y());
|
2018-09-27 00:30:05 +01:00
|
|
|
selection.clear();
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 01:49:00 +01:00
|
|
|
void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) {
|
2021-02-16 02:21:07 +00:00
|
|
|
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-08-04 01:49:00 +01:00
|
|
|
}
|
2020-05-01 00:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
|
2022-09-05 17:35:17 +01:00
|
|
|
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.
|
2020-04-30 02:41:19 +01:00
|
|
|
int xDiff = x - initialX;
|
|
|
|
int yDiff = y - initialY;
|
2022-09-05 17:35:17 +01:00
|
|
|
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
|
|
|
|
2022-09-05 17:35:17 +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
|
|
|
|
2022-09-05 17:35:17 +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)) {
|
2022-09-05 17:35:17 +01:00
|
|
|
int index = j * selection.dimensions.x() + i;
|
|
|
|
MetatileSelectionItem item = selection.metatileItems.at(index);
|
2022-09-07 02:42:19 +01:00
|
|
|
if (!item.enabled)
|
|
|
|
continue;
|
2022-09-05 17:35:17 +01:00
|
|
|
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;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
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_));
|
2020-08-04 01:49:00 +01:00
|
|
|
}
|
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
|
|
|
|
});
|
|
|
|
|
2021-11-24 04:53:41 +00:00
|
|
|
#define IS_SMART_PATH_TILE(block) (selectedMetatiles->contains(block.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
|
2022-09-05 17:35:17 +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) {
|
2022-09-05 17:35:17 +01:00
|
|
|
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
|
2018-09-27 00:30:05 +01:00
|
|
|
|
|
|
|
// Smart path should never be enabled without a 3x3 block selection.
|
2022-09-05 17:35:17 +01:00
|
|
|
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.
|
2022-09-05 17:35:17 +01:00
|
|
|
uint16_t openTile = selection.metatileItems.at(4).metatileId;
|
2020-02-02 22:25:37 +00:00
|
|
|
uint16_t openTileCollision = 0;
|
|
|
|
uint16_t openTileElevation = 0;
|
|
|
|
bool setCollisions = false;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
|
|
|
|
openTileCollision = selection.collisionItems.at(4).collision;
|
|
|
|
openTileElevation = selection.collisionItems.at(4).elevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
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)) {
|
2021-11-24 04:53:41 +00:00
|
|
|
block.metatileId = openTile;
|
2020-02-02 22:25:37 +00:00
|
|
|
if (setCollisions) {
|
2021-02-13 21:16:52 +00:00
|
|
|
block.collision = openTileCollision;
|
|
|
|
block.elevation = openTileElevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
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;
|
2022-09-05 17:35:17 +01:00
|
|
|
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.
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(actualX, actualY - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 1;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(actualX + 1, actualY, &right) && isSmartPathTile(selection.metatileItems, right.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 2;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(actualX, actualY + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 4;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(actualX - 1, actualY, &left) && isSmartPathTile(selection.metatileItems, left.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 8;
|
|
|
|
|
2022-09-05 17:35:17 +01:00
|
|
|
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId;
|
2020-02-02 22:25:37 +00:00
|
|
|
if (setCollisions) {
|
2022-09-05 17:35:17 +01:00
|
|
|
CollisionSelectionItem collisionItem = selection.collisionItems.at(smartPathTable[id]);
|
|
|
|
block.collision = collisionItem.collision;
|
|
|
|
block.elevation = collisionItem.elevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
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_));
|
2020-08-04 05:32:50 +01:00
|
|
|
}
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 17:02:48 +01:00
|
|
|
void MapPixmapItem::lockNondominantAxis(QGraphicsSceneMouseEvent *event) {
|
2020-09-07 08:13:37 +01:00
|
|
|
/* 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;
|
2020-08-21 17:02:48 +01:00
|
|
|
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2020-09-07 08:13:37 +01:00
|
|
|
if (!this->prevStraightPathState) {
|
|
|
|
this->prevStraightPathState = true;
|
2020-10-02 20:32:22 +01:00
|
|
|
this->straight_path_initial_x = pos.x();
|
|
|
|
this->straight_path_initial_y = pos.y();
|
2020-08-23 13:37:14 +01:00
|
|
|
}
|
2021-02-14 20:10:03 +00:00
|
|
|
|
2020-09-07 08:13:37 +01:00
|
|
|
// Only lock an axis when the current position != initial
|
2020-10-02 20:32:22 +01:00
|
|
|
int xDiff = pos.x() - this->straight_path_initial_x;
|
|
|
|
int yDiff = pos.y() - this->straight_path_initial_y;
|
2020-09-07 08:13:37 +01:00
|
|
|
if (xDiff || yDiff) {
|
2020-08-25 21:32:40 +01:00
|
|
|
if (abs(xDiff) < abs(yDiff)) {
|
2020-08-21 17:02:48 +01:00
|
|
|
this->lockedAxis = MapPixmapItem::Axis::X;
|
2020-08-25 21:32:40 +01:00
|
|
|
} else {
|
2020-08-21 17:02:48 +01:00
|
|
|
this->lockedAxis = MapPixmapItem::Axis::Y;
|
2020-08-25 21:32:40 +01:00
|
|
|
}
|
2020-08-21 17:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the cooresponding coordinate when it is locked
|
2020-10-02 20:32:22 +01:00
|
|
|
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);
|
2020-08-25 21:32:40 +01:00
|
|
|
}
|
2020-10-02 20:32:22 +01:00
|
|
|
return pos;
|
2020-08-21 17:02:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:30:05 +01:00
|
|
|
void MapPixmapItem::updateMetatileSelection(QGraphicsSceneMouseEvent *event) {
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2018-09-27 00:30:05 +01:00
|
|
|
|
|
|
|
// Snap point to within map bounds.
|
2020-10-02 20:32:22 +01:00
|
|
|
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) {
|
2020-10-02 20:32:22 +01:00
|
|
|
selection_origin = QPoint(pos.x(), pos.y());
|
2018-09-27 00:30:05 +01:00
|
|
|
selection.clear();
|
2020-10-02 20:32:22 +01:00
|
|
|
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)) {
|
2021-11-24 04:53:41 +00:00
|
|
|
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();
|
2020-10-02 20:32:22 +01:00
|
|
|
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;
|
2020-02-02 22:25:37 +00:00
|
|
|
QList<QPair<uint16_t, uint16_t>> collisions;
|
2018-09-27 00:30:05 +01:00
|
|
|
for (QPoint point : selection) {
|
2020-02-02 22:25:37 +00:00
|
|
|
int x = point.x();
|
|
|
|
int y = point.y();
|
2021-02-13 21:16:52 +00:00
|
|
|
Block block;
|
|
|
|
if (map->getBlock(x, y, &block)) {
|
2021-11-24 04:53:41 +00:00
|
|
|
metatiles.append(block.metatileId);
|
2021-02-13 21:16:52 +00:00
|
|
|
}
|
2020-02-02 22:25:37 +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
|
|
|
}
|
|
|
|
|
2020-02-02 22:25:37 +00: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) {
|
2020-08-01 01:30:35 +01:00
|
|
|
actionId_++;
|
2018-09-27 00:30:05 +01:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2021-02-13 21:16:52 +00:00
|
|
|
Block block;
|
2022-09-05 17:35:17 +01:00
|
|
|
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;
|
2022-09-05 17:35:17 +01:00
|
|
|
if ((this->settings->smartPathsEnabled || smartPathsEnabled) && selection.dimensions.x() == 3 && selection.dimensions.y() == 3)
|
2020-10-02 20:32:22 +01:00
|
|
|
this->floodFillSmartPath(pos.x(), pos.y());
|
2018-09-27 00:30:05 +01:00
|
|
|
else
|
2020-10-02 20:32:22 +01:00
|
|
|
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) {
|
2020-08-01 01:30:35 +01:00
|
|
|
actionId_++;
|
2019-01-04 23:06:27 +00:00
|
|
|
} else {
|
2020-10-02 20:32:22 +01:00
|
|
|
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);
|
2022-09-05 17:35:17 +01:00
|
|
|
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) {
|
2022-09-05 17:35:17 +01:00
|
|
|
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,
|
2022-09-05 17:35:17 +01:00
|
|
|
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)) {
|
2022-09-05 17:35:17 +01:00
|
|
|
if (selectedMetatiles.length() == 1 && selectedMetatiles.at(0).metatileId == block.metatileId) {
|
2020-06-27 23:14:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-14 21:34:17 +00:00
|
|
|
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
|
2020-07-29 20:51:04 +01:00
|
|
|
|
2022-09-05 17:35:17 +01:00
|
|
|
bool setCollisions = selectedCollisions.length() == selectedMetatiles.length();
|
2021-11-24 04:53:41 +00:00
|
|
|
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++) {
|
2021-11-24 04:53:41 +00:00
|
|
|
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;
|
2022-09-05 17:35:17 +01:00
|
|
|
block.metatileId = selectedMetatiles.at(index).metatileId;
|
2020-05-01 00:30:24 +01:00
|
|
|
if (setCollisions) {
|
2022-09-05 17:35:17 +01:00
|
|
|
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_));
|
2020-08-04 01:49:00 +01:00
|
|
|
}
|
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) {
|
2022-09-05 17:35:17 +01:00
|
|
|
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);
|
2022-09-05 17:35:17 +01:00
|
|
|
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,
|
2022-09-05 17:35:17 +01:00
|
|
|
QList<MetatileSelectionItem> selectedMetatiles,
|
|
|
|
QList<CollisionSelectionItem> selectedCollisions,
|
2020-05-01 00:30:24 +01:00
|
|
|
bool fromScriptCall) {
|
2022-09-05 17:35:17 +01:00
|
|
|
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;
|
2020-02-02 22:25:37 +00:00
|
|
|
int index = j * selectionDimensions.x() + i;
|
2022-09-05 17:35:17 +01:00
|
|
|
uint16_t metatileId = selectedMetatiles.at(index).metatileId;
|
2021-11-24 04:53:41 +00:00
|
|
|
uint16_t old_metatileId = block.metatileId;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (selectedMetatiles.count() != 1 || old_metatileId != metatileId) {
|
2021-11-24 04:53:41 +00:00
|
|
|
block.metatileId = metatileId;
|
2020-02-02 22:25:37 +00:00
|
|
|
if (setCollisions) {
|
2022-09-05 17:35:17 +01:00
|
|
|
CollisionSelectionItem item = selectedCollisions.at(index);
|
|
|
|
block.collision = item.collision;
|
|
|
|
block.elevation = item.elevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
2021-02-13 21:16:52 +00:00
|
|
|
map->setBlock(x, y, block, !fromScriptCall);
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
2021-11-24 04:53:41 +00: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
|
|
|
}
|
2021-11-24 04:53:41 +00: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
|
|
|
}
|
2021-11-24 04:53:41 +00: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
|
|
|
}
|
2021-11-24 04:53:41 +00: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_));
|
2020-08-04 01:49:00 +01:00
|
|
|
}
|
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) {
|
2022-09-05 17:35:17 +01:00
|
|
|
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
|
2018-09-27 00:30:05 +01:00
|
|
|
|
|
|
|
// Smart path should never be enabled without a 3x3 block selection.
|
2022-09-05 17:35:17 +01:00
|
|
|
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.
|
2022-09-05 17:35:17 +01:00
|
|
|
uint16_t openTile = selection.metatileItems.at(4).metatileId;
|
2020-02-02 22:25:37 +00:00
|
|
|
uint16_t openTileCollision = 0;
|
|
|
|
uint16_t openTileElevation = 0;
|
|
|
|
bool setCollisions = false;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
|
|
|
|
CollisionSelectionItem item = selection.collisionItems.at(4);
|
|
|
|
openTileCollision = item.collision;
|
|
|
|
openTileElevation = item.elevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-24 04:53:41 +00:00
|
|
|
uint16_t old_metatileId = block.metatileId;
|
|
|
|
if (old_metatileId == openTile) {
|
2018-09-27 00:30:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-24 04:53:41 +00:00
|
|
|
block.metatileId = openTile;
|
2020-02-02 22:25:37 +00:00
|
|
|
if (setCollisions) {
|
2021-02-13 21:16:52 +00:00
|
|
|
block.collision = openTileCollision;
|
|
|
|
block.elevation = openTileElevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
2021-02-13 21:16:52 +00:00
|
|
|
map->setBlock(x, y, block, !fromScriptCall);
|
2021-11-24 04:53:41 +00:00
|
|
|
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));
|
|
|
|
}
|
2021-11-24 04:53:41 +00:00
|
|
|
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));
|
|
|
|
}
|
2021-11-24 04:53:41 +00:00
|
|
|
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));
|
|
|
|
}
|
2021-11-24 04:53:41 +00:00
|
|
|
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.
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(x, y - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 1;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(x + 1, y, &right) && isSmartPathTile(selection.metatileItems, right.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 2;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(x, y + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 4;
|
2022-09-05 17:35:17 +01:00
|
|
|
if (map->getBlock(x - 1, y, &left) && isSmartPathTile(selection.metatileItems, left.metatileId))
|
2018-09-27 00:30:05 +01:00
|
|
|
id += 8;
|
|
|
|
|
2022-09-05 17:35:17 +01:00
|
|
|
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId;
|
2020-02-02 22:25:37 +00:00
|
|
|
if (setCollisions) {
|
2022-09-05 17:35:17 +01:00
|
|
|
CollisionSelectionItem item = selection.collisionItems.at(smartPathTable[id]);
|
|
|
|
block.collision = item.collision;
|
|
|
|
block.elevation = item.elevation;
|
2020-02-02 22:25:37 +00:00
|
|
|
}
|
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.
|
2022-09-05 17:35:17 +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
|
|
|
}
|
2022-09-05 17:35:17 +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
|
|
|
}
|
2022-09-05 17:35:17 +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
|
|
|
}
|
2022-09-05 17:35:17 +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_));
|
2020-08-04 05:32:50 +01:00
|
|
|
}
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2021-02-13 21:16:52 +00:00
|
|
|
Block block;
|
|
|
|
if (map->getBlock(pos.x(), pos.y(), &block)) {
|
2021-11-24 04:53:41 +00:00
|
|
|
this->metatileSelector->selectFromMap(block.metatileId, block.collision, block.elevation);
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
|
2020-10-02 20:32:22 +01:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
2018-09-27 00:30:05 +01:00
|
|
|
if (event->type() == QEvent::GraphicsSceneMousePress) {
|
2020-10-02 20:32:22 +01:00
|
|
|
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();
|
2020-10-02 20:32:22 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 23:30:35 +00:00
|
|
|
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());
|
2021-12-12 17:19:59 +00:00
|
|
|
if (pos != this->metatilePos) {
|
|
|
|
this->metatilePos = pos;
|
|
|
|
emit this->hoveredMapMetatileChanged(pos);
|
|
|
|
}
|
2019-11-02 18:40:43 +00:00
|
|
|
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) {
|
2020-09-07 19:05:17 +01:00
|
|
|
this->has_mouse = true;
|
2021-12-13 16:41:46 +00:00
|
|
|
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
|
|
|
|
emit this->hoveredMapMetatileChanged(pos);
|
2020-09-07 19:05:17 +01:00
|
|
|
}
|
2018-09-27 00:30:05 +01:00
|
|
|
void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
|
|
|
|
emit this->hoveredMapMetatileCleared();
|
2019-11-02 18:40:43 +00:00
|
|
|
if (this->settings->betterCursors && this->paintingMode != MapPixmapItem::PaintMode::Disabled) {
|
2018-09-27 00:30:05 +01:00
|
|
|
unsetCursor();
|
|
|
|
}
|
2020-09-07 19:05:17 +01:00
|
|
|
this->has_mouse = false;
|
2018-09-27 00:30:05 +01:00
|
|
|
}
|
|
|
|
void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
2020-10-02 20:32:22 +01:00
|
|
|
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();
|
2019-01-11 14:52:35 +00:00
|
|
|
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());
|
2021-12-12 17:19:59 +00:00
|
|
|
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) {
|
2020-08-21 17:02:48 +01:00
|
|
|
this->lockedAxis = MapPixmapItem::Axis::None;
|
2019-01-11 14:52:35 +00:00
|
|
|
emit endPaint(event, this);
|
2018-09-27 00:30:05 +01:00
|
|
|
emit mouseEvent(event, this);
|
|
|
|
}
|