diff --git a/include/mainwindow.h b/include/mainwindow.h index fdcbc219..7dc6d63f 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -75,8 +75,8 @@ public: Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000", int layer = 0); 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); - Q_INVOKABLE void createImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false, int layer = 0); - Q_INVOKABLE void addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int palette, bool setTransparency = false, int layer = 0); + Q_INVOKABLE void createImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool xflip = false, bool yflip = false, int paletteId = -1, bool setTransparency = false, int layer = 0); + Q_INVOKABLE void addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency = false, int layer = 0); Q_INVOKABLE void addTilesImage(int x, int y, QJSValue tilesObj, int layer = 0); Q_INVOKABLE void addMetatileImage(int x, int y, int metatileId, int layer = 0); void refreshAfterPaletteChange(Tileset *tileset); diff --git a/include/ui/overlay.h b/include/ui/overlay.h index 04ee7764..5ffe3249 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -81,7 +81,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, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false); + bool addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool xflip = false, bool yflip = false, QList palette = QList(), bool setTransparency = false); bool addImage(int x, int y, QImage image); private: QList items; diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index a61c2803..8a5eaf8c 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -281,18 +281,23 @@ void MainWindow::addFilledRect(int x, int y, int width, int height, QString colo void MainWindow::addImage(int x, int y, QString filepath, int layer) { if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) return; - if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, -1, -1, 0, false, false, false)) + if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath)) this->ui->graphicsView_Map->scene()->update(); } -void MainWindow::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) +void MainWindow::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) { + if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX + || !this->editor || !this->editor->map || !this->editor->map->layout + || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; - if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, width, height, offset, hflip, vflip, setTransparency)) + QList palette; + if (paletteId != -1) + palette = Tileset::getPalette(paletteId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); + if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, width, height, offset, xflip, yflip, palette, setTransparency)) this->ui->graphicsView_Map->scene()->update(); } -void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int palette, bool setTransparency, int layer) { +void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) { if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX || !this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) @@ -300,7 +305,7 @@ void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, QImage image = getPalettedTileImage(tileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary, - palette) + paletteId) .mirrored(xflip, yflip); if (setTransparency) image.setColor(0, qRgba(0, 0, 0, 0)); diff --git a/src/scripting.cpp b/src/scripting.cpp index ce820fc8..496471a9 100644 --- a/src/scripting.cpp +++ b/src/scripting.cpp @@ -14,6 +14,7 @@ Scripting *instance = nullptr; void Scripting::init(MainWindow *mainWindow) { if (instance) { instance->engine->setInterrupted(true); + qDeleteAll(instance->imageCache); delete instance; } instance = new Scripting(mainWindow); diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index c16385c3..55dbd139 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -22,8 +22,11 @@ void GraphicsView::drawForeground(QPainter *painter, const QRectF&) { } void GraphicsView::clearOverlays() { - foreach (Overlay * overlay, this->overlayMap) + foreach (Overlay * overlay, this->overlayMap) { overlay->clearItems(); + delete overlay; + } + this->overlayMap.clear(); } void GraphicsView::setOverlaysHidden(bool hidden) { diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 0318b5e9..65e8845d 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -53,7 +53,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, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) { +bool Overlay::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool xflip, bool yflip, QList palette, bool setTransparency) { QImage image = Scripting::getImage(filepath); if (image.isNull()) { logError(QString("Failed to load image '%1'").arg(filepath)); @@ -81,8 +81,11 @@ bool Overlay::addImage(int x, int y, QString filepath, int width, int height, un 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 (xflip || yflip) + image = image.transformed(QTransform().scale(xflip ? -1 : 1, yflip ? -1 : 1)); + + for (int i = 0; i < palette.size(); i++) + image.setColor(i, palette.at(i)); if (setTransparency) image.setColor(0, qRgba(0, 0, 0, 0));