diff --git a/include/mainwindow.h b/include/mainwindow.h index 235a8653..f94497f3 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -67,11 +67,11 @@ 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(); - Q_INVOKABLE void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12); - Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000"); - Q_INVOKABLE void addFilledRect(int x, int y, int width, int height, QString color = "#000000"); - Q_INVOKABLE void addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false); + Q_INVOKABLE void clearOverlay(int layer = INT_MAX); + 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 addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false, int layer = 0); void refreshAfterPaletteChange(Tileset *tileset); void setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors); Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList> colors); diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 960f7dc4..69faef6c 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -12,11 +12,13 @@ class GraphicsView : public QGraphicsView public: GraphicsView() : QGraphicsView() {} GraphicsView(QWidget *parent) : QGraphicsView(parent) {} + Overlay * getOverlay(int layer); + void clearOverlays(); public: // GraphicsView_Object object; Editor *editor; - Overlay overlay; + QMap overlayMap; protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); diff --git a/include/ui/overlay.h b/include/ui/overlay.h index 28b6e4e0..ec0ab7cb 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -75,6 +75,7 @@ public: ~Overlay() { this->clearItems(); } + void renderItems(QPainter *painter); QList getItems(); void clearItems(); void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bcc1a2b8..61c7c8ce 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -605,7 +605,7 @@ void MainWindow::on_action_Open_Project_triggered() if (!dir.isEmpty()) { if (this->editor && this->editor->project) { Scripting::cb_ProjectClosed(this->editor->project->root); - this->ui->graphicsView_Map->overlay.clearItems(); + this->ui->graphicsView_Map->clearOverlays(); } porymapConfig.setRecentProject(dir); setWindowDisabled(!openProject(dir)); diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 552446d7..52b87e7d 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -227,38 +227,42 @@ void MainWindow::setHeight(int height) { this->onMapNeedsRedrawing(); } -void MainWindow::clearOverlay() { +void MainWindow::clearOverlay(int layer) { if (!this->ui || !this->ui->graphicsView_Map) return; - this->ui->graphicsView_Map->overlay.clearItems(); + // 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->scene()->update(); } -void MainWindow::addText(QString text, int x, int y, QString color, int fontSize) { - if (!this->ui || !this->ui->graphicsView_Map) +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) return; - this->ui->graphicsView_Map->overlay.addText(text, x, y, color, fontSize); + 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) { - if (!this->ui || !this->ui->graphicsView_Map) +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) return; - this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, false); + 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) { - if (!this->ui || !this->ui->graphicsView_Map) +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) return; - this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); + 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 width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) { - if (!this->ui || !this->ui->graphicsView_Map) +void MainWindow::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency, int layer) { + if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) return; - if (this->ui->graphicsView_Map->overlay.addImage(x, y, filepath, width, height, offset, hflip, vflip, setTransparency)) + if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, width, height, offset, hflip, vflip, setTransparency)) this->ui->graphicsView_Map->scene()->update(); } diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index 0e518cf7..2363b511 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -17,9 +17,22 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event) { } void GraphicsView::drawForeground(QPainter *painter, const QRectF&) { - for (auto item : this->overlay.getItems()) { - item->render(painter); + foreach (Overlay * overlay, this->overlayMap) + overlay->renderItems(painter); +} + +void GraphicsView::clearOverlays() { + foreach (Overlay * overlay, this->overlayMap) + overlay->clearItems(); +} + +Overlay * GraphicsView::getOverlay(int layer) { + Overlay * overlay = this->overlayMap.value(layer, nullptr); + if (!overlay) { + overlay = new Overlay(); + this->overlayMap.insert(layer, overlay); } + return overlay; } void GraphicsView::moveEvent(QMoveEvent *event) { diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 0b958544..955ed626 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -22,6 +22,12 @@ void OverlayImage::render(QPainter *painter) { painter->drawImage(this->x, this->y, this->image); } +void Overlay::renderItems(QPainter *painter) { + for (auto item : this->items) { + item->render(painter); + } +} + void Overlay::clearItems() { for (auto item : this->items) { delete item;