Add ability to select metatile chunks directly from the map area. Redraw collision tiles during undo/redo

This commit is contained in:
Marcus Huderle 2018-07-15 09:05:08 -05:00
parent 019a597c28
commit de9bfaa9bb
7 changed files with 382 additions and 82 deletions

View file

@ -31,6 +31,7 @@ void Editor::undo() {
if (current_view) { if (current_view) {
map->undo(); map->undo();
map_item->draw(); map_item->draw();
collision_item->draw();
} }
} }
@ -38,6 +39,7 @@ void Editor::redo() {
if (current_view) { if (current_view) {
map->redo(); map->redo();
map_item->draw(); map_item->draw();
collision_item->draw();
} }
} }
@ -324,13 +326,21 @@ void Editor::setMap(QString map_name) {
} }
if (project) { if (project) {
map = project->loadMap(map_name); map = project->loadMap(map_name);
connect(map, &Map::paintTileChanged, [=](Map *map) {
lastSelectedMetatilesFromMap = false;
redrawCurrentMetatilesSelection();
});
selected_events->clear(); selected_events->clear();
updateCurrentMetatilesSelection();
displayMap(); displayMap();
updateSelectedEvents(); updateSelectedEvents();
} }
} }
void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) { void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
if (event->buttons() & Qt::RightButton) {
item->updateMetatileSelection(event);
} else {
if (map_edit_mode == "paint") { if (map_edit_mode == "paint") {
item->paint(event); item->paint(event);
} else if (map_edit_mode == "fill") { } else if (map_edit_mode == "fill") {
@ -341,6 +351,7 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
item->select(event); item->select(event);
} }
} }
}
void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) { void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
if (map_edit_mode == "paint") { if (map_edit_mode == "paint") {
item->paint(event); item->paint(event);
@ -353,6 +364,15 @@ void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixm
} }
} }
void Editor::updateCurrentMetatilesSelection() {
if (lastSelectedMetatilesFromMap) {
// Remember the copied metatiles from the previously-opened map
map->selected_metatiles_width = this->copiedMetatileSelectionWidth;
map->selected_metatiles_height = this->copiedMetatileSelectionHeight;
*map->selected_metatiles = *this->copiedMetatileSelection;
}
}
void Editor::displayMap() { void Editor::displayMap() {
if (!scene) if (!scene)
scene = new QGraphicsScene; scene = new QGraphicsScene;
@ -361,7 +381,7 @@ void Editor::displayMap() {
scene->removeItem(map_item); scene->removeItem(map_item);
delete map_item; delete map_item;
} }
map_item = new MapPixmapItem(map); map_item = new MapPixmapItem(map, this);
connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)), connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*))); this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
@ -372,7 +392,7 @@ void Editor::displayMap() {
scene->removeItem(collision_item); scene->removeItem(collision_item);
delete collision_item; delete collision_item;
} }
collision_item = new CollisionPixmapItem(map); collision_item = new CollisionPixmapItem(map, this);
connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)), connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*))); this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
@ -390,6 +410,7 @@ void Editor::displayMap() {
displayMetatiles(); displayMetatiles();
displayBorderMetatiles(); displayBorderMetatiles();
displayCurrentMetatilesSelection();
displayCollisionMetatiles(); displayCollisionMetatiles();
displayElevationMetatiles(); displayElevationMetatiles();
displayMapEvents(); displayMapEvents();
@ -434,6 +455,25 @@ void Editor::displayBorderMetatiles() {
connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged())); connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged()));
} }
void Editor::displayCurrentMetatilesSelection() {
if (scene_current_metatile_selection_item && scene_current_metatile_selection_item->scene()) {
scene_current_metatile_selection_item->scene()->removeItem(scene_current_metatile_selection_item);
delete scene_current_metatile_selection_item;
}
scene_current_metatile_selection = new QGraphicsScene;
scene_current_metatile_selection_item = new CurrentSelectedMetatilesPixmapItem(map);
scene_current_metatile_selection_item->draw();
scene_current_metatile_selection->addItem(scene_current_metatile_selection_item);
}
void Editor::redrawCurrentMetatilesSelection() {
if (scene_current_metatile_selection_item) {
scene_current_metatile_selection_item->draw();
emit currentMetatilesSelectionChanged();
}
}
void Editor::displayCollisionMetatiles() { void Editor::displayCollisionMetatiles() {
if (collision_metatiles_item && collision_metatiles_item->scene()) { if (collision_metatiles_item && collision_metatiles_item->scene()) {
collision_metatiles_item->scene()->removeItem(collision_metatiles_item); collision_metatiles_item->scene()->removeItem(collision_metatiles_item);
@ -863,35 +903,28 @@ void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
int y = ((int)pos.y()) / 16; int y = ((int)pos.y()) / 16;
map->paint_metatile_initial_x = x; map->paint_metatile_initial_x = x;
map->paint_metatile_initial_y = y; map->paint_metatile_initial_y = y;
updateSelection(event->pos(), event->button()); updateSelection(event->pos());
} }
void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
updateCurHoveredMetatile(event->pos()); updateCurHoveredMetatile(event->pos());
Qt::MouseButton button = event->button(); updateSelection(event->pos());
if (button == Qt::MouseButton::NoButton) {
Qt::MouseButtons heldButtons = event->buttons();
if (heldButtons & Qt::RightButton) {
button = Qt::RightButton;
} else if (heldButtons & Qt::LeftButton) {
button = Qt::LeftButton;
}
}
updateSelection(event->pos(), button);
} }
void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
updateSelection(event->pos(), event->button()); updateSelection(event->pos());
} }
void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) { void MetatilesPixmapItem::updateSelection(QPointF pos) {
int x = ((int)pos.x()) / 16; int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16; int y = ((int)pos.y()) / 16;
int width = pixmap().width() / 16; int width = pixmap().width() / 16;
int height = pixmap().height() / 16; int height = pixmap().height() / 16;
if ((x >= 0 && x < width) && (y >=0 && y < height)) { if ((x >= 0 && x < width) && (y >=0 && y < height)) {
int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x; int x1 = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y; int y1 = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
map->paint_tile_index = baseTileY * 8 + baseTileX; map->paint_tile_index = y1 * 8 + x1;
map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1; map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1; map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
map->setSelectedMetatilesFromTilePicker();
emit map->paintTileChanged(map); emit map->paintTileChanged(map);
} }
} }
@ -901,10 +934,10 @@ void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
int x = ((int)pos.x()) / 16; int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16; int y = ((int)pos.y()) / 16;
for (int i = 0; i < map->paint_tile_width && (i + x) < 2; i++) { for (int i = 0; i < map->selected_metatiles_width && (i + x) < 2; i++) {
for (int j = 0; j < map->paint_tile_height && (j + y) < 2; j++) { for (int j = 0; j < map->selected_metatiles_height && (j + y) < 2; j++) {
int blockIndex = (j + y) * 2 + (i + x); int blockIndex = (j + y) * 2 + (i + x);
int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8)); int tile = map->selected_metatiles->at(j * map->selected_metatiles_width + i);
(*map->layout->border->blocks)[blockIndex].tile = tile; (*map->layout->border->blocks)[blockIndex].tile = tile;
} }
} }
@ -933,6 +966,27 @@ void BorderMetatilesPixmapItem::draw() {
setPixmap(QPixmap::fromImage(image)); setPixmap(QPixmap::fromImage(image));
} }
void CurrentSelectedMetatilesPixmapItem::draw() {
int width = map->selected_metatiles_width * 16;
int height = map->selected_metatiles_height * 16;
QImage image(width, height, QImage::Format_RGBA8888);
QPainter painter(&image);
for (int i = 0; i < map->selected_metatiles_width; i++)
for (int j = 0; j < map->selected_metatiles_height; j++)
{
int x = i * 16;
int y = j * 16;
int index = j * map->selected_metatiles_width + i;
QImage metatile_image = Metatile::getMetatileImage(map->selected_metatiles->at(index), map->layout->tileset_primary, map->layout->tileset_secondary);
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}
painter.end();
setPixmap(QPixmap::fromImage(image));
}
void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) { void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
QPointF pos = event->pos(); QPointF pos = event->pos();
int x = ((int)pos.x()) / 16; int x = ((int)pos.x()) / 16;
@ -1039,7 +1093,8 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
int x = (int)(pos.x()) / 16; int x = (int)(pos.x()) / 16;
int y = (int)(pos.y()) / 16; int y = (int)(pos.y()) / 16;
if (map->smart_paths_enabled && map->paint_tile_width == 3 && map->paint_tile_height == 3) { // Paint onto the map.
if (map->smart_paths_enabled && map->selected_metatiles_width == 3 && map->selected_metatiles_height == 3) {
paintSmartPath(x, y); paintSmartPath(x, y);
} else { } else {
paintNormal(x, y); paintNormal(x, y);
@ -1048,6 +1103,7 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) { if (event->type() == QEvent::GraphicsSceneMouseRelease) {
map->commit(); map->commit();
} }
draw(); draw();
} }
} }
@ -1057,18 +1113,18 @@ void MapPixmapItem::paintNormal(int x, int y) {
// This allows painting via dragging the mouse to tile the painted region. // This allows painting via dragging the mouse to tile the painted region.
int xDiff = x - map->paint_tile_initial_x; int xDiff = x - map->paint_tile_initial_x;
int yDiff = y - map->paint_tile_initial_y; int yDiff = y - map->paint_tile_initial_y;
if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width; if (xDiff < 0 && xDiff % map->selected_metatiles_width != 0) xDiff -= map->selected_metatiles_width;
if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height; if (yDiff < 0 && yDiff % map->selected_metatiles_height != 0) yDiff -= map->selected_metatiles_height;
x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width; x = map->paint_tile_initial_x + (xDiff / map->selected_metatiles_width) * map->selected_metatiles_width;
y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height; y = map->paint_tile_initial_y + (yDiff / map->selected_metatiles_height) * map->selected_metatiles_height;
for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++) for (int i = 0; i < map->selected_metatiles_width && i + x < map->getWidth(); i++)
for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) { for (int j = 0; j < map->selected_metatiles_height && j + y < map->getHeight(); j++) {
int actualX = i + x; int actualX = i + x;
int actualY = j + y; int actualY = j + y;
Block *block = map->getBlock(actualX, actualY); Block *block = map->getBlock(actualX, actualY);
if (block) { if (block) {
block->tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8)); block->tile = map->selected_metatiles->at(j * map->selected_metatiles_width + i);
map->_setBlock(actualX, actualY, *block); map->_setBlock(actualX, actualY, *block);
} }
} }
@ -1078,34 +1134,32 @@ void MapPixmapItem::paintNormal(int x, int y) {
// Each entry is for one possibility from the marching squares value for a tile. // Each entry is for one possibility from the marching squares value for a tile.
// (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares) // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
QList<int> MapPixmapItem::smartPathTable = QList<int>({ QList<int> MapPixmapItem::smartPathTable = QList<int>({
8 + 1, // 0000 4, // 0000
8 + 1, // 0001 4, // 0001
8 + 1, // 0010 4, // 0010
16 + 0, // 0011 6, // 0011
8 + 1, // 0100 4, // 0100
8 + 1, // 0101 4, // 0101
0 + 0, // 0110 0, // 0110
8 + 0, // 0111 3, // 0111
8 + 1, // 1000 4, // 1000
16 + 2, // 1001 8, // 1001
8 + 1, // 1010 4, // 1010
16 + 1, // 1011 7, // 1011
0 + 2, // 1100 2, // 1100
8 + 2, // 1101 5, // 1101
0 + 1, // 1110 1, // 1110
8 + 1, // 1111 4, // 1111
}); });
#define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \ #define IS_SMART_PATH_TILE(block) (map->selected_metatiles->contains(block->tile))
|| (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
|| (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 19))
void MapPixmapItem::paintSmartPath(int x, int y) { void MapPixmapItem::paintSmartPath(int x, int y) {
// Smart path should never be enabled without a 3x3 block selection. // Smart path should never be enabled without a 3x3 block selection.
if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return; if (map->selected_metatiles_width != 3 || map->selected_metatiles_height != 3) return;
// Shift to the middle tile of the smart path selection. // Shift to the middle tile of the smart path selection.
int openTile = map->paint_tile_index + 8 + 1; int openTile = map->selected_metatiles->at(4);
// Fill the region with the open tile. // Fill the region with the open tile.
for (int i = -1; i <= 1; i++) for (int i = -1; i <= 1; i++)
@ -1117,7 +1171,7 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
int actualY = j + y; int actualY = j + y;
Block *block = map->getBlock(actualX, actualY); Block *block = map->getBlock(actualX, actualY);
if (block) { if (block) {
block->tile = map->getSelectedBlockIndex(openTile); block->tile = openTile;
map->_setBlock(actualX, actualY, *block); map->_setBlock(actualX, actualY, *block);
} }
} }
@ -1157,20 +1211,70 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
if (left && IS_SMART_PATH_TILE(left)) if (left && IS_SMART_PATH_TILE(left))
id += 8; id += 8;
block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]); block->tile = map->selected_metatiles->at(smartPathTable[id]);
map->_setBlock(actualX, actualY, *block); map->_setBlock(actualX, actualY, *block);
} }
} }
void MapPixmapItem::updateMetatileSelection(QGraphicsSceneMouseEvent *event) {
QPointF pos = event->pos();
int x = (int)(pos.x()) / 16;
int y = (int)(pos.y()) / 16;
// Snap point to within map bounds.
if (x < 0) x = 0;
if (x >= map->getWidth()) x = map->getWidth() - 1;
if (y < 0) y = 0;
if (y >= map->getHeight()) y = map->getHeight() - 1;
// Update/apply copied metatiles.
if (event->type() == QEvent::GraphicsSceneMousePress) {
selection_origin = QPoint(x, y);
selection.clear();
selection.append(QPoint(x, y));
editor->copiedMetatileSelectionWidth = 1;
editor->copiedMetatileSelectionHeight = 1;
} else if (event->type() == QEvent::GraphicsSceneMouseMove) {
QPoint pos = QPoint(x, y);
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));
}
}
editor->copiedMetatileSelectionWidth = x2 - x1 + 1;
editor->copiedMetatileSelectionHeight = y2 - y1 + 1;
}
editor->copiedMetatileSelection->clear();
for (QPoint point : selection) {
editor->copiedMetatileSelection->append(map->getBlock(point.x(), point.y())->tile);
}
editor->lastSelectedMetatilesFromMap = true;
map->selected_metatiles_width = editor->copiedMetatileSelectionWidth;
map->selected_metatiles_height = editor->copiedMetatileSelectionHeight;
*map->selected_metatiles = *editor->copiedMetatileSelection;
editor->redrawCurrentMetatilesSelection();
}
void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) { void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
if (map) { if (map) {
QPointF pos = event->pos(); QPointF pos = event->pos();
int x = (int)(pos.x()) / 16; int x = (int)(pos.x()) / 16;
int y = (int)(pos.y()) / 16; int y = (int)(pos.y()) / 16;
Block *block = map->getBlock(x, y); Block *block = map->getBlock(x, y);
int tile = map->getSelectedBlockIndex(map->paint_tile_index); int tile = map->selected_metatiles->first();
if (block && block->tile != tile) { if (block && block->tile != tile) {
if (map->smart_paths_enabled && map->paint_tile_width == 3 && map->paint_tile_height == 3) if (map->smart_paths_enabled && map->selected_metatiles_width == 3 && map->selected_metatiles_height == 3)
this->_floodFillSmartPath(x, y); this->_floodFillSmartPath(x, y);
else else
this->_floodFill(x, y); this->_floodFill(x, y);
@ -1198,11 +1302,11 @@ void MapPixmapItem::_floodFill(int initialX, int initialY) {
int xDiff = x - initialX; int xDiff = x - initialX;
int yDiff = y - initialY; int yDiff = y - initialY;
int i = xDiff % map->paint_tile_width; int i = xDiff % map->selected_metatiles_width;
int j = yDiff % map->paint_tile_height; int j = yDiff % map->selected_metatiles_height;
if (i < 0) i = map->paint_tile_width + i; if (i < 0) i = map->selected_metatiles_width + i;
if (j < 0) j = map->paint_tile_height + j; if (j < 0) j = map->selected_metatiles_height + j;
int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8)); int tile = map->selected_metatiles->at(j * map->selected_metatiles_width + i);
uint old_tile = block->tile; uint old_tile = block->tile;
if (old_tile == tile) { if (old_tile == tile) {
continue; continue;
@ -1227,10 +1331,10 @@ void MapPixmapItem::_floodFill(int initialX, int initialY) {
void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) { void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
// Smart path should never be enabled without a 3x3 block selection. // Smart path should never be enabled without a 3x3 block selection.
if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return; if (map->selected_metatiles_width != 3 || map->selected_metatiles_height != 3) return;
// Shift to the middle tile of the smart path selection. // Shift to the middle tile of the smart path selection.
int openTile = map->paint_tile_index + 8 + 1; int openTile = map->selected_metatiles->at(4);
// Flood fill the region with the open tile. // Flood fill the region with the open tile.
QList<QPoint> todo; QList<QPoint> todo;
@ -1245,13 +1349,12 @@ void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
continue; continue;
} }
int tile = map->getSelectedBlockIndex(openTile);
uint old_tile = block->tile; uint old_tile = block->tile;
if (old_tile == tile) { if (old_tile == openTile) {
continue; continue;
} }
block->tile = tile; block->tile = openTile;
map->_setBlock(x, y, *block); map->_setBlock(x, y, *block);
if ((block = map->getBlock(x + 1, y)) && block->tile == old_tile) { if ((block = map->getBlock(x + 1, y)) && block->tile == old_tile) {
todo.append(QPoint(x + 1, y)); todo.append(QPoint(x + 1, y));
@ -1301,7 +1404,7 @@ void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
if (left && IS_SMART_PATH_TILE(left)) if (left && IS_SMART_PATH_TILE(left))
id += 8; id += 8;
block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]); block->tile = map->selected_metatiles->at(smartPathTable[id]);
map->_setBlock(x, y, *block); map->_setBlock(x, y, *block);
// 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.
@ -1333,12 +1436,12 @@ void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
map->paint_tile_index = map->getDisplayedBlockIndex(block->tile); map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
map->paint_tile_width = 1; map->paint_tile_width = 1;
map->paint_tile_height = 1; map->paint_tile_height = 1;
map->setSelectedMetatilesFromTilePicker();
emit map->paintTileChanged(map); emit map->paintTileChanged(map);
} }
} }
#define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) { void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
QPointF pos = event->pos(); QPointF pos = event->pos();
int x = (int)(pos.x()) / 16; int x = (int)(pos.x()) / 16;

View file

@ -17,9 +17,12 @@ class CollisionPixmapItem;
class ConnectionPixmapItem; class ConnectionPixmapItem;
class MetatilesPixmapItem; class MetatilesPixmapItem;
class BorderMetatilesPixmapItem; class BorderMetatilesPixmapItem;
class CurrentSelectedMetatilesPixmapItem;
class CollisionMetatilesPixmapItem; class CollisionMetatilesPixmapItem;
class ElevationMetatilesPixmapItem; class ElevationMetatilesPixmapItem;
#define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
class Editor : public QObject class Editor : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -35,9 +38,12 @@ public:
void undo(); void undo();
void redo(); void redo();
void setMap(QString map_name); void setMap(QString map_name);
void updateCurrentMetatilesSelection();
void displayMap(); void displayMap();
void displayMetatiles(); void displayMetatiles();
void displayBorderMetatiles(); void displayBorderMetatiles();
void displayCurrentMetatilesSelection();
void redrawCurrentMetatilesSelection();
void displayCollisionMetatiles(); void displayCollisionMetatiles();
void displayElevationMetatiles(); void displayElevationMetatiles();
void displayMapEvents(); void displayMapEvents();
@ -84,17 +90,25 @@ public:
QList<QGraphicsLineItem*> gridLines; QList<QGraphicsLineItem*> gridLines;
QGraphicsScene *scene_metatiles = NULL; QGraphicsScene *scene_metatiles = NULL;
QGraphicsScene *scene_current_metatile_selection = NULL;
QGraphicsScene *scene_selected_border_metatiles = NULL; QGraphicsScene *scene_selected_border_metatiles = NULL;
QGraphicsScene *scene_collision_metatiles = NULL; QGraphicsScene *scene_collision_metatiles = NULL;
QGraphicsScene *scene_elevation_metatiles = NULL; QGraphicsScene *scene_elevation_metatiles = NULL;
MetatilesPixmapItem *metatiles_item = NULL; MetatilesPixmapItem *metatiles_item = NULL;
BorderMetatilesPixmapItem *selected_border_metatiles_item = NULL; BorderMetatilesPixmapItem *selected_border_metatiles_item = NULL;
CurrentSelectedMetatilesPixmapItem *scene_current_metatile_selection_item = NULL;
CollisionMetatilesPixmapItem *collision_metatiles_item = NULL; CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL; ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
QList<DraggablePixmapItem*> *events = NULL; QList<DraggablePixmapItem*> *events = NULL;
QList<DraggablePixmapItem*> *selected_events = NULL; QList<DraggablePixmapItem*> *selected_events = NULL;
bool lastSelectedMetatilesFromMap = false;
int copiedMetatileSelectionWidth = 0;
int copiedMetatileSelectionHeight = 0;
QList<int> *copiedMetatileSelection = new QList<int>;
QString map_edit_mode; QString map_edit_mode;
void objectsView_onMousePress(QMouseEvent *event); void objectsView_onMousePress(QMouseEvent *event);
@ -139,6 +153,7 @@ signals:
void loadMapRequested(QString, QString); void loadMapRequested(QString, QString);
void tilesetChanged(QString); void tilesetChanged(QString);
void warpEventDoubleClicked(QString mapName, QString warpNum); void warpEventDoubleClicked(QString mapName, QString warpNum);
void currentMetatilesSelectionChanged();
}; };
@ -241,8 +256,10 @@ public:
MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) { MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
} }
Map *map = NULL; Map *map = NULL;
MapPixmapItem(Map *map_) { Editor *editor = NULL;
MapPixmapItem(Map *map_, Editor *editor_) {
map = map_; map = map_;
editor = editor_;
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
bool active; bool active;
@ -256,6 +273,7 @@ public:
virtual void pick(QGraphicsSceneMouseEvent*); virtual void pick(QGraphicsSceneMouseEvent*);
virtual void select(QGraphicsSceneMouseEvent*); virtual void select(QGraphicsSceneMouseEvent*);
virtual void draw(bool ignoreCache = false); virtual void draw(bool ignoreCache = false);
void updateMetatileSelection(QGraphicsSceneMouseEvent *event);
private: private:
void updateCurHoveredTile(QPointF pos); void updateCurHoveredTile(QPointF pos);
@ -279,7 +297,7 @@ class CollisionPixmapItem : public MapPixmapItem {
public: public:
CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) { CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
} }
CollisionPixmapItem(Map *map_): MapPixmapItem(map_) { CollisionPixmapItem(Map *map_, Editor *editor_): MapPixmapItem(map_, editor_) {
} }
virtual void paint(QGraphicsSceneMouseEvent*); virtual void paint(QGraphicsSceneMouseEvent*);
virtual void floodFill(QGraphicsSceneMouseEvent*); virtual void floodFill(QGraphicsSceneMouseEvent*);
@ -349,7 +367,7 @@ public:
Map* map = NULL; Map* map = NULL;
virtual void draw(); virtual void draw();
private: private:
void updateSelection(QPointF pos, Qt::MouseButton button); void updateSelection(QPointF pos);
protected: protected:
virtual void updateCurHoveredMetatile(QPointF pos); virtual void updateCurHoveredMetatile(QPointF pos);
private slots: private slots:
@ -377,6 +395,16 @@ protected:
void mousePressEvent(QGraphicsSceneMouseEvent*); void mousePressEvent(QGraphicsSceneMouseEvent*);
}; };
class CurrentSelectedMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
CurrentSelectedMetatilesPixmapItem(Map *map_) {
map = map_;
}
Map* map = NULL;
virtual void draw();
};
class MovementPermissionsPixmapItem : public MetatilesPixmapItem { class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
Q_OBJECT Q_OBJECT
public: public:

View file

@ -38,6 +38,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(editor, SIGNAL(loadMapRequested(QString, QString)), this, SLOT(onLoadMapRequested(QString, QString))); connect(editor, SIGNAL(loadMapRequested(QString, QString)), this, SLOT(onLoadMapRequested(QString, QString)));
connect(editor, SIGNAL(tilesetChanged(QString)), this, SLOT(onTilesetChanged(QString))); connect(editor, SIGNAL(tilesetChanged(QString)), this, SLOT(onTilesetChanged(QString)));
connect(editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString))); connect(editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString)));
connect(editor, SIGNAL(currentMetatilesSelectionChanged()), this, SLOT(currentMetatilesSelectionChanged()));
on_toolButton_Paint_clicked(); on_toolButton_Paint_clicked();
@ -188,6 +189,9 @@ void MainWindow::redrawMapScene()
ui->graphicsView_BorderMetatile->setScene(editor->scene_selected_border_metatiles); ui->graphicsView_BorderMetatile->setScene(editor->scene_selected_border_metatiles);
ui->graphicsView_BorderMetatile->setFixedSize(editor->selected_border_metatiles_item->pixmap().width() + 2, editor->selected_border_metatiles_item->pixmap().height() + 2); ui->graphicsView_BorderMetatile->setFixedSize(editor->selected_border_metatiles_item->pixmap().width() + 2, editor->selected_border_metatiles_item->pixmap().height() + 2);
ui->graphicsView_currentMetatileSelection->setScene(editor->scene_current_metatile_selection);
ui->graphicsView_currentMetatileSelection->setFixedSize(editor->scene_current_metatile_selection_item->pixmap().width() + 2, editor->scene_current_metatile_selection_item->pixmap().height() + 2);
ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles); ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles);
//ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect()); //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect());
ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2); ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2);
@ -485,6 +489,12 @@ void MainWindow::onTilesetChanged(QString mapName)
setMap(mapName); setMap(mapName);
} }
void MainWindow::currentMetatilesSelectionChanged()
{
ui->graphicsView_currentMetatileSelection->setFixedSize(editor->scene_current_metatile_selection_item->pixmap().width() + 2, editor->scene_current_metatile_selection_item->pixmap().height() + 2);
ui->graphicsView_currentMetatileSelection->setSceneRect(0, 0, editor->scene_current_metatile_selection_item->pixmap().width(), editor->scene_current_metatile_selection_item->pixmap().height());
}
void MainWindow::on_mapList_activated(const QModelIndex &index) void MainWindow::on_mapList_activated(const QModelIndex &index)
{ {
QVariant data = index.data(Qt::UserRole); QVariant data = index.data(Qt::UserRole);

View file

@ -74,6 +74,7 @@ private slots:
void onOpenMapListContextMenu(const QPoint &point); void onOpenMapListContextMenu(const QPoint &point);
void onAddNewMapToGroupClick(QAction* triggeredAction); void onAddNewMapToGroupClick(QAction* triggeredAction);
void onTilesetChanged(QString); void onTilesetChanged(QString);
void currentMetatilesSelectionChanged();
void on_action_Export_Map_Image_triggered(); void on_action_Export_Map_Image_triggered();

View file

@ -171,7 +171,7 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="toolTip"> <property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Pencil&lt;/p&gt;&lt;p&gt;Click and drag to draw on the map.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Editor&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Click&lt;/span&gt; and drag to draw on the map.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Right-click&lt;/span&gt; and drag to select tiles.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="text"> <property name="text">
<string>Paint</string> <string>Paint</string>
@ -334,7 +334,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>436</width> <width>429</width>
<height>620</height> <height>620</height>
</rect> </rect>
</property> </property>
@ -647,6 +647,143 @@
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QFrame" name="frame_currentMetatileSelection">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>92</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>92</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="spacing">
<number>6</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item>
<widget class="QLabel" name="label_10">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Selection</string>
</property>
</widget>
</item>
<item>
<widget class="QScrollArea" name="scrollArea_6">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_6">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>315</width>
<height>86</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer_16">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QGraphicsView" name="graphicsView_currentMetatileSelection">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="interactive">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_17">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QScrollArea" name="scrollArea_2"> <widget class="QScrollArea" name="scrollArea_2">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
@ -677,8 +814,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>358</width> <width>365</width>
<height>497</height> <height>405</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">

17
map.cpp
View file

@ -12,6 +12,10 @@ Map::Map(QObject *parent) : QObject(parent)
paint_tile_index = 1; paint_tile_index = 1;
paint_collision = 0; paint_collision = 0;
paint_elevation = 3; paint_elevation = 3;
selected_metatiles_width = 1;
selected_metatiles_height = 1;
selected_metatiles = new QList<int>;
selected_metatiles->append(1);
} }
void Map::setName(QString mapName) { void Map::setName(QString mapName) {
@ -715,3 +719,16 @@ void Map::hoveredElevationTileChanged(int elevation) {
void Map::clearHoveredElevationTile() { void Map::clearHoveredElevationTile() {
emit statusBarMessage(QString("")); emit statusBarMessage(QString(""));
} }
void Map::setSelectedMetatilesFromTilePicker() {
this->selected_metatiles_width = this->paint_tile_width;
this->selected_metatiles_height = this->paint_tile_height;
this->selected_metatiles->clear();
for (int j = 0; j < this->paint_tile_height; j++) {
for (int i = 0; i < this->paint_tile_width; i++) {
int metatile = this->getSelectedBlockIndex(this->paint_tile_index + i + (j * 8));
this->selected_metatiles->append(metatile);
}
}
}

4
map.h
View file

@ -177,6 +177,9 @@ public:
int paint_tile_height = 1; int paint_tile_height = 1;
int paint_tile_initial_x; int paint_tile_initial_x;
int paint_tile_initial_y; int paint_tile_initial_y;
int selected_metatiles_width;
int selected_metatiles_height;
QList<int> *selected_metatiles = NULL;
int paint_collision; int paint_collision;
int paint_elevation; int paint_elevation;
@ -218,6 +221,7 @@ public:
void clearHoveredCollisionTile(); void clearHoveredCollisionTile();
void hoveredElevationTileChanged(int elevation); void hoveredElevationTileChanged(int elevation);
void clearHoveredElevationTile(); void clearHoveredElevationTile();
void setSelectedMetatilesFromTilePicker();
signals: signals:
void paintTileChanged(Map *map); void paintTileChanged(Map *map);