From 23efdc2bea60bbcc2b2379a2899365f409a182e0 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Wed, 12 Sep 2018 17:44:30 -0500 Subject: [PATCH] Support spritesheets instead of individual frames for event object pics --- editor.cpp | 1 + editor.h | 7 +++---- event.cpp | 22 ++++++++++++++++++++++ event.h | 5 +++++ project.cpp | 26 ++++++++++++++++++++++---- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/editor.cpp b/editor.cpp index 962907d9..13790463 100755 --- a/editor.cpp +++ b/editor.cpp @@ -1653,6 +1653,7 @@ QList *Editor::getObjects() { void Editor::redrawObject(DraggablePixmapItem *item) { if (item) { item->setPixmap(item->event->pixmap); + item->setShapeMode(QGraphicsPixmapItem::BoundingRectShape); if (selected_events && selected_events->contains(item)) { QImage image = item->pixmap().toImage(); QPainter painter(&image); diff --git a/editor.h b/editor.h index 29b832dc..1beadf75 100755 --- a/editor.h +++ b/editor.h @@ -174,10 +174,8 @@ public: int last_x; int last_y; void updatePosition() { - int x = event->x() * 16; - int y = event->y() * 16; - x -= pixmap().width() / 32 * 16; - y -= pixmap().height() - 16; + int x = event->getPixelX(); + int y = event->getPixelY(); setX(x); setY(y); setZValue(event->y()); @@ -193,6 +191,7 @@ public: objects.append(event); event->pixmap = QPixmap(); editor->project->loadEventPixmaps(objects); + this->updatePosition(); editor->redrawObject(this); emit spriteChanged(event->pixmap); } diff --git a/event.cpp b/event.cpp index c92b9a26..a1c6f5e4 100755 --- a/event.cpp +++ b/event.cpp @@ -12,6 +12,8 @@ QString EventType::HealLocation = "event_heal_location"; Event::Event() { + this->spriteWidth = 16; + this->spriteHeight = 16; } Event* Event::createNewEvent(QString event_type, QString map_name) @@ -126,6 +128,16 @@ Event* Event::createNewSecretBaseEvent() return event; } +int Event::getPixelX() +{ + return (this->x() * 16) - qMax(0, (this->spriteWidth - 16) / 2); +} + +int Event::getPixelY() +{ + return (this->y() * 16) - qMax(0, this->spriteHeight - 16); +} + QString Event::buildObjectEventMacro(int item_index) { int radius_x = this->getInt("radius_x"); @@ -236,3 +248,13 @@ QString Event::buildSecretBaseEventMacro() text += "\n"; return text; } + +void Event::setPixmapFromSpritesheet(QImage spritesheet, int spriteWidth, int spriteHeight) +{ + // Set first palette color fully transparent. + QImage img = spritesheet.copy(0, 0, spriteWidth, spriteHeight); + img.setColor(0, qRgba(0, 0, 0, 0)); + pixmap = QPixmap::fromImage(img); + this->spriteWidth = spriteWidth; + this->spriteHeight = spriteHeight; +} diff --git a/event.h b/event.h index 13aa114a..fc71e8a7 100755 --- a/event.h +++ b/event.h @@ -71,9 +71,14 @@ public: QString buildSignEventMacro(); QString buildHiddenItemEventMacro(); QString buildSecretBaseEventMacro(); + void setPixmapFromSpritesheet(QImage, int, int); + int getPixelX(); + int getPixelY(); QMap values; QPixmap pixmap; + int spriteWidth; + int spriteHeight; }; #endif // EVENT_H diff --git a/project.cpp b/project.cpp index bdd4c8ec..61baae72 100755 --- a/project.cpp +++ b/project.cpp @@ -1292,6 +1292,9 @@ void Project::loadEventPixmaps(QList objects) { if (!object->pixmap.isNull()) { continue; } + + object->spriteWidth = 16; + object->spriteHeight = 16; QString event_type = object->get("event_type"); if (event_type == EventType::Object) { object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16); @@ -1309,16 +1312,31 @@ void Project::loadEventPixmaps(QList objects) { int sprite_id = constants.value(object->get("sprite")); QString info_label = pointers.value(sprite_id).replace("&", ""); - QString pic_label = readCArray(info_text, info_label).value(14); + QStringList gfx_info = readCArray(info_text, info_label); + QString pic_label = gfx_info.value(14); + QString dimensions_label = gfx_info.value(11); + QString subsprites_label = gfx_info.value(12); QString gfx_label = readCArray(pic_text, pic_label).value(0); gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1); QString path = readCIncbin(assets_text, gfx_label); if (!path.isNull()) { path = fixGraphicPath(path); - QPixmap pixmap(root + "/" + path); - if (!pixmap.isNull()) { - object->pixmap = pixmap; + QImage spritesheet(root + "/" + path); + if (!spritesheet.isNull()) { + // Infer the sprite dimensions from the OAM labels. + int spriteWidth = spritesheet.width(); + int spriteHeight = spritesheet.height(); + QRegularExpression re("\\S+_(\\d+)x(\\d+)"); + QRegularExpressionMatch dimensionMatch = re.match(dimensions_label); + if (dimensionMatch.hasMatch()) { + QRegularExpressionMatch oamTablesMatch = re.match(subsprites_label); + if (oamTablesMatch.hasMatch()) { + spriteWidth = dimensionMatch.captured(1).toInt(); + spriteHeight = dimensionMatch.captured(2).toInt(); + } + } + object->setPixmapFromSpritesheet(spritesheet, spriteWidth, spriteHeight); } } }