Add layering to scripting overlay

This commit is contained in:
GriffinR 2021-11-30 12:58:39 -05:00 committed by huderlem
parent 630febff54
commit af4c99537f
7 changed files with 49 additions and 23 deletions

View file

@ -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<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);

View file

@ -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<int, Overlay*> overlayMap;
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);

View file

@ -75,6 +75,7 @@ public:
~Overlay() {
this->clearItems();
}
void renderItems(QPainter *painter);
QList<OverlayItem*> getItems();
void clearItems();
void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12);

View file

@ -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));

View file

@ -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();
}

View file

@ -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) {

View file

@ -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;