Replace offset in createImage with x/y offset

This commit is contained in:
GriffinR 2022-10-01 17:38:50 -04:00 committed by Marcus Huderle
parent 9fd6d8417a
commit 1b743f9625
4 changed files with 14 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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