diff --git a/include/ui/mapview.h b/include/ui/mapview.h index 957a8bbd..b9f06888 100644 --- a/include/ui/mapview.h +++ b/include/ui/mapview.h @@ -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 x, QList y, QString color = "#000000", int layer = 0); private: QMap overlayMap; diff --git a/include/ui/overlay.h b/include/ui/overlay.h index da379110..ac1f64c2 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -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 palette = QList(), bool setTransparency = false); bool addImage(int x, int y, QImage image); + bool addPath(QList x, QList y, QString color); private: QList items; int x; diff --git a/src/scriptapi/apioverlay.cpp b/src/scriptapi/apioverlay.cpp index 58bf58dd..892befbc 100644 --- a/src/scriptapi/apioverlay.cpp +++ b/src/scriptapi/apioverlay.cpp @@ -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 x, QList y, QString color, int layer) { + if (this->getOverlay(layer)->addPath(x, y, color)) + this->scene()->update(); +} diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 22aba3de..67832403 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -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 x, QList 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; +}