Add path drawing to the API

This commit is contained in:
GriffinR 2022-10-11 10:34:09 -04:00 committed by Marcus Huderle
parent 2fa2fc52e8
commit 385c17fd23
4 changed files with 52 additions and 0 deletions

View file

@ -55,6 +55,7 @@ public:
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 addTileImage(int x, int y, QJSValue tileObj, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addMetatileImage(int x, int y, int metatileId, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addPath(QList<int> x, QList<int> y, QString color = "#000000", int layer = 0);
private:
QMap<int, Overlay*> overlayMap;

View file

@ -70,6 +70,23 @@ private:
QImage image;
};
class OverlayPath : public OverlayItem {
public:
OverlayPath(QPainterPath path, QString color) {
this->prevX = 0;
this->prevY = 0;
this->path = path;
this->color = color;
}
~OverlayPath() {}
virtual void render(QPainter *painter, int x, int y);
private:
int prevX;
int prevY;
QPainterPath path;
QString color;
};
class Overlay
{
public:
@ -102,6 +119,7 @@ public:
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, 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);
bool addPath(QList<int> x, QList<int> y, QString color);
private:
QList<OverlayItem*> items;
int x;

View file

@ -212,3 +212,8 @@ void MapView::addMetatileImage(int x, int y, int metatileId, bool setTransparenc
if (this->getOverlay(layer)->addImage(x, y, image))
this->scene()->update();
}
void MapView::addPath(QList<int> x, QList<int> y, QString color, int layer) {
if (this->getOverlay(layer)->addPath(x, y, color))
this->scene()->update();
}

View file

@ -23,6 +23,17 @@ void OverlayImage::render(QPainter *painter, int x, int y) {
painter->drawImage(this->x + x, this->y + y, this->image);
}
void OverlayPath::render(QPainter *painter, int x, int y) {
if (x != this->prevX || y != this->prevY) {
// Overlay has moved since the path was last drawn
path.translate(x - prevX, y - prevY);
}
this->prevX = x;
this->prevY = y;
painter->setPen(this->color);
painter->drawPath(this->path);
}
void Overlay::renderItems(QPainter *painter) {
if (this->hidden) return;
@ -175,3 +186,20 @@ bool Overlay::addImage(int x, int y, QImage image) {
this->items.append(new OverlayImage(x, y, image));
return true;
}
bool Overlay::addPath(QList<int> x, QList<int> y, QString color) {
int numPoints = qMin(x.length(), y.length());
if (numPoints < 2) {
logError("Overlay path must have at least two points.");
return false;
}
QPainterPath path;
path.moveTo(x.at(0), y.at(0));
for (int i = 1; i < numPoints; i++)
path.lineTo(x.at(i), y.at(i));
this->items.append(new OverlayPath(path, color));
return true;
}