Expand addImage to allow image subsets, transformations, and transparency

This commit is contained in:
GriffinR 2021-11-27 23:50:44 -05:00 committed by huderlem
parent ebd7af8846
commit 630febff54
4 changed files with 33 additions and 5 deletions

View file

@ -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<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);

View file

@ -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<OverlayItem*> items;
};

View file

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

View file

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