diff --git a/include/ui/mapview.h b/include/ui/mapview.h index b9f06888..e09c5b65 100644 --- a/include/ui/mapview.h +++ b/include/ui/mapview.h @@ -45,8 +45,7 @@ public: Q_INVOKABLE void setOpacity(int opacity, int layer); Q_INVOKABLE void setOpacity(int opacity); Q_INVOKABLE void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12, int layer = 0); - 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 addRect(int x, int y, int width, int height, QString borderColor = "#000000", QString fillColor = "transparent", 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, int xOffset = 0, int yOffset = 0, diff --git a/include/ui/overlay.h b/include/ui/overlay.h index ac1f64c2..deb262e5 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -36,13 +36,13 @@ private: class OverlayRect : public OverlayItem { public: - OverlayRect(int x, int y, int width, int height, QColor color, bool filled) { + OverlayRect(int x, int y, int width, int height, QColor borderColor, QColor fillColor) { this->x = x; this->y = y; this->width = width; this->height = height; - this->color = color; - this->filled = filled; + this->borderColor = borderColor; + this->fillColor = fillColor; } ~OverlayRect() {} virtual void render(QPainter *painter, int x, int y); @@ -51,8 +51,8 @@ private: int y; int width; int height; - QColor color; - bool filled; + QColor borderColor; + QColor fillColor; }; class OverlayImage : public OverlayItem { @@ -72,7 +72,7 @@ private: class OverlayPath : public OverlayItem { public: - OverlayPath(QPainterPath path, QString color) { + OverlayPath(QPainterPath path, QColor color) { this->prevX = 0; this->prevY = 0; this->path = path; @@ -84,7 +84,7 @@ private: int prevX; int prevY; QPainterPath path; - QString color; + QColor color; }; class Overlay @@ -115,12 +115,13 @@ public: void renderItems(QPainter *painter); QList getItems(); void clearItems(); - void addText(const 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 addText(const QString text, int x, int y, QString colorStr, int fontSize); + bool addRect(int x, int y, int width, int height, QString borderColorStr, QString fillColorStr); 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); + bool addPath(QList x, QList y, QString colorStr); private: + QColor getColor(QString colorStr, bool * ok); QList items; int x; int y; diff --git a/src/scriptapi/apioverlay.cpp b/src/scriptapi/apioverlay.cpp index 892befbc..17187ec3 100644 --- a/src/scriptapi/apioverlay.cpp +++ b/src/scriptapi/apioverlay.cpp @@ -148,18 +148,18 @@ void MapView::setOpacity(int opacity) { } void MapView::addText(QString text, int x, int y, QString color, int fontSize, int layer) { - this->getOverlay(layer)->addText(text, x, y, color, fontSize); - this->scene()->update(); + if (this->getOverlay(layer)->addText(text, x, y, color, fontSize)) + this->scene()->update(); } -void MapView::addRect(int x, int y, int width, int height, QString color, int layer) { - this->getOverlay(layer)->addRect(x, y, width, height, color, false); - this->scene()->update(); +void MapView::addRect(int x, int y, int width, int height, QString borderColor, QString fillColor, int layer) { + if (this->getOverlay(layer)->addRect(x, y, width, height, borderColor, fillColor)) + this->scene()->update(); } -void MapView::addFilledRect(int x, int y, int width, int height, QString color, int layer) { - this->getOverlay(layer)->addRect(x, y, width, height, color, true); - 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(); } void MapView::addImage(int x, int y, QString filepath, int layer, bool useCache) { @@ -212,8 +212,3 @@ 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 67832403..1e4e2731 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -11,12 +11,10 @@ void OverlayText::render(QPainter *painter, int x, int y) { } void OverlayRect::render(QPainter *painter, int x, int y) { - if (this->filled) { - painter->fillRect(this->x + x, this->y + y, this->width, this->height, this->color); - } else { - painter->setPen(this->color); - painter->drawRect(this->x + x, this->y + y, this->width, this->height); - } + QRectF rect(this->x + x, this->y + y, this->width, this->height); + painter->setPen(this->borderColor); + painter->drawRect(rect); + painter->fillRect(rect, this->fillColor); } void OverlayImage::render(QPainter *painter, int x, int y) { @@ -124,12 +122,55 @@ void Overlay::move(int deltaX, int deltaY) { this->y += deltaY; } -void Overlay::addText(const QString text, int x, int y, QString color, int fontSize) { - this->items.append(new OverlayText(text, x, y, QColor(color), fontSize)); +QColor Overlay::getColor(QString colorStr, bool * ok) { + if (colorStr.isEmpty()) + colorStr = "transparent"; + QColor color = QColor(colorStr); + if (!color.isValid()) { + logError(QString("\"%1\" is not a valid color. Colors can be in the format \"#RRGGBB\" or \"#AARRGGBB\"").arg(colorStr)); + if (ok) *ok = false; // Not set to true in here, allow for repeat usage + } + return color; } -void Overlay::addRect(int x, int y, int width, int height, QString color, bool filled) { - this->items.append(new OverlayRect(x, y, width, height, QColor(color), filled)); +bool Overlay::addText(const QString text, int x, int y, QString colorStr, int fontSize) { + bool ok = true; + QColor color = getColor(colorStr, &ok); + if (!ok) return false; + + this->items.append(new OverlayText(text, x, y, color, fontSize)); + return true; +} + +bool Overlay::addRect(int x, int y, int width, int height, QString borderColorStr, QString fillColorStr) { + bool ok = true; + QColor borderColor = getColor(borderColorStr, &ok); + QColor fillColor = getColor(fillColorStr, &ok); + if (!ok) return false; + + this->items.append(new OverlayRect(x, y, width, height, borderColor, fillColor)); + return true; +} + +bool Overlay::addPath(QList x, QList y, QString colorStr) { + bool ok = true; + QColor color = getColor(colorStr, &ok); + if (!ok) return false; + + 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; } bool Overlay::addImage(int x, int y, QString filepath, bool useCache, int width, int height, int xOffset, int yOffset, qreal hScale, qreal vScale, QList palette, bool setTransparency) { @@ -186,20 +227,3 @@ 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; -}