From 630febff5484bfc7c7905e8f5e27d88ed600dca4 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sat, 27 Nov 2021 23:50:44 -0500 Subject: [PATCH] Expand addImage to allow image subsets, transformations, and transparency --- include/mainwindow.h | 2 +- include/ui/overlay.h | 2 +- src/mainwindow_scriptapi.cpp | 4 ++-- src/ui/overlay.cpp | 30 +++++++++++++++++++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index 39122b3b..235a8653 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -71,7 +71,7 @@ public: Q_INVOKABLE void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12); Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000"); Q_INVOKABLE void addFilledRect(int x, int y, int width, int height, QString color = "#000000"); - Q_INVOKABLE void addImage(int x, int y, QString filepath); + Q_INVOKABLE void addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false); void refreshAfterPaletteChange(Tileset *tileset); void setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors); Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList> colors); diff --git a/include/ui/overlay.h b/include/ui/overlay.h index b6aa3ef1..28b6e4e0 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -79,7 +79,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 addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false); private: QList items; }; diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 18a696e8..552446d7 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -255,10 +255,10 @@ void MainWindow::addFilledRect(int x, int y, int width, int height, QString colo this->ui->graphicsView_Map->scene()->update(); } -void MainWindow::addImage(int x, int y, QString filepath) { +void MainWindow::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) { if (!this->ui || !this->ui->graphicsView_Map) return; - if (this->ui->graphicsView_Map->overlay.addImage(x, y, filepath)) + if (this->ui->graphicsView_Map->overlay.addImage(x, y, filepath, width, height, offset, hflip, vflip, setTransparency)) this->ui->graphicsView_Map->scene()->update(); } diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index c195d714..0b958544 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -41,12 +41,40 @@ 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 Overlay::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) { QImage image = QImage(filepath); if (image.isNull()) { logError(QString("Failed to load image '%1'").arg(filepath)); return false; } + + int fullWidth = image.width(); + int fullHeight = image.height(); + + 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'") + .arg(width) + .arg(height) + .arg(offset) + .arg(filepath)); + return false; + } + + // Get specified subset of image + if (width != fullWidth || height != fullHeight) + image = image.copy(offset % fullWidth, offset / fullWidth, width, height); + + if (hflip || vflip) + image = image.transformed(QTransform().scale(hflip ? -1 : 1, vflip ? -1 : 1)); + + if (setTransparency) + image.setColor(0, qRgba(0, 0, 0, 0)); + this->items.append(new OverlayImage(x, y, image)); return true; }