diff --git a/include/ui/mapview.h b/include/ui/mapview.h index 5954f710..8629c460 100644 --- a/include/ui/mapview.h +++ b/include/ui/mapview.h @@ -45,7 +45,7 @@ public: Q_INVOKABLE void addFilledRect(int x, int y, int width, int height, QString color = "#000000", int layer = 0); Q_INVOKABLE void addImage(int x, int y, QString filepath, int layer = 0, bool useCache = true); Q_INVOKABLE void createImage(int x, int y, QString filepath, - int width = -1, int height = -1, unsigned offset = 0, + int width = -1, int height = -1, int xOffset = 0, int yOffset = 0, qreal hScale = 1, qreal vScale = 1, int paletteId = -1, bool setTransparency = false, int layer = 0, bool useCache = true); Q_INVOKABLE void addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency = false, int layer = 0); diff --git a/include/ui/overlay.h b/include/ui/overlay.h index 0b35964d..e4414d8b 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -95,7 +95,7 @@ public: void clearItems(); void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12); void addRect(int x, int y, int width, int height, QString color = "#000000", bool filled = false); - bool addImage(int x, int y, QString filepath, bool useCache = true, int width = -1, int height = -1, unsigned offset = 0, qreal hScale = 1, qreal vScale = 1, QList palette = QList(), bool setTransparency = false); + bool addImage(int x, int y, QString filepath, bool useCache = true, int width = -1, int height = -1, int xOffset = 0, int yOffset = 0, qreal hScale = 1, qreal vScale = 1, QList palette = QList(), bool setTransparency = false); bool addImage(int x, int y, QImage image); private: QList items; diff --git a/src/scriptapi/apioverlay.cpp b/src/scriptapi/apioverlay.cpp index 3ce3e145..c38ee0ab 100644 --- a/src/scriptapi/apioverlay.cpp +++ b/src/scriptapi/apioverlay.cpp @@ -144,14 +144,14 @@ void MapView::addImage(int x, int y, QString filepath, int layer, bool useCache) this->scene()->update(); } -void MapView::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, qreal hScale, qreal vScale, int paletteId, bool setTransparency, int layer, bool useCache) { +void MapView::createImage(int x, int y, QString filepath, int width, int height, int xOffset, int yOffset, qreal hScale, qreal vScale, int paletteId, bool setTransparency, int layer, bool useCache) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; QList palette; if (paletteId != -1) palette = Tileset::getPalette(paletteId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); - if (this->getOverlay(layer)->addImage(x, y, filepath, useCache, width, height, offset, hScale, vScale, palette, setTransparency)) + if (this->getOverlay(layer)->addImage(x, y, filepath, useCache, width, height, xOffset, yOffset, hScale, vScale, palette, setTransparency)) this->scene()->update(); } diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index f5060708..1b64cb35 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -98,7 +98,7 @@ void Overlay::addRect(int x, int y, int width, int height, QString color, bool f this->items.append(new OverlayRect(x, y, width, height, QColor(color), filled)); } -bool Overlay::addImage(int x, int y, QString filepath, bool useCache, int width, int height, unsigned offset, qreal hScale, qreal vScale, QList palette, bool setTransparency) { +bool Overlay::addImage(int x, int y, QString filepath, bool useCache, int width, int height, int xOffset, int yOffset, qreal hScale, qreal vScale, QList palette, bool setTransparency) { QImage image = useCache ? Scripting::getImage(filepath) : QImage(filepath); if (image.isNull()) { logError(QString("Failed to load image '%1'").arg(filepath)); @@ -108,23 +108,28 @@ bool Overlay::addImage(int x, int y, QString filepath, bool useCache, int width, int fullWidth = image.width(); int fullHeight = image.height(); + // Negative values used as an indicator for "use full dimension" if (width <= 0) width = fullWidth; if (height <= 0) height = fullHeight; - if ((unsigned)(width * height) + offset > (unsigned)(fullWidth * fullHeight)) { - logError(QString("%1x%2 image starting at offset %3 exceeds the image size for '%4'") + if (xOffset < 0) xOffset = 0; + if (yOffset < 0) yOffset = 0; + + if (width + xOffset > fullWidth || height + yOffset > fullHeight) { + logError(QString("%1x%2 image starting at (%3,%4) exceeds the image size for '%5'") .arg(width) .arg(height) - .arg(offset) + .arg(xOffset) + .arg(yOffset) .arg(filepath)); return false; } // Get specified subset of image if (width != fullWidth || height != fullHeight) - image = image.copy(offset % fullWidth, offset / fullWidth, width, height); + image = image.copy(xOffset, yOffset, width, height); if (hScale != 1 || vScale != 1) image = image.transformed(QTransform().scale(hScale, vScale));