Add rounding to addRect, add fill color to addPath

This commit is contained in:
GriffinR 2022-10-11 14:52:52 -04:00 committed by Marcus Huderle
parent 77d04bb6de
commit 67bec313a5
4 changed files with 47 additions and 79 deletions

View file

@ -45,7 +45,8 @@ 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 borderColor = "#000000", QString fillColor = "transparent", int layer = 0);
Q_INVOKABLE void addRect(int x, int y, int width, int height, QString borderColor = "#000000", QString fillColor = "transparent", int rounding = 0, int layer = 0);
Q_INVOKABLE void addPath(QList<int> x, QList<int> y, 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,
@ -54,7 +55,6 @@ 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

@ -34,23 +34,21 @@ private:
int fontSize;
};
class OverlayRect : public OverlayItem {
class OverlayPath : public OverlayItem {
public:
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;
OverlayPath(QPainterPath path, QColor borderColor, QColor fillColor) {
this->prevX = 0;
this->prevY = 0;
this->path = path;
this->borderColor = borderColor;
this->fillColor = fillColor;
}
~OverlayRect() {}
~OverlayPath() {}
virtual void render(QPainter *painter, int x, int y);
private:
int x;
int y;
int width;
int height;
int prevX;
int prevY;
QPainterPath path;
QColor borderColor;
QColor fillColor;
};
@ -70,23 +68,6 @@ private:
QImage image;
};
class OverlayPath : public OverlayItem {
public:
OverlayPath(QPainterPath path, QColor 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;
QColor color;
};
class Overlay
{
public:
@ -115,13 +96,13 @@ public:
void renderItems(QPainter *painter);
QList<OverlayItem*> getItems();
void clearItems();
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);
void 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, int rounding);
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 colorStr);
bool addPath(QList<int> x, QList<int> y, QString borderColorStr, QString fillColorStr);
private:
QColor getColor(QString colorStr, bool * ok);
QColor getColor(QString colorStr);
QList<OverlayItem*> items;
int x;
int y;

View file

@ -148,17 +148,17 @@ void MapView::setOpacity(int opacity) {
}
void MapView::addText(QString text, int x, int y, QString color, int fontSize, int layer) {
if (this->getOverlay(layer)->addText(text, x, y, color, fontSize))
this->getOverlay(layer)->addText(text, x, y, color, fontSize);
this->scene()->update();
}
void MapView::addRect(int x, int y, int width, int height, QString borderColor, QString fillColor, int rounding, int layer) {
if (this->getOverlay(layer)->addRect(x, y, width, height, borderColor, fillColor, rounding))
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::addPath(QList<int> x, QList<int> y, QString color, int layer) {
if (this->getOverlay(layer)->addPath(x, y, color))
void MapView::addPath(QList<int> x, QList<int> y, QString borderColor, QString fillColor, int layer) {
if (this->getOverlay(layer)->addPath(x, y, borderColor, fillColor))
this->scene()->update();
}

View file

@ -10,17 +10,6 @@ void OverlayText::render(QPainter *painter, int x, int y) {
painter->drawStaticText(this->x + x, this->y + y, this->text);
}
void OverlayRect::render(QPainter *painter, int x, int y) {
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) {
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
@ -28,8 +17,13 @@ void OverlayPath::render(QPainter *painter, int x, int y) {
}
this->prevX = x;
this->prevY = y;
painter->setPen(this->color);
painter->setPen(this->borderColor);
painter->drawPath(this->path);
painter->fillPath(this->path, this->fillColor);
}
void OverlayImage::render(QPainter *painter, int x, int y) {
painter->drawImage(this->x + x, this->y + y, this->image);
}
void Overlay::renderItems(QPainter *painter) {
@ -76,7 +70,7 @@ int Overlay::getOpacity() {
void Overlay::setOpacity(int opacity) {
if (opacity < 0 || opacity > 100) {
logError(QString("Invalid overlay opacity '%1'").arg(opacity));
logError(QString("Invalid overlay opacity '%1', must be in range 0-100").arg(opacity));
return;
}
this->opacity = static_cast<qreal>(opacity) / 100;
@ -122,41 +116,34 @@ void Overlay::move(int deltaX, int deltaY) {
this->y += deltaY;
}
QColor Overlay::getColor(QString colorStr, bool * ok) {
QColor Overlay::getColor(QString colorStr) {
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
logWarn(QString("Invalid overlay color \"%1\". Colors can be in the format \"#RRGGBB\" or \"#AARRGGBB\"").arg(colorStr));
color = QColor("transparent");
}
return color;
}
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;
void Overlay::addText(const QString text, int x, int y, QString colorStr, int fontSize) {
this->items.append(new OverlayText(text, x, y, getColor(colorStr), fontSize));
}
this->items.append(new OverlayText(text, x, y, color, fontSize));
bool Overlay::addRect(int x, int y, int width, int height, QString borderColorStr, QString fillColorStr, int rounding) {
if (rounding < 0 || rounding > 100) {
logError(QString("Invalid rectangle rounding '%1', must be in range 0-100").arg(rounding));
return false;
}
QPainterPath path;
path.addRoundedRect(QRectF(x, y, width, height), rounding, rounding, Qt::RelativeSize);
this->items.append(new OverlayPath(path, getColor(borderColorStr), getColor(fillColorStr)));
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<int> x, QList<int> y, QString colorStr) {
bool ok = true;
QColor color = getColor(colorStr, &ok);
if (!ok) return false;
bool Overlay::addPath(QList<int> x, QList<int> y, QString borderColorStr, QString fillColorStr) {
int numPoints = qMin(x.length(), y.length());
if (numPoints < 2) {
logError("Overlay path must have at least two points.");
@ -169,7 +156,7 @@ bool Overlay::addPath(QList<int> x, QList<int> y, QString colorStr) {
for (int i = 1; i < numPoints; i++)
path.lineTo(x.at(i), y.at(i));
this->items.append(new OverlayPath(path, color));
this->items.append(new OverlayPath(path, getColor(borderColorStr), getColor(fillColorStr)));
return true;
}