Implement some overlay functions and on_map_opened callback

This commit is contained in:
Marcus Huderle 2020-05-02 16:25:35 -05:00
parent 95012838fd
commit 567a45b7e4
10 changed files with 210 additions and 19 deletions

View file

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

View file

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

View file

@ -1,6 +1,7 @@
#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H
#include "overlay.h"
#include <QGraphicsView>
#include <QMouseEvent>
@ -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)

87
include/ui/overlay.h Normal file
View file

@ -0,0 +1,87 @@
#ifndef OVERLAY_H
#define OVERLAY_H
#include <QList>
#include <QString>
#include <QColor>
#include <QPainter>
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<OverlayItem*> 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<OverlayItem*> items;
};
#endif // OVERLAY_H

View file

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

View file

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

View file

@ -3,6 +3,7 @@
QMap<CallbackType, QString> 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());

View file

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

46
src/ui/overlay.cpp Normal file
View file

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

View file

@ -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}`)
}