diff --git a/include/mainwindow.h b/include/mainwindow.h index f9a016a9..ce447cdf 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -67,10 +67,26 @@ public: Q_INVOKABLE void setDimensions(int width, int height); Q_INVOKABLE void setWidth(int width); Q_INVOKABLE void setHeight(int height); - Q_INVOKABLE void clearOverlay(int layer = INT_MAX); - Q_INVOKABLE void hideOverlay(int layer = INT_MAX); - Q_INVOKABLE void showOverlay(int layer = INT_MAX); - Q_INVOKABLE void setOverlayVisibility(bool visible, int layer = INT_MAX); + Q_INVOKABLE void clearOverlay(int layer = 0); + Q_INVOKABLE void clearOverlays(); + Q_INVOKABLE void hideOverlay(int layer = 0); + Q_INVOKABLE void hideOverlays(); + Q_INVOKABLE void showOverlay(int layer = 0); + Q_INVOKABLE void showOverlays(); + Q_INVOKABLE bool getOverlayVisibility(int layer = 0); + Q_INVOKABLE void setOverlayVisibility(bool visible, int layer = 0); + Q_INVOKABLE void setOverlaysVisibility(bool visible); + Q_INVOKABLE int getOverlayX(int layer = 0); + Q_INVOKABLE int getOverlayY(int layer = 0); + Q_INVOKABLE void setOverlayX(int x, int layer = 0); + Q_INVOKABLE void setOverlayY(int y, int layer = 0); + Q_INVOKABLE void setOverlaysX(int x); + Q_INVOKABLE void setOverlaysY(int y); + Q_INVOKABLE QJSValue getOverlayPosition(int layer = 0); + Q_INVOKABLE void setOverlayPosition(int x, int y, int layer = 0); + Q_INVOKABLE void setOverlaysPosition(int x, int y); + Q_INVOKABLE void moveOverlay(int deltaX, int deltaY, int layer = 0); + Q_INVOKABLE void moveOverlays(int deltaX, int deltaY); 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); diff --git a/include/scripting.h b/include/scripting.h index fab2dbfc..47f3ba72 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -25,6 +25,7 @@ public: static QJSValue fromTile(Tile tile); static Tile toTile(QJSValue obj); static QJSValue dimensions(int width, int height); + static QJSValue position(int x, int y); static QJSEngine *getEngine(); static QImage getImage(QString filepath); static void init(MainWindow *mainWindow); diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 78bfe40d..7c5cc2bc 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -15,6 +15,10 @@ public: Overlay * getOverlay(int layer); void clearOverlays(); void setOverlaysHidden(bool hidden); + void setOverlaysX(int x); + void setOverlaysY(int y); + void setOverlaysPosition(int x, int y); + void moveOverlays(int deltaX, int deltaY); public: // GraphicsView_Object object; diff --git a/include/ui/overlay.h b/include/ui/overlay.h index 5ffe3249..7c134251 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -10,7 +10,7 @@ class OverlayItem { public: OverlayItem() {} virtual ~OverlayItem() {}; - virtual void render(QPainter *) {}; + virtual void render(QPainter *, int, int) {}; }; class OverlayText : public OverlayItem { @@ -23,7 +23,7 @@ public: this->fontSize = fontSize; } ~OverlayText() {} - virtual void render(QPainter *painter); + virtual void render(QPainter *painter, int x, int y); private: QString text; int x; @@ -43,7 +43,7 @@ public: this->filled = filled; } ~OverlayRect() {} - virtual void render(QPainter *painter); + virtual void render(QPainter *painter, int x, int y); private: int x; int y; @@ -61,7 +61,7 @@ public: this->image = image; } ~OverlayImage() {} - virtual void render(QPainter *painter); + virtual void render(QPainter *painter, int x, int y); private: int x; int y; @@ -71,11 +71,22 @@ private: class Overlay { public: - Overlay() {} + Overlay() { + this->x = 0; + this->y = 0; + this->hidden = false; + } ~Overlay() { this->clearItems(); } + bool getHidden(); void setHidden(bool hidden); + int getX(); + int getY(); + void setX(int x); + void setY(int y); + void setPosition(int x, int y); + void move(int deltaX, int deltaY); void renderItems(QPainter *painter); QList getItems(); void clearItems(); @@ -85,6 +96,8 @@ public: bool addImage(int x, int y, QImage image); private: QList items; + int x; + int y; bool hidden; }; diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 2bd38649..bccff8a2 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -231,11 +231,14 @@ void MainWindow::setHeight(int height) { void MainWindow::clearOverlay(int layer) { if (!this->ui || !this->ui->graphicsView_Map) return; - // INT_MAX is used as an indicator value to refer to all overlays - if (layer == INT_MAX) - this->ui->graphicsView_Map->clearOverlays(); - else - this->ui->graphicsView_Map->getOverlay(layer)->clearItems(); + this->ui->graphicsView_Map->getOverlay(layer)->clearItems(); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::clearOverlays() { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->clearOverlays(); this->ui->graphicsView_Map->scene()->update(); } @@ -243,51 +246,143 @@ void MainWindow::hideOverlay(int layer) { this->setOverlayVisibility(false, layer); } +void MainWindow::hideOverlays() { + this->setOverlaysVisibility(false); +} + void MainWindow::showOverlay(int layer) { this->setOverlayVisibility(true, layer); } +void MainWindow::showOverlays() { + this->setOverlaysVisibility(true); +} + +bool MainWindow::getOverlayVisibility(int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return false; + return !(this->ui->graphicsView_Map->getOverlay(layer)->getHidden()); +} + void MainWindow::setOverlayVisibility(bool visible, int layer) { if (!this->ui || !this->ui->graphicsView_Map) return; - if (layer == INT_MAX) - this->ui->graphicsView_Map->setOverlaysHidden(!visible); - else - this->ui->graphicsView_Map->getOverlay(layer)->setHidden(!visible); + this->ui->graphicsView_Map->getOverlay(layer)->setHidden(!visible); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::setOverlaysVisibility(bool visible) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->setOverlaysHidden(!visible); + this->ui->graphicsView_Map->scene()->update(); +} + +int MainWindow::getOverlayX(int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return 0; + return this->ui->graphicsView_Map->getOverlay(layer)->getX(); +} + +int MainWindow::getOverlayY(int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return 0; + return this->ui->graphicsView_Map->getOverlay(layer)->getY(); +} + +void MainWindow::setOverlayX(int x, int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->getOverlay(layer)->setX(x); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::setOverlayY(int y, int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->getOverlay(layer)->setY(y); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::setOverlaysX(int x) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->setOverlaysX(x); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::setOverlaysY(int y) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->setOverlaysY(y); + this->ui->graphicsView_Map->scene()->update(); +} + +QJSValue MainWindow::getOverlayPosition(int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return QJSValue(); + Overlay * overlay = this->ui->graphicsView_Map->getOverlay(layer); + return Scripting::position(overlay->getX(), overlay->getY()); +} + +void MainWindow::setOverlayPosition(int x, int y, int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->getOverlay(layer)->setPosition(x, y); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::setOverlaysPosition(int x, int y) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->setOverlaysPosition(x, y); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::moveOverlay(int deltaX, int deltaY, int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->getOverlay(layer)->move(deltaX, deltaY); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::moveOverlays(int deltaX, int deltaY) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->moveOverlays(deltaX, deltaY); this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addText(QString text, int x, int y, QString color, int fontSize, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) + if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->getOverlay(layer)->addText(text, x, y, color, fontSize); this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addRect(int x, int y, int width, int height, QString color, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) + if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->getOverlay(layer)->addRect(x, y, width, height, color, false); this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addFilledRect(int x, int y, int width, int height, QString color, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) + if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->getOverlay(layer)->addRect(x, y, width, height, color, true); this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addImage(int x, int y, QString filepath, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) + if (!this->ui || !this->ui->graphicsView_Map) return; if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath)) this->ui->graphicsView_Map->scene()->update(); } void MainWindow::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX - || !this->editor || !this->editor->map || !this->editor->map->layout + if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; QList palette; @@ -298,8 +393,7 @@ void MainWindow::createImage(int x, int y, QString filepath, int width, int heig } void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX - || !this->editor || !this->editor->map || !this->editor->map->layout + if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; QImage image = getPalettedTileImage(tileId, @@ -314,8 +408,7 @@ void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, } void MainWindow::addTilesImage(int x, int y, QJSValue tilesObj, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX - || !this->editor || !this->editor->map || !this->editor->map->layout + if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; @@ -340,8 +433,7 @@ void MainWindow::addTilesImage(int x, int y, QJSValue tilesObj, int layer) { } void MainWindow::addMetatileImage(int x, int y, int metatileId, int layer) { - if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX - || !this->editor || !this->editor->map || !this->editor->map->layout + if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary) return; QImage image = getMetatileImage(static_cast(metatileId), diff --git a/src/scripting.cpp b/src/scripting.cpp index c532e996..8e7c2351 100644 --- a/src/scripting.cpp +++ b/src/scripting.cpp @@ -179,6 +179,13 @@ QJSValue Scripting::dimensions(int width, int height) { return obj; } +QJSValue Scripting::position(int x, int y) { + QJSValue obj = instance->engine->newObject(); + obj.setProperty("x", x); + obj.setProperty("y", y); + return obj; +} + Tile Scripting::toTile(QJSValue obj) { if (!obj.hasProperty("tileId") || !obj.hasProperty("xflip") diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index 55dbd139..80e61afa 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -34,6 +34,26 @@ void GraphicsView::setOverlaysHidden(bool hidden) { overlay->setHidden(hidden); } +void GraphicsView::setOverlaysX(int x) { + foreach (Overlay * overlay, this->overlayMap) + overlay->setX(x); +} + +void GraphicsView::setOverlaysY(int y) { + foreach (Overlay * overlay, this->overlayMap) + overlay->setY(y); +} + +void GraphicsView::setOverlaysPosition(int x, int y) { + foreach (Overlay * overlay, this->overlayMap) + overlay->setPosition(x, y); +} + +void GraphicsView::moveOverlays(int deltaX, int deltaY) { + foreach (Overlay * overlay, this->overlayMap) + overlay->move(deltaX, deltaY); +} + Overlay * GraphicsView::getOverlay(int layer) { Overlay * overlay = this->overlayMap.value(layer, nullptr); if (!overlay) { diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 65e8845d..a111753d 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -2,32 +2,32 @@ #include "scripting.h" #include "log.h" -void OverlayText::render(QPainter *painter) { +void OverlayText::render(QPainter *painter, int x, int y) { QFont font = painter->font(); font.setPixelSize(this->fontSize); painter->setFont(font); painter->setPen(this->color); - painter->drawText(this->x, this->y, this->text); + painter->drawText(this->x + x, this->y + y, this->text); } -void OverlayRect::render(QPainter *painter) { +void OverlayRect::render(QPainter *painter, int x, int y) { if (this->filled) { - painter->fillRect(this->x, this->y, this->width, this->height, this->color); + painter->fillRect(this->x + x, this->y + y, this->width, this->height, this->color); } else { painter->setPen(this->color); - painter->drawRect(this->x, this->y, this->width, this->height); + painter->drawRect(this->x + x, this->y + y, this->width, this->height); } } -void OverlayImage::render(QPainter *painter) { - painter->drawImage(this->x, this->y, this->image); +void OverlayImage::render(QPainter *painter, int x, int y) { + painter->drawImage(this->x + x, this->y + y, this->image); } void Overlay::renderItems(QPainter *painter) { if (this->hidden) return; for (auto item : this->items) - item->render(painter); + item->render(painter, this->x, this->y); } void Overlay::clearItems() { @@ -41,10 +41,40 @@ QList Overlay::getItems() { return this->items; } +bool Overlay::getHidden() { + return this->hidden; +} + void Overlay::setHidden(bool hidden) { this->hidden = hidden; } +int Overlay::getX() { + return this->x; +} + +int Overlay::getY() { + return this->y; +} + +void Overlay::setX(int x) { + this->x = x; +} + +void Overlay::setY(int y) { + this->y = y; +} + +void Overlay::setPosition(int x, int y) { + this->x = x; + this->y = y; +} + +void Overlay::move(int deltaX, int deltaY) { + this->x += deltaX; + this->y += deltaY; +} + void Overlay::addText(QString text, int x, int y, QString color, int fontSize) { this->items.append(new OverlayText(text, x, y, QColor(color), fontSize)); }