Add paletteId to createImage

This commit is contained in:
GriffinR 2021-12-08 09:20:17 -05:00 committed by huderlem
parent eca84beae0
commit 03949d45a4
6 changed files with 25 additions and 13 deletions

View file

@ -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);

View file

@ -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<QRgb> palette = QList<QRgb>(), bool setTransparency = false);
bool addImage(int x, int y, QImage image);
private:
QList<OverlayItem*> items;

View file

@ -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<QRgb> 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));

View file

@ -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);

View file

@ -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) {

View file

@ -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<QRgb> 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));