Merge pull request #61 from huderlem/master

Efficient map editing
This commit is contained in:
yenatch 2018-07-21 13:39:37 -04:00 committed by GitHub
commit 6f3c5c25dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 594 additions and 411 deletions

View file

@ -11,6 +11,9 @@ class Blockdata : public QObject
Q_OBJECT
public:
explicit Blockdata(QObject *parent = 0);
~Blockdata() {
if (blocks) delete blocks;
}
public:
QList<Block> *blocks = NULL;

View file

@ -31,6 +31,7 @@ void Editor::undo() {
if (current_view) {
map->undo();
map_item->draw();
collision_item->draw();
}
}
@ -38,6 +39,7 @@ void Editor::redo() {
if (current_view) {
map->redo();
map_item->draw();
collision_item->draw();
}
}
@ -324,13 +326,21 @@ void Editor::setMap(QString map_name) {
}
if (project) {
map = project->loadMap(map_name);
connect(map, &Map::paintTileChanged, [=](Map *map) {
lastSelectedMetatilesFromMap = false;
redrawCurrentMetatilesSelection();
});
selected_events->clear();
updateCurrentMetatilesSelection();
displayMap();
updateSelectedEvents();
}
}
void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
if (event->buttons() & Qt::RightButton) {
item->updateMetatileSelection(event);
} else {
if (map_edit_mode == "paint") {
item->paint(event);
} else if (map_edit_mode == "fill") {
@ -341,7 +351,11 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
item->select(event);
}
}
}
void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
if (event->buttons() & Qt::RightButton) {
item->updateMovementPermissionSelection(event);
} else {
if (map_edit_mode == "paint") {
item->paint(event);
} else if (map_edit_mode == "fill") {
@ -352,6 +366,16 @@ void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixm
item->select(event);
}
}
}
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() {
if (!scene)
@ -361,7 +385,7 @@ void Editor::displayMap() {
scene->removeItem(map_item);
delete map_item;
}
map_item = new MapPixmapItem(map);
map_item = new MapPixmapItem(map, this);
connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
@ -372,7 +396,7 @@ void Editor::displayMap() {
scene->removeItem(collision_item);
delete collision_item;
}
collision_item = new CollisionPixmapItem(map);
collision_item = new CollisionPixmapItem(map, this);
connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
@ -390,8 +414,8 @@ void Editor::displayMap() {
displayMetatiles();
displayBorderMetatiles();
displayCurrentMetatilesSelection();
displayCollisionMetatiles();
displayElevationMetatiles();
displayMapEvents();
displayMapConnections();
displayMapBorder();
@ -434,6 +458,25 @@ void Editor::displayBorderMetatiles() {
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() {
if (collision_metatiles_item && collision_metatiles_item->scene()) {
collision_metatiles_item->scene()->removeItem(collision_metatiles_item);
@ -441,23 +484,11 @@ void Editor::displayCollisionMetatiles() {
}
scene_collision_metatiles = new QGraphicsScene;
collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
collision_metatiles_item = new MovementPermissionsPixmapItem(map);
collision_metatiles_item->draw();
scene_collision_metatiles->addItem(collision_metatiles_item);
}
void Editor::displayElevationMetatiles() {
if (elevation_metatiles_item && elevation_metatiles_item->scene()) {
elevation_metatiles_item->scene()->removeItem(elevation_metatiles_item);
delete elevation_metatiles_item;
}
scene_elevation_metatiles = new QGraphicsScene;
elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
elevation_metatiles_item->draw();
scene_elevation_metatiles->addItem(elevation_metatiles_item);
}
void Editor::displayMapEvents() {
if (events_group) {
for (QGraphicsItem *child : events_group->childItems()) {
@ -863,35 +894,28 @@ void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
int y = ((int)pos.y()) / 16;
map->paint_metatile_initial_x = x;
map->paint_metatile_initial_y = y;
updateSelection(event->pos(), event->button());
updateSelection(event->pos());
}
void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
updateCurHoveredMetatile(event->pos());
Qt::MouseButton button = event->button();
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);
updateSelection(event->pos());
}
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 y = ((int)pos.y()) / 16;
int width = pixmap().width() / 16;
int height = pixmap().height() / 16;
if ((x >= 0 && x < width) && (y >=0 && y < height)) {
int baseTileX = 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;
map->paint_tile_index = baseTileY * 8 + baseTileX;
int x1 = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
int y1 = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
map->paint_tile_index = y1 * 8 + x1;
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->setSelectedMetatilesFromTilePicker();
emit map->paintTileChanged(map);
}
}
@ -901,10 +925,10 @@ void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16;
for (int i = 0; i < map->paint_tile_width && (i + x) < 2; i++) {
for (int j = 0; j < map->paint_tile_height && (j + y) < 2; j++) {
for (int i = 0; i < map->selected_metatiles_width && (i + x) < 2; i++) {
for (int j = 0; j < map->selected_metatiles_height && (j + y) < 2; j++) {
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;
}
}
@ -933,15 +957,40 @@ void BorderMetatilesPixmapItem::draw() {
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) {
QPointF pos = event->pos();
int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16;
int width = pixmap().width() / 16;
int height = pixmap().height() / 16;
if ((x >= 0 && x < width) && (y >=0 && y < height)) {
pick(y * width + x);
}
int x = ((int)pos.x()) / 32;
int y = ((int)pos.y()) / 32;
int width = pixmap().width() / 32;
int height = pixmap().height() / 32;
// Snap to a valid position inside the selection area.
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height - 1;
pick(x, y);
}
void MovementPermissionsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
updateCurHoveredMetatile(event->pos());
@ -951,17 +1000,18 @@ void MovementPermissionsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent*
mousePressEvent(event);
}
void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16;
int width = pixmap().width() / 16;
int height = pixmap().height() / 16;
if (x < 0 || x >= width || y < 0 || y >= height) {
map->clearHoveredCollisionTile();
} else {
int collision = y * width + x;
map->hoveredCollisionTileChanged(collision);
}
void MovementPermissionsPixmapItem::updateCurHoveredMetatile(QPointF pos) {
int x = ((int)pos.x()) / 32;
int y = ((int)pos.y()) / 32;
int width = pixmap().width() / 32;
int height = pixmap().height() / 32;
// Snap to a valid position inside the selection area.
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height - 1;
map->hoveredMovementPermissionTileChanged(x, y);
}
int ConnectionPixmapItem::getMinOffset() {
@ -1020,34 +1070,23 @@ void ConnectionPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) {
emit connectionItemDoubleClicked(this);
}
void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16;
int width = pixmap().width() / 16;
int height = pixmap().height() / 16;
if (x < 0 || x >= width || y < 0 || y >= height) {
map->clearHoveredElevationTile();
} else {
int elevation = y * width + x;
map->hoveredElevationTileChanged(elevation);
}
}
void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
map->commit();
} else {
QPointF pos = event->pos();
int x = (int)(pos.x()) / 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);
} else {
paintNormal(x, y);
}
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
map->commit();
}
draw();
}
}
@ -1057,18 +1096,18 @@ void MapPixmapItem::paintNormal(int x, int y) {
// This allows painting via dragging the mouse to tile the painted region.
int xDiff = x - map->paint_tile_initial_x;
int yDiff = y - map->paint_tile_initial_y;
if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
if (xDiff < 0 && xDiff % map->selected_metatiles_width != 0) xDiff -= map->selected_metatiles_width;
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;
y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
x = map->paint_tile_initial_x + (xDiff / map->selected_metatiles_width) * map->selected_metatiles_width;
y = map->paint_tile_initial_y + (yDiff / map->selected_metatiles_height) * map->selected_metatiles_height;
for (int i = 0; i < map->selected_metatiles_width && i + x < map->getWidth(); i++)
for (int j = 0; j < map->selected_metatiles_height && j + y < map->getHeight(); j++) {
int actualX = i + x;
int actualY = j + y;
Block *block = map->getBlock(actualX, actualY);
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);
}
}
@ -1078,34 +1117,32 @@ void MapPixmapItem::paintNormal(int x, int y) {
// 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>({
8 + 1, // 0000
8 + 1, // 0001
8 + 1, // 0010
16 + 0, // 0011
8 + 1, // 0100
8 + 1, // 0101
0 + 0, // 0110
8 + 0, // 0111
8 + 1, // 1000
16 + 2, // 1001
8 + 1, // 1010
16 + 1, // 1011
0 + 2, // 1100
8 + 2, // 1101
0 + 1, // 1110
8 + 1, // 1111
4, // 0000
4, // 0001
4, // 0010
6, // 0011
4, // 0100
4, // 0101
0, // 0110
3, // 0111
4, // 1000
8, // 1001
4, // 1010
7, // 1011
2, // 1100
5, // 1101
1, // 1110
4, // 1111
});
#define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
|| (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))
#define IS_SMART_PATH_TILE(block) (map->selected_metatiles->contains(block->tile))
void MapPixmapItem::paintSmartPath(int x, int y) {
// 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.
int openTile = map->paint_tile_index + 8 + 1;
int openTile = map->selected_metatiles->at(4);
// Fill the region with the open tile.
for (int i = -1; i <= 1; i++)
@ -1117,7 +1154,7 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
int actualY = j + y;
Block *block = map->getBlock(actualX, actualY);
if (block) {
block->tile = map->getSelectedBlockIndex(openTile);
block->tile = openTile;
map->_setBlock(actualX, actualY, *block);
}
}
@ -1157,28 +1194,79 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
if (left && IS_SMART_PATH_TILE(left))
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);
}
}
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) {
if (map) {
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
map->commit();
} else {
QPointF pos = event->pos();
int x = (int)(pos.x()) / 16;
int y = (int)(pos.y()) / 16;
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 (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);
else
this->_floodFill(x, y);
}
if (event->type() == QEvent::GraphicsSceneMouseRelease) {
map->commit();
}
draw();
}
}
@ -1198,11 +1286,11 @@ void MapPixmapItem::_floodFill(int initialX, int initialY) {
int xDiff = x - initialX;
int yDiff = y - initialY;
int i = xDiff % map->paint_tile_width;
int j = yDiff % map->paint_tile_height;
if (i < 0) i = map->paint_tile_width + i;
if (j < 0) j = map->paint_tile_height + j;
int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
int i = xDiff % map->selected_metatiles_width;
int j = yDiff % map->selected_metatiles_height;
if (i < 0) i = map->selected_metatiles_width + i;
if (j < 0) j = map->selected_metatiles_height + j;
int tile = map->selected_metatiles->at(j * map->selected_metatiles_width + i);
uint old_tile = block->tile;
if (old_tile == tile) {
continue;
@ -1227,10 +1315,10 @@ void MapPixmapItem::_floodFill(int initialX, int initialY) {
void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
// 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.
int openTile = map->paint_tile_index + 8 + 1;
int openTile = map->selected_metatiles->at(4);
// Flood fill the region with the open tile.
QList<QPoint> todo;
@ -1245,13 +1333,12 @@ void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
continue;
}
int tile = map->getSelectedBlockIndex(openTile);
uint old_tile = block->tile;
if (old_tile == tile) {
if (old_tile == openTile) {
continue;
}
block->tile = tile;
block->tile = openTile;
map->_setBlock(x, y, *block);
if ((block = map->getBlock(x + 1, y)) && block->tile == old_tile) {
todo.append(QPoint(x + 1, y));
@ -1301,7 +1388,7 @@ void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
if (left && IS_SMART_PATH_TILE(left))
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);
// Visit neighbors if they are smart-path tiles, and don't revisit any.
@ -1333,12 +1420,12 @@ void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
map->paint_tile_width = 1;
map->paint_tile_height = 1;
map->setSelectedMetatilesFromTilePicker();
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) {
QPointF pos = event->pos();
int x = (int)(pos.x()) / 16;
@ -1384,8 +1471,14 @@ void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
map->clearHoveredTile();
} else {
if (editor->current_view == editor->map_item) {
int tile = map->layout->blockdata->blocks->at(blockIndex).tile;
map->hoveredTileChanged(x, y, tile);
} else if (editor->current_view == editor->collision_item) {
int collision = map->layout->blockdata->blocks->at(blockIndex).collision;
int elevation = map->layout->blockdata->blocks->at(blockIndex).elevation;
map->hoveredMovementPermissionTileChanged(collision, elevation);
}
}
}
@ -1479,6 +1572,24 @@ void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
}
}
void CollisionPixmapItem::updateMovementPermissionSelection(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;
int collision = map->getBlock(x, y)->collision;
int elevation = map->getBlock(x, y)->elevation;
map->paint_collision = collision;
map->paint_elevation = elevation;
editor->collision_metatiles_item->draw();
}
void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
active = true;
last_x = (mouse->pos().x() + this->pos().x()) / 16;

View file

@ -17,8 +17,10 @@ class CollisionPixmapItem;
class ConnectionPixmapItem;
class MetatilesPixmapItem;
class BorderMetatilesPixmapItem;
class CollisionMetatilesPixmapItem;
class ElevationMetatilesPixmapItem;
class CurrentSelectedMetatilesPixmapItem;
class MovementPermissionsPixmapItem;
#define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
class Editor : public QObject
{
@ -35,9 +37,12 @@ public:
void undo();
void redo();
void setMap(QString map_name);
void updateCurrentMetatilesSelection();
void displayMap();
void displayMetatiles();
void displayBorderMetatiles();
void displayCurrentMetatilesSelection();
void redrawCurrentMetatilesSelection();
void displayCollisionMetatiles();
void displayElevationMetatiles();
void displayMapEvents();
@ -84,17 +89,24 @@ public:
QList<QGraphicsLineItem*> gridLines;
QGraphicsScene *scene_metatiles = NULL;
QGraphicsScene *scene_current_metatile_selection = NULL;
QGraphicsScene *scene_selected_border_metatiles = NULL;
QGraphicsScene *scene_collision_metatiles = NULL;
QGraphicsScene *scene_elevation_metatiles = NULL;
MetatilesPixmapItem *metatiles_item = NULL;
BorderMetatilesPixmapItem *selected_border_metatiles_item = NULL;
CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
CurrentSelectedMetatilesPixmapItem *scene_current_metatile_selection_item = NULL;
MovementPermissionsPixmapItem *collision_metatiles_item = NULL;
QList<DraggablePixmapItem*> *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;
void objectsView_onMousePress(QMouseEvent *event);
@ -139,6 +151,7 @@ signals:
void loadMapRequested(QString, QString);
void tilesetChanged(QString);
void warpEventDoubleClicked(QString mapName, QString warpNum);
void currentMetatilesSelectionChanged();
};
@ -241,8 +254,10 @@ public:
MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
}
Map *map = NULL;
MapPixmapItem(Map *map_) {
Editor *editor = NULL;
MapPixmapItem(Map *map_, Editor *editor_) {
map = map_;
editor = editor_;
setAcceptHoverEvents(true);
}
bool active;
@ -256,6 +271,7 @@ public:
virtual void pick(QGraphicsSceneMouseEvent*);
virtual void select(QGraphicsSceneMouseEvent*);
virtual void draw(bool ignoreCache = false);
void updateMetatileSelection(QGraphicsSceneMouseEvent *event);
private:
void updateCurHoveredTile(QPointF pos);
@ -279,8 +295,9 @@ class CollisionPixmapItem : public MapPixmapItem {
public:
CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
}
CollisionPixmapItem(Map *map_): MapPixmapItem(map_) {
CollisionPixmapItem(Map *map_, Editor *editor_): MapPixmapItem(map_, editor_) {
}
void updateMovementPermissionSelection(QGraphicsSceneMouseEvent *event);
virtual void paint(QGraphicsSceneMouseEvent*);
virtual void floodFill(QGraphicsSceneMouseEvent*);
virtual void pick(QGraphicsSceneMouseEvent*);
@ -349,7 +366,7 @@ public:
Map* map = NULL;
virtual void draw();
private:
void updateSelection(QPointF pos, Qt::MouseButton button);
void updateSelection(QPointF pos);
protected:
virtual void updateCurHoveredMetatile(QPointF pos);
private slots:
@ -377,34 +394,34 @@ protected:
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 {
Q_OBJECT
public:
MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {}
virtual void pick(uint collision) {
map->paint_collision = collision;
draw();
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
};
class CollisionMetatilesPixmapItem : public MovementPermissionsPixmapItem {
Q_OBJECT
public:
CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
}
virtual void pick(uint collision) {
virtual void pick(uint collision, uint elevation) {
map->paint_collision = collision;
map->paint_elevation = elevation;
draw();
}
virtual void draw() {
setPixmap(map->renderCollisionMetatiles());
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
virtual void updateCurHoveredMetatile(QPointF pos);
private slots:
void paintCollisionChanged(Map *map) {
@ -412,26 +429,4 @@ private slots:
}
};
class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
Q_OBJECT
public:
ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
}
virtual void pick(uint elevation) {
map->paint_elevation = elevation;
draw();
}
virtual void draw() {
setPixmap(map->renderElevationMetatiles());
}
protected:
virtual void updateCurHoveredMetatile(QPointF pos);
private slots:
void paintCollisionChanged(Map *map) {
draw();
}
};
#endif // EDITOR_H

View file

@ -38,6 +38,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(editor, SIGNAL(loadMapRequested(QString, QString)), this, SLOT(onLoadMapRequested(QString, QString)));
connect(editor, SIGNAL(tilesetChanged(QString)), this, SLOT(onTilesetChanged(QString)));
connect(editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString)));
connect(editor, SIGNAL(currentMetatilesSelectionChanged()), this, SLOT(currentMetatilesSelectionChanged()));
on_toolButton_Paint_clicked();
@ -188,13 +189,12 @@ void MainWindow::redrawMapScene()
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_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->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_Elevation->setScene(editor->scene_elevation_metatiles);
//ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2);
}
void MainWindow::openWarpMap(QString map_name, QString warp_num) {
@ -485,6 +485,12 @@ void MainWindow::onTilesetChanged(QString 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)
{
QVariant data = index.data(Qt::UserRole);

View file

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

View file

@ -171,7 +171,7 @@
<bool>true</bool>
</property>
<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 name="text">
<string>Paint</string>
@ -334,7 +334,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>436</width>
<width>429</width>
<height>620</height>
</rect>
</property>
@ -476,7 +476,7 @@
</sizepolicy>
</property>
<attribute name="title">
<string>Blocks</string>
<string>Metatiles</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_3">
<property name="leftMargin">
@ -647,6 +647,143 @@
</widget>
</item>
<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">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
@ -677,8 +814,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>358</width>
<height>497</height>
<width>365</width>
<height>405</height>
</rect>
</property>
<property name="sizePolicy">
@ -775,7 +912,7 @@
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
@ -783,97 +920,78 @@
<attribute name="title">
<string>Collision</string>
</attribute>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y>
<width>111</width>
<height>20</height>
</rect>
<layout class="QGridLayout" name="gridLayout_CollisionSelection">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<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 row="0" column="1">
<widget class="QGraphicsView" name="graphicsView_Collision">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>64</width>
<height>512</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer_18">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>0</x>
<y>80</y>
<width>151</width>
<height>101</height>
</rect>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</spacer>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_19">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>47</width>
<height>13</height>
</rect>
</spacer>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="text">
<string>Elevation</string>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</widget>
<widget class="QGraphicsView" name="graphicsView_Elevation">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>131</width>
<height>61</height>
</rect>
</property>
</widget>
</widget>
<widget class="QFrame" name="frame_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>151</width>
<height>61</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>47</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Collision</string>
</property>
</widget>
<widget class="QGraphicsView" name="graphicsView_Collision">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>66</width>
<height>18</height>
</rect>
</property>
</widget>
</widget>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>

163
map.cpp
View file

@ -12,6 +12,10 @@ Map::Map(QObject *parent) : QObject(parent)
paint_tile_index = 1;
paint_collision = 0;
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) {
@ -77,46 +81,14 @@ int Map::getDisplayedBlockIndex(int index) {
}
QImage Map::getCollisionMetatileImage(Block block) {
return getCollisionMetatileImage(block.collision);
return getCollisionMetatileImage(block.collision, block.elevation);
}
QImage Map::getCollisionMetatileImage(int collision) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
QColor color;
if (collision == 0) {
color.setGreen(0xff);
} else if (collision == 1) {
color.setRed(0xff);
} else if (collision == 2) {
color.setBlue(0xff);
} else if (collision == 3) {
// black
}
metatile_image.fill(color);
return metatile_image;
}
QImage Map::getElevationMetatileImage(Block block) {
return getElevationMetatileImage(block.elevation);
}
QImage Map::getElevationMetatileImage(int elevation) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
QColor color;
if (elevation < 15) {
uint saturation = (elevation + 1) * 16 + 15;
color.setGreen(saturation);
color.setRed(saturation);
color.setBlue(saturation);
} else {
color.setGreen(0xd0);
color.setBlue(0xd0);
color.setRed(0);
}
metatile_image.fill(color);
//QPainter painter(&metatile_image);
//painter.end();
return metatile_image;
QImage Map::getCollisionMetatileImage(int collision, int elevation) {
int x = collision * 16;
int y = elevation * 16;
QPixmap collisionImage = QPixmap(":/images/collisions.png").copy(x, y, 16, 16);
return collisionImage.toImage();
}
bool Map::blockChanged(int i, Blockdata *cache) {
@ -199,40 +171,15 @@ QPixmap Map::renderCollision(bool ignoreCache) {
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
QImage collision_metatile_image = getCollisionMetatileImage(block);
QImage elevation_metatile_image = getElevationMetatileImage(block);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
painter.setOpacity(1);
painter.drawImage(metatile_origin, metatile_image);
painter.save();
if (block.elevation == 15) {
painter.setOpacity(0.5);
} else if (block.elevation == 0) {
painter.setOpacity(0);
} else {
painter.setOpacity(1);//(block.elevation / 16.0) * 0.8);
painter.setCompositionMode(QPainter::CompositionMode_Overlay);
}
painter.drawImage(metatile_origin, elevation_metatile_image);
painter.restore();
painter.save();
if (block.collision == 0) {
painter.setOpacity(0.1);
} else {
painter.setOpacity(0.4);
}
painter.setOpacity(0.55);
painter.drawImage(metatile_origin, collision_metatile_image);
painter.restore();
painter.save();
painter.setOpacity(0.6);
painter.setPen(QColor(255, 255, 255, 192));
painter.setFont(QFont("Helvetica", 8));
painter.drawText(QPoint(metatile_origin.x(), metatile_origin.y() + 8), QString("%1").arg(block.elevation));
painter.restore();
}
painter.end();
cacheCollision();
@ -349,54 +296,35 @@ QPixmap Map::renderConnection(Connection connection) {
}
QPixmap Map::renderCollisionMetatiles() {
int length_ = 4;
int height_ = 1;
int width_ = length_ / height_;
QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
int width_ = 2;
int height_ = 16;
QImage image(width_ * 32, height_ * 32, QImage::Format_RGBA8888);
QPainter painter(&image);
for (int i = 0; i < length_; i++) {
int y = i / width_;
int x = i % width_;
QPoint origin(x * 16, y * 16);
QImage metatile_image = getCollisionMetatileImage(i);
for (int i = 0; i < width_; i++) {
for (int j = 0; j < height_; j++) {
QPoint origin(i * 32, j * 32);
QImage metatile_image = getCollisionMetatileImage(i, j).scaled(32, 32);
painter.drawImage(origin, metatile_image);
}
drawSelection(paint_collision, width_, 1, 1, &painter);
}
drawSelection(paint_collision + paint_elevation * width_, width_, 1, 1, &painter, 32);
painter.end();
return QPixmap::fromImage(image);
}
QPixmap Map::renderElevationMetatiles() {
int length_ = 16;
int height_ = 2;
int width_ = length_ / height_;
QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
QPainter painter(&image);
for (int i = 0; i < length_; i++) {
int y = i / width_;
int x = i % width_;
QPoint origin(x * 16, y * 16);
QImage metatile_image = getElevationMetatileImage(i);
painter.drawImage(origin, metatile_image);
}
drawSelection(paint_elevation, width_, 1, 1, &painter);
painter.end();
return QPixmap::fromImage(image);
}
void Map::drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter) {
void Map::drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter, int gridWidth) {
int x = i % w;
int y = i / w;
painter->save();
QColor penColor = QColor(0xff, 0xff, 0xff);
painter->setPen(penColor);
int rectWidth = selectionWidth * 16;
int rectHeight = selectionHeight * 16;
painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
int rectWidth = selectionWidth * gridWidth;
int rectHeight = selectionHeight * gridWidth;
painter->drawRect(x * gridWidth, y * gridWidth, rectWidth - 1, rectHeight -1);
painter->setPen(QColor(0, 0, 0));
painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
painter->drawRect(x * 16 + 1, y * 16 + 1, rectWidth - 3, rectHeight - 3);
painter->drawRect(x * gridWidth - 1, y * gridWidth - 1, rectWidth + 1, rectHeight + 1);
painter->drawRect(x * gridWidth + 1, y * gridWidth + 1, rectWidth - 3, rectHeight - 3);
painter->restore();
}
@ -423,7 +351,7 @@ QPixmap Map::renderMetatiles() {
painter.drawImage(metatile_origin, metatile_image);
}
drawSelection(paint_tile_index, width_, paint_tile_width, paint_tile_height, &painter);
drawSelection(paint_tile_index, width_, paint_tile_width, paint_tile_height, &painter, 16);
painter.end();
return QPixmap::fromImage(image);
@ -680,7 +608,7 @@ bool Map::hasUnsavedChanges() {
}
void Map::hoveredTileChanged(int x, int y, int block) {
emit statusBarMessage(QString("X: %1, Y: %2, Block: 0x%3")
emit statusBarMessage(QString("X: %1, Y: %2, Metatile: 0x%3")
.arg(x)
.arg(y)
.arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
@ -692,7 +620,7 @@ void Map::clearHoveredTile() {
void Map::hoveredMetatileChanged(int blockIndex) {
int tile = getSelectedBlockIndex(blockIndex);
emit statusBarMessage(QString("Block: 0x%1")
emit statusBarMessage(QString("Metatile: 0x%1")
.arg(QString("%1").arg(tile, 3, 16, QChar('0')).toUpper()));
}
@ -700,18 +628,35 @@ void Map::clearHoveredMetatile() {
emit statusBarMessage(QString(""));
}
void Map::hoveredCollisionTileChanged(int collision) {
emit statusBarMessage(QString("Collision: %1").arg(collision));
void Map::hoveredMovementPermissionTileChanged(int collision, int elevation) {
QString message;
if (collision == 0 && elevation == 0) {
message = "Collision: Transition between elevations";
} else if (collision == 0 && elevation == 15) {
message = "Collision: Multi-Level (Bridge)";
} else if (collision == 0 && elevation == 1) {
message = "Collision: Surf";
} else if (collision == 0) {
message = QString("Collision: Passable, Elevation: %1").arg(elevation);
} else {
message = QString("Collision: Impassable, Elevation: %1").arg(elevation);
}
emit statusBarMessage(message);
}
void Map::clearHoveredCollisionTile() {
void Map::clearHoveredMovementPermissionTile() {
emit statusBarMessage(QString(""));
}
void Map::hoveredElevationTileChanged(int elevation) {
emit statusBarMessage(QString("Elevation: %1").arg(elevation));
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);
}
}
}
void Map::clearHoveredElevationTile() {
emit statusBarMessage(QString(""));
}

19
map.h
View file

@ -155,13 +155,10 @@ public:
QImage collision_image;
QPixmap collision_pixmap;
QImage getCollisionMetatileImage(Block);
QImage getElevationMetatileImage(Block);
QImage getCollisionMetatileImage(int);
QImage getElevationMetatileImage(int);
QImage getCollisionMetatileImage(int, int);
QPixmap renderCollisionMetatiles();
QPixmap renderElevationMetatiles();
void drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter);
void drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter, int gridWidth);
bool blockChanged(int, Blockdata*);
void cacheBlockdata();
@ -177,6 +174,9 @@ public:
int paint_tile_height = 1;
int paint_tile_initial_x;
int paint_tile_initial_y;
int selected_metatiles_width;
int selected_metatiles_height;
QList<int> *selected_metatiles = NULL;
int paint_collision;
int paint_elevation;
@ -214,10 +214,9 @@ public:
void clearHoveredTile();
void hoveredMetatileChanged(int block);
void clearHoveredMetatile();
void hoveredCollisionTileChanged(int collision);
void clearHoveredCollisionTile();
void hoveredElevationTileChanged(int elevation);
void clearHoveredElevationTile();
void hoveredMovementPermissionTileChanged(int collision, int elevation);
void clearHoveredMovementPermissionTile();
void setSelectedMetatilesFromTilePicker();
signals:
void paintTileChanged(Map *map);

View file

@ -15,5 +15,6 @@
<file>icons/add.ico</file>
<file>icons/delete.ico</file>
<file>icons/viewsprites.ico</file>
<file>images/collisions.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -110,7 +110,11 @@ QList<QList<QRgb>> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *
for (int i = 0; i < 6; i++) {
palettes.append(primaryTileset->palettes->at(i));
}
for (int i = 6; i < 12; i++) {
// TODO: Find a reliable way to detect Ruby vs. Emerald
// Ruby's secondary tilesets only use palettes 6-11, whereas
// Emerald uses 6-12.
for (int i = 6; i < 13; i++) {
palettes.append(secondaryTileset->palettes->at(i));
}
return palettes;