Better handling of generic PaintSelection, and accurate selection cursor size

This commit is contained in:
Marcus Huderle 2023-02-28 16:23:10 -06:00
parent 64004a68f2
commit 23747d9822
17 changed files with 147 additions and 124 deletions

View file

@ -164,6 +164,8 @@ public slots:
void maskNonVisibleConnectionTiles(); void maskNonVisibleConnectionTiles();
void onBorderMetatilesChanged(); void onBorderMetatilesChanged();
void selectedEventIndexChanged(int index, Event::Group eventGroup); void selectedEventIndexChanged(int index, Event::Group eventGroup);
void onSelectedMetatilesChanged();
void onSelectedStampsChanged();
private: private:
void setConnectionItemsVisible(bool); void setConnectionItemsVisible(bool);
@ -207,7 +209,6 @@ private slots:
void onHoveredMapMetatileCleared(); void onHoveredMapMetatileCleared();
void onHoveredMapMovementPermissionChanged(int, int); void onHoveredMapMovementPermissionChanged(int, int);
void onHoveredMapMovementPermissionCleared(); void onHoveredMapMovementPermissionCleared();
void onSelectedMetatilesChanged();
void onWheelZoom(int); void onWheelZoom(int);
void onToggleGridClicked(bool); void onToggleGridClicked(bool);

View file

@ -18,6 +18,6 @@ public:
void setMap(Map *map) { this->map = map; } void setMap(Map *map) { this->map = map; }
}; };
QPixmap drawMetatileSelection(MetatileSelection selection, Map *map); QPixmap drawMetatileSelection(MetatileSelection *selection, Map *map);
#endif // CURRENTSELECTEDMETATILESPIXMAPITEM_H #endif // CURRENTSELECTEDMETATILESPIXMAPITEM_H

View file

@ -91,6 +91,7 @@ public:
QPoint adjustCoords(QPoint pos); QPoint adjustCoords(QPoint pos);
private: private:
PaintSelection* getActiveSelection();
void paintSmartPath(int x, int y, bool fromScriptCall = false); void paintSmartPath(int x, int y, bool fromScriptCall = false);
static QList<int> smartPathTable; static QList<int> smartPathTable;

View file

@ -18,7 +18,7 @@ public:
this->map = map; this->map = map;
this->primaryTileset = map->layout->tileset_primary; this->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary; this->secondaryTileset = map->layout->tileset_secondary;
this->selection = MetatileSelection(); this->selection = new MetatileSelection();
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
QPoint getSelectionDimensions(); QPoint getSelectionDimensions();
@ -26,7 +26,7 @@ public:
bool select(uint16_t metatile); bool select(uint16_t metatile);
bool selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation); bool selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation);
void setTilesets(Tileset*, Tileset*); void setTilesets(Tileset*, Tileset*);
MetatileSelection getMetatileSelection(); MetatileSelection* getMetatileSelection();
void setPrefabSelection(MetatileSelection selection); void setPrefabSelection(MetatileSelection selection);
void setExternalSelection(int, int, QList<uint16_t>, QList<QPair<uint16_t, uint16_t>>); void setExternalSelection(int, int, QList<uint16_t>, QList<QPair<uint16_t, uint16_t>>);
QPoint getMetatileIdCoordsOnWidget(uint16_t); QPoint getMetatileIdCoordsOnWidget(uint16_t);
@ -47,7 +47,7 @@ private:
int externalSelectionWidth; int externalSelectionWidth;
int externalSelectionHeight; int externalSelectionHeight;
QList<uint16_t> externalSelectedMetatiles; QList<uint16_t> externalSelectedMetatiles;
MetatileSelection selection; MetatileSelection *selection;
void updateSelectedMetatiles(); void updateSelectedMetatiles();
void updateExternalSelectedMetatiles(); void updateExternalSelectedMetatiles();

View file

@ -31,6 +31,12 @@ class MetatileSelection: public PaintSelection
{ {
public: public:
MetatileSelection() {} MetatileSelection() {}
void clone(MetatileSelection other) {
this->dimensions = other.dimensions;
this->hasCollision = other.hasCollision;
this->metatileItems = other.metatileItems;
this->collisionItems = other.collisionItems;
}
bool paintNormal(int index, Block *block) override; bool paintNormal(int index, Block *block) override;
bool hasCollision = false; bool hasCollision = false;
QList<MetatileSelectionItem> metatileItems; QList<MetatileSelectionItem> metatileItems;

View file

@ -21,7 +21,7 @@ class Prefab
{ {
public: public:
void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map); void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map);
void addPrefab(MetatileSelection selection, Map *map, QString name); void addPrefab(MetatileSelection *selection, Map *map, QString name);
void updatePrefabUi(Map *map); void updatePrefabUi(Map *map);
void tryImportDefaultPrefabs(Map *map); void tryImportDefaultPrefabs(Map *map);

View file

@ -22,7 +22,7 @@ public:
private: private:
Map *map; Map *map;
Ui::PrefabCreationDialog *ui; Ui::PrefabCreationDialog *ui;
MetatileSelection selection; MetatileSelection *selection;
}; };
#endif // PREFABCREATIONDIALOG_H #endif // PREFABCREATIONDIALOG_H

View file

@ -15,13 +15,13 @@ public:
this->map = map; this->map = map;
this->primaryTileset = map->layout->tileset_primary; this->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary; this->secondaryTileset = map->layout->tileset_secondary;
this->selection = StampSelection{}; this->selection = new StampSelection();
} }
QPoint getSelectionDimensions(); QPoint getSelectionDimensions();
void draw(); void draw();
bool select(uint16_t stampId); bool select(uint16_t stampId);
void setTilesets(Tileset*, Tileset*); void setTilesets(Tileset*, Tileset*);
StampSelection getStampSelection(); StampSelection* getStampSelection();
QPoint getStampIdCoordsOnWidget(uint16_t); QPoint getStampIdCoordsOnWidget(uint16_t);
void setMap(Map*); void setMap(Map*);
Tileset *primaryTileset; Tileset *primaryTileset;
@ -33,7 +33,7 @@ protected:
private: private:
int numStampsWide; int numStampsWide;
Map *map; Map *map;
StampSelection selection; StampSelection *selection;
void updateSelectedStamps(); void updateSelectedStamps();
uint16_t getStampId(int x, int y); uint16_t getStampId(int x, int y);

View file

@ -957,6 +957,12 @@ void Editor::onSelectedMetatilesChanged() {
this->redrawCurrentMetatilesSelection(); this->redrawCurrentMetatilesSelection();
} }
void Editor::onSelectedStampsChanged() {
QPoint size = this->stamp_selector_item->getSelectionDimensions();
this->cursorMapTileRect->updateSelectionSize(size.x(), size.y());
this->redrawCurrentMetatilesSelection();
}
void Editor::onWheelZoom(int s) { void Editor::onWheelZoom(int s) {
// Don't zoom the map when the user accidentally scrolls while performing a magic fill. (ctrl + middle button click) // Don't zoom the map when the user accidentally scrolls while performing a magic fill. (ctrl + middle button click)
if (!(QApplication::mouseButtons() & Qt::MiddleButton)) { if (!(QApplication::mouseButtons() & Qt::MiddleButton)) {
@ -1410,6 +1416,8 @@ void Editor::displayStampSelector() {
scene_stamps = new QGraphicsScene; scene_stamps = new QGraphicsScene;
if (!stamp_selector_item) { if (!stamp_selector_item) {
stamp_selector_item = new StampSelector(8, map); stamp_selector_item = new StampSelector(8, map);
connect(stamp_selector_item, &StampSelector::selectedStampsChanged,
this, &Editor::onSelectedStampsChanged);
stamp_selector_item->select(0); stamp_selector_item->select(0);
} else { } else {
stamp_selector_item->setMap(map); stamp_selector_item->setMap(map);

View file

@ -1334,8 +1334,8 @@ void MainWindow::redrawMetatileSelection()
QPoint size = editor->metatile_selector_item->getSelectionDimensions(); QPoint size = editor->metatile_selector_item->getSelectionDimensions();
if (size.x() == 1 && size.y() == 1) { if (size.x() == 1 && size.y() == 1) {
MetatileSelection selection = editor->metatile_selector_item->getMetatileSelection(); MetatileSelection *selection = editor->metatile_selector_item->getMetatileSelection();
QPoint pos = editor->metatile_selector_item->getMetatileIdCoordsOnWidget(selection.metatileItems.first().metatileId); QPoint pos = editor->metatile_selector_item->getMetatileIdCoordsOnWidget(selection->metatileItems.first().metatileId);
pos *= scale; pos *= scale;
ui->scrollArea_2->ensureVisible(pos.x(), pos.y(), 8 * scale, 8 * scale); ui->scrollArea_2->ensureVisible(pos.x(), pos.y(), 8 * scale, 8 * scale);
} }
@ -1345,8 +1345,8 @@ void MainWindow::currentMetatilesSelectionChanged()
{ {
redrawMetatileSelection(); redrawMetatileSelection();
if (this->tilesetEditor) { if (this->tilesetEditor) {
MetatileSelection selection = editor->metatile_selector_item->getMetatileSelection(); MetatileSelection *selection = editor->metatile_selector_item->getMetatileSelection();
this->tilesetEditor->selectMetatile(selection.metatileItems.first().metatileId); this->tilesetEditor->selectMetatile(selection->metatileItems.first().metatileId);
} }
} }
@ -1419,13 +1419,13 @@ void MainWindow::copy() {
OrderedJson::object copyObject; OrderedJson::object copyObject;
copyObject["object"] = "metatile_selection"; copyObject["object"] = "metatile_selection";
OrderedJson::array metatiles; OrderedJson::array metatiles;
MetatileSelection selection = editor->metatile_selector_item->getMetatileSelection(); MetatileSelection *selection = editor->metatile_selector_item->getMetatileSelection();
for (auto item : selection.metatileItems) { for (auto item : selection->metatileItems) {
metatiles.append(static_cast<int>(item.metatileId)); metatiles.append(static_cast<int>(item.metatileId));
} }
OrderedJson::array collisions; OrderedJson::array collisions;
if (selection.hasCollision) { if (selection->hasCollision) {
for (auto item : selection.collisionItems) { for (auto item : selection->collisionItems) {
OrderedJson::object collision; OrderedJson::object collision;
collision["collision"] = item.collision; collision["collision"] = item.collision;
collision["elevation"] = item.elevation; collision["elevation"] = item.elevation;
@ -1673,12 +1673,16 @@ void MainWindow::on_mapViewTab_tabBarClicked(int index)
if (index == 0) { if (index == 0) {
editor->setEditingMap(); editor->setEditingMap();
editor->onSelectedMetatilesChanged();
} else if (index == 1) { } else if (index == 1) {
editor->setEditingCollision(); editor->setEditingCollision();
editor->onSelectedMetatilesChanged();
} else if (index == 2) { } else if (index == 2) {
editor->setEditingMap(); editor->setEditingMap();
editor->onSelectedStampsChanged();
} else if (index == 3) { } else if (index == 3) {
editor->setEditingMap(); editor->setEditingMap();
editor->onSelectedMetatilesChanged();
prefab.tryImportDefaultPrefabs(this->editor->map); prefab.tryImportDefaultPrefabs(this->editor->map);
} }
editor->setCursorRectVisible(false); editor->setCursorRectVisible(false);
@ -2684,8 +2688,8 @@ void MainWindow::on_actionTileset_Editor_triggered()
this->tilesetEditor->activateWindow(); this->tilesetEditor->activateWindow();
} }
MetatileSelection selection = this->editor->metatile_selector_item->getMetatileSelection(); MetatileSelection *selection = this->editor->metatile_selector_item->getMetatileSelection();
this->tilesetEditor->selectMetatile(selection.metatileItems.first().metatileId); this->tilesetEditor->selectMetatile(selection->metatileItems.first().metatileId);
} }
void MainWindow::initTilesetEditor() { void MainWindow::initTilesetEditor() {

View file

@ -5,16 +5,16 @@
#include <QPainter> #include <QPainter>
void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
int width = map->getBorderWidth(); int width = map->getBorderWidth();
int height = map->getBorderHeight(); int height = map->getBorderHeight();
Blockdata oldBorder = map->layout->border; Blockdata oldBorder = map->layout->border;
for (int i = 0; i < selection.dimensions.x() && (i + pos.x()) < width; i++) { for (int i = 0; i < selection->dimensions.x() && (i + pos.x()) < width; i++) {
for (int j = 0; j < selection.dimensions.y() && (j + pos.y()) < height; j++) { for (int j = 0; j < selection->dimensions.y() && (j + pos.y()) < height; j++) {
MetatileSelectionItem item = selection.metatileItems.at(j * selection.dimensions.x() + i); MetatileSelectionItem item = selection->metatileItems.at(j * selection->dimensions.x() + i);
map->setBorderMetatileId(pos.x() + i, pos.y() + j, item.metatileId, true); map->setBorderMetatileId(pos.x() + i, pos.y() + j, item.metatileId, true);
} }
} }

View file

@ -2,20 +2,20 @@
#include "imageproviders.h" #include "imageproviders.h"
#include <QPainter> #include <QPainter>
QPixmap drawMetatileSelection(MetatileSelection selection, Map *map) { QPixmap drawMetatileSelection(MetatileSelection *selection, Map *map) {
int width = selection.dimensions.x() * 16; int width = selection->dimensions.x() * 16;
int height = selection.dimensions.y() * 16; int height = selection->dimensions.y() * 16;
QImage image(width, height, QImage::Format_RGBA8888); QImage image(width, height, QImage::Format_RGBA8888);
image.fill(QColor(0, 0, 0, 0)); image.fill(QColor(0, 0, 0, 0));
QPainter painter(&image); QPainter painter(&image);
for (int i = 0; i < selection.dimensions.x(); i++) { for (int i = 0; i < selection->dimensions.x(); i++) {
for (int j = 0; j < selection.dimensions.y(); j++) { for (int j = 0; j < selection->dimensions.y(); j++) {
int x = i * 16; int x = i * 16;
int y = j * 16; int y = j * 16;
QPoint metatile_origin = QPoint(x, y); QPoint metatile_origin = QPoint(x, y);
int index = j * selection.dimensions.x() + i; int index = j * selection->dimensions.x() + i;
MetatileSelectionItem item = selection.metatileItems.at(index); MetatileSelectionItem item = selection->metatileItems.at(index);
if (item.enabled) { if (item.enabled) {
QImage metatile_image = getMetatileImage( QImage metatile_image = getMetatileImage(
item.metatileId, item.metatileId,
@ -33,6 +33,6 @@ QPixmap drawMetatileSelection(MetatileSelection selection, Map *map) {
} }
void CurrentSelectedMetatilesPixmapItem::draw() { void CurrentSelectedMetatilesPixmapItem::draw() {
MetatileSelection selection = metatileSelector->getMetatileSelection(); MetatileSelection *selection = metatileSelector->getMetatileSelection();
setPixmap(drawMetatileSelection(selection, this->map)); setPixmap(drawMetatileSelection(selection, this->map));
} }

View file

@ -25,7 +25,8 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
// Paint onto the map. // Paint onto the map.
bool shiftPressed = event->modifiers() & Qt::ShiftModifier; bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions(); PaintSelection *selection = this->getActiveSelection();
QPoint selectionDimensions = selection->dimensions;
if (settings->smartPathsEnabled) { if (settings->smartPathsEnabled) {
if (!shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) { if (!shiftPressed && selectionDimensions.x() == 3 && selectionDimensions.y() == 3) {
paintSmartPath(pos.x(), pos.y()); paintSmartPath(pos.x(), pos.y());
@ -101,15 +102,16 @@ void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) {
} }
} }
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) { PaintSelection* MapPixmapItem::getActiveSelection() {
PaintSelection *selection = nullptr;
if (this->getActivePaintType() == PaintType::PaintTypeMetatile) { if (this->getActivePaintType() == PaintType::PaintTypeMetatile) {
MetatileSelection metatileSelection = this->metatileSelector->getMetatileSelection(); return this->metatileSelector->getMetatileSelection();
selection = &metatileSelection;
} else { } else {
StampSelection stampSelection = this->stampSelector->getStampSelection(); return this->stampSelector->getStampSelection();
selection = &stampSelection;
} }
}
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
PaintSelection *selection = this->getActiveSelection();
int initialX = fromScriptCall ? x : this->paint_tile_initial_x; int initialX = fromScriptCall ? x : this->paint_tile_initial_x;
int initialY = fromScriptCall ? y : this->paint_tile_initial_y; int initialY = fromScriptCall ? y : this->paint_tile_initial_y;
int selectionWidth = selection->dimensions.x(); int selectionWidth = selection->dimensions.x();
@ -179,12 +181,12 @@ bool isSmartPathTile(QList<MetatileSelectionItem> metatileItems, uint16_t metati
return false; return false;
} }
bool isValidSmartPathSelection(MetatileSelection selection) { bool isValidSmartPathSelection(MetatileSelection *selection) {
if (selection.dimensions.x() != 3 || selection.dimensions.y() != 3) if (selection->dimensions.x() != 3 || selection->dimensions.y() != 3)
return false; return false;
for (int i = 0; i < selection.metatileItems.length(); i++) { for (int i = 0; i < selection->metatileItems.length(); i++) {
if (!selection.metatileItems.at(i).enabled) if (!selection->metatileItems.at(i).enabled)
return false; return false;
} }
@ -192,18 +194,21 @@ bool isValidSmartPathSelection(MetatileSelection selection) {
} }
void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) { void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); if (this->getActivePaintType() != PaintType::PaintTypeMetatile) {
return;
}
MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
if (!isValidSmartPathSelection(selection)) if (!isValidSmartPathSelection(selection))
return; return;
// Shift to the middle tile of the smart path selection. // Shift to the middle tile of the smart path selection.
uint16_t openTile = selection.metatileItems.at(4).metatileId; uint16_t openTile = selection->metatileItems.at(4).metatileId;
uint16_t openTileCollision = 0; uint16_t openTileCollision = 0;
uint16_t openTileElevation = 0; uint16_t openTileElevation = 0;
bool setCollisions = false; bool setCollisions = false;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) { if (selection->hasCollision && selection->collisionItems.length() == selection->metatileItems.length()) {
openTileCollision = selection.collisionItems.at(4).collision; openTileCollision = selection->collisionItems.at(4).collision;
openTileElevation = selection.collisionItems.at(4).elevation; openTileElevation = selection->collisionItems.at(4).elevation;
setCollisions = true; setCollisions = true;
} }
@ -242,7 +247,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
int actualX = i + x; int actualX = i + x;
int actualY = j + y; int actualY = j + y;
Block block; Block block;
if (!map->getBlock(actualX, actualY, &block) || !isSmartPathTile(selection.metatileItems, block.metatileId)) { if (!map->getBlock(actualX, actualY, &block) || !isSmartPathTile(selection->metatileItems, block.metatileId)) {
continue; continue;
} }
@ -253,18 +258,18 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
Block left; Block left;
// Get marching squares value, to determine which tile to use. // Get marching squares value, to determine which tile to use.
if (map->getBlock(actualX, actualY - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId)) if (map->getBlock(actualX, actualY - 1, &top) && isSmartPathTile(selection->metatileItems, top.metatileId))
id += 1; id += 1;
if (map->getBlock(actualX + 1, actualY, &right) && isSmartPathTile(selection.metatileItems, right.metatileId)) if (map->getBlock(actualX + 1, actualY, &right) && isSmartPathTile(selection->metatileItems, right.metatileId))
id += 2; id += 2;
if (map->getBlock(actualX, actualY + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId)) if (map->getBlock(actualX, actualY + 1, &bottom) && isSmartPathTile(selection->metatileItems, bottom.metatileId))
id += 4; id += 4;
if (map->getBlock(actualX - 1, actualY, &left) && isSmartPathTile(selection.metatileItems, left.metatileId)) if (map->getBlock(actualX - 1, actualY, &left) && isSmartPathTile(selection->metatileItems, left.metatileId))
id += 8; id += 8;
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId; block.metatileId = selection->metatileItems.at(smartPathTable[id]).metatileId;
if (setCollisions) { if (setCollisions) {
CollisionSelectionItem collisionItem = selection.collisionItems.at(smartPathTable[id]); CollisionSelectionItem collisionItem = selection->collisionItems.at(smartPathTable[id]);
block.collision = collisionItem.collision; block.collision = collisionItem.collision;
block.elevation = collisionItem.elevation; block.elevation = collisionItem.elevation;
} }
@ -371,11 +376,11 @@ void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
} else { } else {
QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
Block block; Block block;
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
int metatileId = selection.metatileItems.first().metatileId; int metatileId = selection->metatileItems.first().metatileId;
if (selection.metatileItems.count() > 1 || (map->getBlock(pos.x(), pos.y(), &block) && block.metatileId != metatileId)) { if (selection->metatileItems.count() > 1 || (map->getBlock(pos.x(), pos.y(), &block) && block.metatileId != metatileId)) {
bool smartPathsEnabled = event->modifiers() & Qt::ShiftModifier; bool smartPathsEnabled = event->modifiers() & Qt::ShiftModifier;
if ((this->settings->smartPathsEnabled || smartPathsEnabled) && selection.dimensions.x() == 3 && selection.dimensions.y() == 3) if ((this->settings->smartPathsEnabled || smartPathsEnabled) && selection->dimensions.x() == 3 && selection->dimensions.y() == 3)
this->floodFillSmartPath(pos.x(), pos.y()); this->floodFillSmartPath(pos.x(), pos.y());
else else
this->floodFill(pos.x(), pos.y()); this->floodFill(pos.x(), pos.y());
@ -402,8 +407,8 @@ void MapPixmapItem::magicFill(int x, int y, uint16_t metatileId, bool fromScript
} }
void MapPixmapItem::magicFill(int x, int y, bool fromScriptCall) { void MapPixmapItem::magicFill(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
this->magicFill(x, y, selection.dimensions, selection.metatileItems, selection.collisionItems, fromScriptCall); this->magicFill(x, y, selection->dimensions, selection->metatileItems, selection->collisionItems, fromScriptCall);
} }
void MapPixmapItem::magicFill( void MapPixmapItem::magicFill(
@ -453,8 +458,8 @@ void MapPixmapItem::magicFill(
} }
void MapPixmapItem::floodFill(int initialX, int initialY, bool fromScriptCall) { void MapPixmapItem::floodFill(int initialX, int initialY, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
this->floodFill(initialX, initialY, selection.dimensions, selection.metatileItems, selection.collisionItems, fromScriptCall); this->floodFill(initialX, initialY, selection->dimensions, selection->metatileItems, selection->collisionItems, fromScriptCall);
} }
void MapPixmapItem::floodFill(int initialX, int initialY, uint16_t metatileId, bool fromScriptCall) { void MapPixmapItem::floodFill(int initialX, int initialY, uint16_t metatileId, bool fromScriptCall) {
@ -528,17 +533,17 @@ void MapPixmapItem::floodFill(
} }
void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScriptCall) { void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection(); MetatileSelection *selection = this->metatileSelector->getMetatileSelection();
if (!isValidSmartPathSelection(selection)) if (!isValidSmartPathSelection(selection))
return; return;
// Shift to the middle tile of the smart path selection. // Shift to the middle tile of the smart path selection.
uint16_t openTile = selection.metatileItems.at(4).metatileId; uint16_t openTile = selection->metatileItems.at(4).metatileId;
uint16_t openTileCollision = 0; uint16_t openTileCollision = 0;
uint16_t openTileElevation = 0; uint16_t openTileElevation = 0;
bool setCollisions = false; bool setCollisions = false;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) { if (selection->hasCollision && selection->collisionItems.length() == selection->metatileItems.length()) {
CollisionSelectionItem item = selection.collisionItems.at(4); CollisionSelectionItem item = selection->collisionItems.at(4);
openTileCollision = item.collision; openTileCollision = item.collision;
openTileElevation = item.elevation; openTileElevation = item.elevation;
setCollisions = true; setCollisions = true;
@ -604,37 +609,37 @@ void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScri
Block left; Block left;
// Get marching squares value, to determine which tile to use. // Get marching squares value, to determine which tile to use.
if (map->getBlock(x, y - 1, &top) && isSmartPathTile(selection.metatileItems, top.metatileId)) if (map->getBlock(x, y - 1, &top) && isSmartPathTile(selection->metatileItems, top.metatileId))
id += 1; id += 1;
if (map->getBlock(x + 1, y, &right) && isSmartPathTile(selection.metatileItems, right.metatileId)) if (map->getBlock(x + 1, y, &right) && isSmartPathTile(selection->metatileItems, right.metatileId))
id += 2; id += 2;
if (map->getBlock(x, y + 1, &bottom) && isSmartPathTile(selection.metatileItems, bottom.metatileId)) if (map->getBlock(x, y + 1, &bottom) && isSmartPathTile(selection->metatileItems, bottom.metatileId))
id += 4; id += 4;
if (map->getBlock(x - 1, y, &left) && isSmartPathTile(selection.metatileItems, left.metatileId)) if (map->getBlock(x - 1, y, &left) && isSmartPathTile(selection->metatileItems, left.metatileId))
id += 8; id += 8;
block.metatileId = selection.metatileItems.at(smartPathTable[id]).metatileId; block.metatileId = selection->metatileItems.at(smartPathTable[id]).metatileId;
if (setCollisions) { if (setCollisions) {
CollisionSelectionItem item = selection.collisionItems.at(smartPathTable[id]); CollisionSelectionItem item = selection->collisionItems.at(smartPathTable[id]);
block.collision = item.collision; block.collision = item.collision;
block.elevation = item.elevation; block.elevation = item.elevation;
} }
map->setBlock(x, y, block, !fromScriptCall); map->setBlock(x, y, block, !fromScriptCall);
// Visit neighbors if they are smart-path tiles, and don't revisit any. // 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)) { if (!visited.contains(x + 1 + y * map->getWidth()) && map->getBlock(x + 1, y, &block) && isSmartPathTile(selection->metatileItems, block.metatileId)) {
todo.append(QPoint(x + 1, y)); todo.append(QPoint(x + 1, y));
visited.insert(x + 1 + y * map->getWidth()); visited.insert(x + 1 + y * map->getWidth());
} }
if (!visited.contains(x - 1 + y * map->getWidth()) && map->getBlock(x - 1, y, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) { if (!visited.contains(x - 1 + y * map->getWidth()) && map->getBlock(x - 1, y, &block) && isSmartPathTile(selection->metatileItems, block.metatileId)) {
todo.append(QPoint(x - 1, y)); todo.append(QPoint(x - 1, y));
visited.insert(x - 1 + y * map->getWidth()); visited.insert(x - 1 + y * map->getWidth());
} }
if (!visited.contains(x + (y + 1) * map->getWidth()) && map->getBlock(x, y + 1, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) { if (!visited.contains(x + (y + 1) * map->getWidth()) && map->getBlock(x, y + 1, &block) && isSmartPathTile(selection->metatileItems, block.metatileId)) {
todo.append(QPoint(x, y + 1)); todo.append(QPoint(x, y + 1));
visited.insert(x + (y + 1) * map->getWidth()); visited.insert(x + (y + 1) * map->getWidth());
} }
if (!visited.contains(x + (y - 1) * map->getWidth()) && map->getBlock(x, y - 1, &block) && isSmartPathTile(selection.metatileItems, block.metatileId)) { if (!visited.contains(x + (y - 1) * map->getWidth()) && map->getBlock(x, y - 1, &block) && isSmartPathTile(selection->metatileItems, block.metatileId)) {
todo.append(QPoint(x, y - 1)); todo.append(QPoint(x, y - 1));
visited.insert(x + (y - 1) * map->getWidth()); visited.insert(x + (y - 1) * map->getWidth());
} }

View file

@ -4,7 +4,7 @@
#include <QPainter> #include <QPainter>
QPoint MetatileSelector::getSelectionDimensions() { QPoint MetatileSelector::getSelectionDimensions() {
return selection.dimensions; return selection->dimensions;
} }
void MetatileSelector::draw() { void MetatileSelector::draw() {
@ -45,11 +45,10 @@ bool MetatileSelector::select(uint16_t metatileId) {
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return false; if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return false;
this->externalSelection = false; this->externalSelection = false;
this->prefabSelection = false; this->prefabSelection = false;
this->selection = MetatileSelection(); this->selection->dimensions = QPoint(1, 1);
this->selection.dimensions = QPoint(1, 1); this->selection->hasCollision = false;
this->selection.hasCollision = false; this->selection->metatileItems = QList<MetatileSelectionItem>({MetatileSelectionItem{true, metatileId}});
this->selection.metatileItems = QList<MetatileSelectionItem>({MetatileSelectionItem{true, metatileId}}); this->selection->collisionItems = QList<CollisionSelectionItem>();
this->selection.collisionItems = QList<CollisionSelectionItem>();
QPoint coords = this->getMetatileIdCoords(metatileId); QPoint coords = this->getMetatileIdCoords(metatileId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->updateSelectedMetatiles(); this->updateSelectedMetatiles();
@ -59,8 +58,8 @@ bool MetatileSelector::select(uint16_t metatileId) {
bool MetatileSelector::selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation) { bool MetatileSelector::selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation) {
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return false; if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return false;
this->select(metatileId); this->select(metatileId);
this->selection.collisionItems.append(CollisionSelectionItem{true, collision, elevation}); this->selection->collisionItems.append(CollisionSelectionItem{true, collision, elevation});
this->selection.hasCollision = true; this->selection->hasCollision = true;
return true; return true;
} }
@ -74,7 +73,7 @@ void MetatileSelector::setTilesets(Tileset *primaryTileset, Tileset *secondaryTi
this->draw(); this->draw();
} }
MetatileSelection MetatileSelector::getMetatileSelection() { MetatileSelection* MetatileSelector::getMetatileSelection() {
return selection; return selection;
} }
@ -84,18 +83,18 @@ void MetatileSelector::setExternalSelection(int width, int height, QList<uint16_
this->externalSelectionWidth = width; this->externalSelectionWidth = width;
this->externalSelectionHeight = height; this->externalSelectionHeight = height;
this->externalSelectedMetatiles.clear(); this->externalSelectedMetatiles.clear();
this->selection.metatileItems.clear(); this->selection->metatileItems.clear();
this->selection.collisionItems.clear(); this->selection->collisionItems.clear();
this->selection.hasCollision = true; this->selection->hasCollision = true;
this->selection.dimensions = QPoint(width, height); this->selection->dimensions = QPoint(width, height);
for (int i = 0; i < metatiles.length(); i++) { for (int i = 0; i < metatiles.length(); i++) {
auto collision = collisions.at(i); auto collision = collisions.at(i);
this->selection.collisionItems.append(CollisionSelectionItem{true, collision.first, collision.second}); this->selection->collisionItems.append(CollisionSelectionItem{true, collision.first, collision.second});
uint16_t metatileId = metatiles.at(i); uint16_t metatileId = metatiles.at(i);
this->externalSelectedMetatiles.append(metatileId); this->externalSelectedMetatiles.append(metatileId);
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
metatileId = 0; metatileId = 0;
this->selection.metatileItems.append(MetatileSelectionItem{true, metatileId}); this->selection->metatileItems.append(MetatileSelectionItem{true, metatileId});
} }
this->draw(); this->draw();
@ -106,7 +105,7 @@ void MetatileSelector::setPrefabSelection(MetatileSelection selection) {
this->externalSelection = false; this->externalSelection = false;
this->prefabSelection = true; this->prefabSelection = true;
this->externalSelectedMetatiles.clear(); this->externalSelectedMetatiles.clear();
this->selection = selection; this->selection->clone(selection);
this->draw(); this->draw();
emit selectedMetatilesChanged(); emit selectedMetatilesChanged();
} }
@ -151,30 +150,30 @@ void MetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
void MetatileSelector::updateSelectedMetatiles() { void MetatileSelector::updateSelectedMetatiles() {
this->externalSelection = false; this->externalSelection = false;
this->prefabSelection = false; this->prefabSelection = false;
this->selection.metatileItems.clear(); this->selection->metatileItems.clear();
this->selection.collisionItems.clear(); this->selection->collisionItems.clear();
this->selection.hasCollision = false; this->selection->hasCollision = false;
this->selection.dimensions = SelectablePixmapItem::getSelectionDimensions(); this->selection->dimensions = SelectablePixmapItem::getSelectionDimensions();
QPoint origin = this->getSelectionStart(); QPoint origin = this->getSelectionStart();
for (int j = 0; j < this->selection.dimensions.y(); j++) { for (int j = 0; j < this->selection->dimensions.y(); j++) {
for (int i = 0; i < this->selection.dimensions.x(); i++) { for (int i = 0; i < this->selection->dimensions.x(); i++) {
uint16_t metatileId = this->getMetatileId(origin.x() + i, origin.y() + j); uint16_t metatileId = this->getMetatileId(origin.x() + i, origin.y() + j);
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
metatileId = 0; metatileId = 0;
this->selection.metatileItems.append(MetatileSelectionItem{true, metatileId}); this->selection->metatileItems.append(MetatileSelectionItem{true, metatileId});
} }
} }
emit selectedMetatilesChanged(); emit selectedMetatilesChanged();
} }
void MetatileSelector::updateExternalSelectedMetatiles() { void MetatileSelector::updateExternalSelectedMetatiles() {
this->selection.metatileItems.clear(); this->selection->metatileItems.clear();
this->selection.dimensions = QPoint(this->externalSelectionWidth, this->externalSelectionHeight); this->selection->dimensions = QPoint(this->externalSelectionWidth, this->externalSelectionHeight);
for (int i = 0; i < this->externalSelectedMetatiles.count(); ++i) { for (int i = 0; i < this->externalSelectedMetatiles.count(); ++i) {
uint16_t metatileId = this->externalSelectedMetatiles.at(i); uint16_t metatileId = this->externalSelectedMetatiles.at(i);
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
metatileId = 0; metatileId = 0;
this->selection.metatileItems.append(MetatileSelectionItem{true, metatileId}); this->selection->metatileItems.append(MetatileSelectionItem{true, metatileId});
} }
emit selectedMetatilesChanged(); emit selectedMetatilesChanged();
} }

View file

@ -199,7 +199,7 @@ void Prefab::updatePrefabUi(Map *map) {
frame->ui->label_Name->setText(item.name); frame->ui->label_Name->setText(item.name);
auto scene = new QGraphicsScene; auto scene = new QGraphicsScene;
scene->addPixmap(drawMetatileSelection(item.selection, map)); scene->addPixmap(drawMetatileSelection(&item.selection, map));
scene->setSceneRect(scene->itemsBoundingRect()); scene->setSceneRect(scene->itemsBoundingRect());
frame->ui->graphicsView_Prefab->setScene(scene); frame->ui->graphicsView_Prefab->setScene(scene);
frame->ui->graphicsView_Prefab->setFixedSize(scene->itemsBoundingRect().width() + 2, frame->ui->graphicsView_Prefab->setFixedSize(scene->itemsBoundingRect().width() + 2,
@ -243,12 +243,12 @@ void Prefab::updatePrefabUi(Map *map) {
prefabWidget->layout()->addItem(verticalSpacer); prefabWidget->layout()->addItem(verticalSpacer);
} }
void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) { void Prefab::addPrefab(MetatileSelection *selection, Map *map, QString name) {
// First, determine which tilesets are actually used in this new prefab, // First, determine which tilesets are actually used in this new prefab,
// based on the metatile ids. // based on the metatile ids.
bool usesPrimaryTileset = false; bool usesPrimaryTileset = false;
bool usesSecondaryTileset = false; bool usesSecondaryTileset = false;
for (auto metatile : selection.metatileItems) { for (auto metatile : selection->metatileItems) {
if (!metatile.enabled) if (!metatile.enabled)
continue; continue;
if (metatile.metatileId < Project::getNumMetatilesPrimary()) { if (metatile.metatileId < Project::getNumMetatilesPrimary()) {
@ -263,7 +263,7 @@ void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) {
name, name,
usesPrimaryTileset ? map->layout->tileset_primary_label : "", usesPrimaryTileset ? map->layout->tileset_primary_label : "",
usesSecondaryTileset ? map->layout->tileset_secondary_label: "", usesSecondaryTileset ? map->layout->tileset_secondary_label: "",
selection *selection
}); });
this->savePrefabs(); this->savePrefabs();
this->updatePrefabUi(map); this->updatePrefabUi(map);

View file

@ -23,17 +23,17 @@ PrefabCreationDialog::PrefabCreationDialog(QWidget *parent, MetatileSelector *me
QObject::connect(this->ui->graphicsView_Prefab, &ClickableGraphicsView::clicked, [=](QMouseEvent *event){ QObject::connect(this->ui->graphicsView_Prefab, &ClickableGraphicsView::clicked, [=](QMouseEvent *event){
auto pos = event->pos(); auto pos = event->pos();
int selectionWidth = this->selection.dimensions.x() * 16; int selectionWidth = this->selection->dimensions.x() * 16;
int selectionHeight = this->selection.dimensions.y() * 16; int selectionHeight = this->selection->dimensions.y() * 16;
if (pos.x() < 0 || pos.x() >= selectionWidth || pos.y() < 0 || pos.y() >= selectionHeight) if (pos.x() < 0 || pos.x() >= selectionWidth || pos.y() < 0 || pos.y() >= selectionHeight)
return; return;
int metatileX = pos.x() / 16; int metatileX = pos.x() / 16;
int metatileY = pos.y() / 16; int metatileY = pos.y() / 16;
int index = metatileY * this->selection.dimensions.x() + metatileX; int index = metatileY * this->selection->dimensions.x() + metatileX;
bool toggledState = !this->selection.metatileItems[index].enabled; bool toggledState = !this->selection->metatileItems[index].enabled;
this->selection.metatileItems[index].enabled = toggledState; this->selection->metatileItems[index].enabled = toggledState;
if (this->selection.hasCollision) { if (this->selection->hasCollision) {
this->selection.collisionItems[index].enabled = toggledState; this->selection->collisionItems[index].enabled = toggledState;
} }
pixmapItem->setPixmap(drawMetatileSelection(this->selection, map)); pixmapItem->setPixmap(drawMetatileSelection(this->selection, map));
}); });

View file

@ -5,7 +5,7 @@
#include <QPainter> #include <QPainter>
QPoint StampSelector::getSelectionDimensions() { QPoint StampSelector::getSelectionDimensions() {
return selection.dimensions; return selection->dimensions;
} }
void StampSelector::draw() { void StampSelector::draw() {
@ -43,9 +43,8 @@ bool StampSelector::select(uint16_t stampId) {
// TODO: // TODO:
if (stampId > 9999999999999) return false; if (stampId > 9999999999999) return false;
this->selection = StampSelection(); this->selection->dimensions = QPoint(1, 1);
this->selection.dimensions = QPoint(1, 1); this->selection->stampIds = QList<uint16_t>(stampId);
this->selection.stampIds = QList<uint16_t>(stampId);
QPoint coords = this->getStampIdCoords(stampId); QPoint coords = this->getStampIdCoords(stampId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
@ -60,7 +59,7 @@ void StampSelector::setTilesets(Tileset *primaryTileset, Tileset *secondaryTiles
this->draw(); this->draw();
} }
StampSelection StampSelector::getStampSelection() { StampSelection* StampSelector::getStampSelection() {
return selection; return selection;
} }
@ -90,16 +89,16 @@ void StampSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
} }
void StampSelector::updateSelectedStamps() { void StampSelector::updateSelectedStamps() {
this->selection.stampIds.clear(); this->selection->stampIds.clear();
this->selection.dimensions = SelectablePixmapItem::getSelectionDimensions(); this->selection->dimensions = SelectablePixmapItem::getSelectionDimensions();
QPoint origin = this->getSelectionStart(); QPoint origin = this->getSelectionStart();
for (int j = 0; j < this->selection.dimensions.y(); j++) { for (int j = 0; j < this->selection->dimensions.y(); j++) {
for (int i = 0; i < this->selection.dimensions.x(); i++) { for (int i = 0; i < this->selection->dimensions.x(); i++) {
uint16_t stampId = this->getStampId(origin.x() + i, origin.y() + j); uint16_t stampId = this->getStampId(origin.x() + i, origin.y() + j);
// TODO: check if stamp id is valid // TODO: check if stamp id is valid
if (false) if (false)
stampId = 0; stampId = 0;
this->selection.stampIds.append(stampId); this->selection->stampIds.append(stampId);
} }
} }
emit selectedStampsChanged(); emit selectedStampsChanged();