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

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