diff --git a/include/mainwindow.h b/include/mainwindow.h index 8c343220..c69351f4 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -58,6 +58,12 @@ 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); + public slots: void scaleMapView(int); diff --git a/include/scripting.h b/include/scripting.h index 84468e11..7b8b0f91 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -9,6 +9,7 @@ enum CallbackType { OnBlockChanged, + OnMapOpened, }; class Scripting @@ -19,6 +20,7 @@ public: static QJSValue dimensions(int width, int height); static void init(MainWindow *mainWindow); static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock); + static void cb_MapOpened(QString mapName); private: QJSEngine *engine; diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 0b3a12c7..85fb7a73 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -1,6 +1,7 @@ #ifndef GRAPHICSVIEW_H #define GRAPHICSVIEW_H +#include "overlay.h" #include #include @@ -15,10 +16,12 @@ public: public: // GraphicsView_Object object; Editor *editor; + Overlay overlay; protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); + void drawForeground(QPainter *painter, const QRectF &rect); }; //Q_DECLARE_METATYPE(GraphicsView) diff --git a/include/ui/overlay.h b/include/ui/overlay.h new file mode 100644 index 00000000..a8e13337 --- /dev/null +++ b/include/ui/overlay.h @@ -0,0 +1,87 @@ +#ifndef OVERLAY_H +#define OVERLAY_H + +#include +#include +#include +#include + +class OverlayItem { +public: + OverlayItem() {} + virtual ~OverlayItem() {}; + virtual void render(QPainter *painter) {}; +}; + +class OverlayText : public OverlayItem { +public: + OverlayText(QString text, int x, int y, QColor color, int fontSize) { + this->text = text; + this->x = x; + this->y = y; + this->color = color; + this->fontSize = fontSize; + } + ~OverlayText() {} + virtual void render(QPainter *painter); +private: + QString text; + int x; + int y; + QColor color; + int fontSize; +}; + +class OverlayRect : public OverlayItem { +public: + OverlayRect(int x, int y, int width, int height, QColor color, bool filled) { + this->x = x; + this->y = y; + this->width = width; + this->height = height; + this->color = color; + this->filled = filled; + } + ~OverlayRect() {} + virtual void render(QPainter *painter); +private: + int x; + int y; + int width; + int height; + QColor color; + bool filled; +}; + +class OverlayImage : public OverlayItem { +public: + OverlayImage(int x, int y, QImage image) { + this->x = x; + this->y = y; + this->image = image; + } + ~OverlayImage() {} + virtual void render(QPainter *painter); +private: + int x; + int y; + QImage image; +}; + +class Overlay +{ +public: + Overlay() {} + ~Overlay() { + this->clearItems(); + } + QList getItems(); + void clearItems(); + void addText(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); + void addImage(int x, int y, QString filepath); +private: + QList items; +}; + +#endif // OVERLAY_H diff --git a/porymap.pro b/porymap.pro index 1a58b420..2a138e30 100644 --- a/porymap.pro +++ b/porymap.pro @@ -36,6 +36,7 @@ SOURCES += src/core/block.cpp \ src/ui/collisionpixmapitem.cpp \ src/ui/connectionpixmapitem.cpp \ src/ui/currentselectedmetatilespixmapitem.cpp \ + src/ui/overlay.cpp \ src/ui/regionmaplayoutpixmapitem.cpp \ src/ui/regionmapentriespixmapitem.cpp \ src/ui/cursortilerect.cpp \ @@ -136,7 +137,8 @@ HEADERS += include/core/block.h \ include/scripting.h \ include/settings.h \ include/log.h \ - include/ui/newtilesetdialog.h + include/ui/newtilesetdialog.h \ + include/ui/overlay.h FORMS += forms/mainwindow.ui \ forms/eventpropertiesframe.ui \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 05ebd6ea..39b44058 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -446,6 +446,8 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) { setRecentMap(map_name); updateMapList(); updateTilesetEditor(); + + Scripting::cb_MapOpened(map_name); return true; } @@ -2714,3 +2716,33 @@ void MainWindow::setHeight(int height) { this->editor->map->commit(); this->onMapNeedsRedrawing(); } + +void MainWindow::clearOverlay() { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.clearItems(); +} + +void MainWindow::addText(QString text, int x, int y, QString color, int fontSize) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.addText(text, x, y, color, fontSize); +} + +void MainWindow::addRect(int x, int y, int width, int height, QString color) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, false); +} + +void MainWindow::addFilledRect(int x, int y, int width, int height, QString color) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); +} + +void MainWindow::addImage(int x, int y, QString filepath) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.addImage(x, y, filepath); +} diff --git a/src/scripting.cpp b/src/scripting.cpp index f946bef5..8b64f296 100644 --- a/src/scripting.cpp +++ b/src/scripting.cpp @@ -3,6 +3,7 @@ QMap callbackFunctions = { {OnBlockChanged, "on_block_changed"}, + {OnMapOpened, "on_map_opened"}, }; Scripting *instance = nullptr; @@ -65,9 +66,18 @@ void Scripting::cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock instance->invokeCallback(OnBlockChanged, args); } +void Scripting::cb_MapOpened(QString mapName) { + if (!instance) return; + + QJSValueList args { + mapName, + }; + instance->invokeCallback(OnMapOpened, args); +} + QJSValue Scripting::fromBlock(Block block) { QJSValue obj = instance->engine->newObject(); - obj.setProperty("tile", block.tile); + obj.setProperty("metatileId", block.tile); obj.setProperty("collision", block.collision); obj.setProperty("elevation", block.elevation); obj.setProperty("rawValue", block.rawValue()); diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index 778436df..19cf1a76 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -15,3 +15,9 @@ void GraphicsView::mouseMoveEvent(QMouseEvent *event) { void GraphicsView::mouseReleaseEvent(QMouseEvent *event) { QGraphicsView::mouseReleaseEvent(event); } + +void GraphicsView::drawForeground(QPainter *painter, const QRectF &rect) { + for (auto item : this->overlay.getItems()) { + item->render(painter); + } +} diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp new file mode 100644 index 00000000..f1be1a94 --- /dev/null +++ b/src/ui/overlay.cpp @@ -0,0 +1,46 @@ +#include "overlay.h" + +void OverlayText::render(QPainter *painter) { + QFont font = painter->font(); + font.setPixelSize(this->fontSize); + painter->setFont(font); + painter->setPen(this->color); + painter->drawText(this->x, this->y, this->text); +} + +void OverlayRect::render(QPainter *painter) { + if (this->filled) { + painter->fillRect(this->x, this->y, this->width, this->height, this->color); + } else { + painter->setPen(this->color); + painter->drawRect(this->x, this->y, this->width, this->height); + } +} + +void OverlayImage::render(QPainter *painter) { + painter->drawImage(this->x, this->y, this->image); +} + +void Overlay::clearItems() { + for (auto item : this->items) { + delete item; + } + this->items.clear(); +} + +QList Overlay::getItems() { + return this->items; +} + +void Overlay::addText(QString text, int x, int y, QString color, int fontSize) { + this->items.append(new OverlayText(text, x, y, QColor(color), fontSize)); +} + +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)); +} + +void Overlay::addImage(int x, int y, QString filepath) { + this->items.append(new OverlayImage(x, y, QImage(filepath))); +} + diff --git a/test_script.js b/test_script.js index 4cf9362e..ed74953e 100644 --- a/test_script.js +++ b/test_script.js @@ -1,20 +1,17 @@ -function randInt(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min)) + min; -} - -const grassTiles = [0x8, 0x9, 0x10, 0x11]; - // Porymap callback when a block is painted. export function on_block_changed(x, y, prevBlock, newBlock) { - try { - // if (grassTiles.indexOf(newBlock.tile) != -1) { - // const i = randInt(0, grassTiles.length); - // map.setBlock(x, y, grassTiles[i], newBlock.collision, newBlock.elevation); - // } - console.log("ran", map.getWidth(), map.getHeight()); - } catch(err) { - console.log(err); - } + map.clearOverlay() + map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#80FF0040") + map.addRect(10, 10, 100, 30, "#FF00FF") + map.addImage(80, 80, "D:\\cygwin64\\home\\huder\\scratch\\github-avatar.png") + map.addText(`coords ${x}, ${y}`, 20, 20, "#00FF00", 24) + map.addText(`block ${prevBlock.metatileId}`, 20, 60, "#00FFFF", 18) + console.log("ran", x, y) +} + +// Porymap callback when a map is opened. +export function on_map_opened(mapName) { + map.clearOverlay() + map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#4000FF00") + console.log(`opened ${mapName}`) }