Reorganize overlay functions under MapView

This commit is contained in:
GriffinR 2022-09-04 21:17:50 -04:00
parent b25dbb849e
commit a3dadadd5b
7 changed files with 157 additions and 213 deletions

View file

@ -42,6 +42,8 @@ public:
MainWindow(const MainWindow &) = delete;
MainWindow & operator = (const MainWindow &) = delete;
MapView *getMapView();
// Scripting API
Q_INVOKABLE QJSValue getBlock(int x, int y);
void tryRedrawMapArea(bool forceRedraw);
@ -76,40 +78,6 @@ public:
Q_INVOKABLE void setBorderDimensions(int width, int height);
Q_INVOKABLE void setBorderWidth(int width);
Q_INVOKABLE void setBorderHeight(int height);
Q_INVOKABLE void clearOverlay(int layer = 0);
Q_INVOKABLE void clearOverlays();
Q_INVOKABLE void hideOverlay(int layer = 0);
Q_INVOKABLE void hideOverlays();
Q_INVOKABLE void showOverlay(int layer = 0);
Q_INVOKABLE void showOverlays();
Q_INVOKABLE bool getOverlayVisibility(int layer = 0);
Q_INVOKABLE void setOverlayVisibility(bool visible, int layer = 0);
Q_INVOKABLE void setOverlaysVisibility(bool visible);
Q_INVOKABLE int getOverlayX(int layer = 0);
Q_INVOKABLE int getOverlayY(int layer = 0);
Q_INVOKABLE void setOverlayX(int x, int layer = 0);
Q_INVOKABLE void setOverlayY(int y, int layer = 0);
Q_INVOKABLE void setOverlaysX(int x);
Q_INVOKABLE void setOverlaysY(int y);
Q_INVOKABLE QJSValue getOverlayPosition(int layer = 0);
Q_INVOKABLE void setOverlayPosition(int x, int y, int layer = 0);
Q_INVOKABLE void setOverlaysPosition(int x, int y);
Q_INVOKABLE void moveOverlay(int deltaX, int deltaY, int layer = 0);
Q_INVOKABLE void moveOverlays(int deltaX, int deltaY);
Q_INVOKABLE int getOverlayOpacity(int layer);
Q_INVOKABLE void setOverlayOpacity(int opacity, int layer = 0);
Q_INVOKABLE void setOverlaysOpacity(int opacity);
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 layer = 0, bool useCache = true);
Q_INVOKABLE void createImage(int x, int y, QString filepath,
int width = -1, int height = -1, unsigned offset = 0,
qreal hScale = 1, qreal vScale = 1, int paletteId = -1, bool setTransparency = false,
int layer = 0, bool useCache = true);
Q_INVOKABLE void addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addTileImage(int x, int y, QJSValue tileObj, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addMetatileImage(int x, int y, int metatileId, 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);
@ -226,7 +194,6 @@ public:
Q_INVOKABLE int getFloorNumber();
Q_INVOKABLE void setFloorNumber(int floorNumber);
private slots:
void on_action_Open_Project_triggered();
void on_action_Reload_Project_triggered();

View file

@ -1,24 +1,58 @@
#ifndef MAPVIEW_H
#define MAPVIEW_H
#include <QJSValue>
#include "graphicsview.h"
#include "overlay.h"
class MapView : public GraphicsView
{
Q_OBJECT
public:
MapView() : GraphicsView() {}
MapView(QWidget *parent) : GraphicsView(parent) {}
Overlay * getOverlay(int layer);
void clearOverlays();
void setOverlaysHidden(bool hidden);
void setOverlaysX(int x);
void setOverlaysY(int y);
void setOverlaysPosition(int x, int y);
void moveOverlays(int deltaX, int deltaY);
void setOverlaysOpacity(int opacity);
public:
Overlay * getOverlay(int layer);
void clearOverlayMap();
// Overlay scripting API
Q_INVOKABLE void clear(int layer);
Q_INVOKABLE void clear();
Q_INVOKABLE void hide(int layer);
Q_INVOKABLE void hide();
Q_INVOKABLE void show(int layer);
Q_INVOKABLE void show();
Q_INVOKABLE bool getVisibility(int layer = 0);
Q_INVOKABLE void setVisibility(bool visible, int layer);
Q_INVOKABLE void setVisibility(bool visible);
Q_INVOKABLE int getX(int layer = 0);
Q_INVOKABLE int getY(int layer = 0);
Q_INVOKABLE void setX(int x, int layer);
Q_INVOKABLE void setX(int x);
Q_INVOKABLE void setY(int y, int layer);
Q_INVOKABLE void setY(int y);
Q_INVOKABLE QJSValue getPosition(int layer = 0);
Q_INVOKABLE void setPosition(int x, int y, int layer);
Q_INVOKABLE void setPosition(int x, int y);
Q_INVOKABLE void move(int deltaX, int deltaY, int layer);
Q_INVOKABLE void move(int deltaX, int deltaY);
Q_INVOKABLE int getOpacity(int layer = 0);
Q_INVOKABLE void setOpacity(int opacity, int layer);
Q_INVOKABLE void setOpacity(int opacity);
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 layer = 0, bool useCache = true);
Q_INVOKABLE void createImage(int x, int y, QString filepath,
int width = -1, int height = -1, unsigned offset = 0,
qreal hScale = 1, qreal vScale = 1, int paletteId = -1, bool setTransparency = false,
int layer = 0, bool useCache = true);
Q_INVOKABLE void addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addTileImage(int x, int y, QJSValue tileObj, bool setTransparency = false, int layer = 0);
Q_INVOKABLE void addMetatileImage(int x, int y, int metatileId, bool setTransparency = false, int layer = 0);
private:
QMap<int, Overlay*> overlayMap;
protected:
void drawForeground(QPainter *painter, const QRectF &rect);

View file

@ -137,6 +137,7 @@ HEADERS += include/core/block.h \
include/ui/graphicsview.h \
include/ui/imageproviders.h \
include/ui/mappixmapitem.h \
include/ui/mapview.h \
include/ui/regionmappixmapitem.h \
include/ui/citymappixmapitem.h \
include/ui/mapsceneeventfilter.h \

View file

@ -604,7 +604,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->clearOverlays();
this->ui->graphicsView_Map->clearOverlayMap();
}
porymapConfig.setRecentProject(dir);
setWindowDisabled(!openProject(dir));
@ -3236,3 +3236,7 @@ void MainWindow::closeEvent(QCloseEvent *event) {
QMainWindow::closeEvent(event);
}
MapView *MainWindow::getMapView() {
return this->ui->graphicsView_Map;
}

View file

@ -1,195 +1,162 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mapview.h"
#include "scripting.h"
#include "config.h"
#include "imageproviders.h"
void MainWindow::clearOverlay(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->clearItems();
this->ui->graphicsView_Map->scene()->update();
void MapView::clear(int layer) {
this->getOverlay(layer)->clearItems();
this->scene()->update();
}
void MainWindow::clearOverlays() {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->clearOverlays();
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, clear all layers
void MapView::clear() {
this->clearOverlayMap();
this->scene()->update();
}
void MainWindow::hideOverlay(int layer) {
this->setOverlayVisibility(false, layer);
void MapView::hide(int layer) {
this->setVisibility(false, layer);
}
void MainWindow::hideOverlays() {
this->setOverlaysVisibility(false);
// Overload. No layer provided, hide all layers
void MapView::hide() {
this->setVisibility(false);
}
void MainWindow::showOverlay(int layer) {
this->setOverlayVisibility(true, layer);
void MapView::show(int layer) {
this->setVisibility(true, layer);
}
void MainWindow::showOverlays() {
this->setOverlaysVisibility(true);
// Overload. No layer provided, show all layers
void MapView::show() {
this->setVisibility(true);
}
bool MainWindow::getOverlayVisibility(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return false;
return !(this->ui->graphicsView_Map->getOverlay(layer)->getHidden());
bool MapView::getVisibility(int layer) {
return !(this->getOverlay(layer)->getHidden());
}
void MainWindow::setOverlayVisibility(bool visible, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->setHidden(!visible);
this->ui->graphicsView_Map->scene()->update();
void MapView::setVisibility(bool visible, int layer) {
this->getOverlay(layer)->setHidden(!visible);
this->scene()->update();
}
void MainWindow::setOverlaysVisibility(bool visible) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->setOverlaysHidden(!visible);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, set visibility of all layers
void MapView::setVisibility(bool visible) {
foreach (Overlay * layer, this->overlayMap)
layer->setHidden(!visible);
this->scene()->update();
}
int MainWindow::getOverlayX(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return 0;
return this->ui->graphicsView_Map->getOverlay(layer)->getX();
int MapView::getX(int layer) {
return this->getOverlay(layer)->getX();
}
int MainWindow::getOverlayY(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return 0;
return this->ui->graphicsView_Map->getOverlay(layer)->getY();
int MapView::getY(int layer) {
return this->getOverlay(layer)->getY();
}
void MainWindow::setOverlayX(int x, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->setX(x);
this->ui->graphicsView_Map->scene()->update();
void MapView::setX(int x, int layer) {
this->getOverlay(layer)->setX(x);
this->scene()->update();
}
void MainWindow::setOverlayY(int y, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->setY(y);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, set x of all layers
void MapView::setX(int x) {
foreach (Overlay * layer, this->overlayMap)
layer->setX(x);
this->scene()->update();
}
void MainWindow::setOverlaysX(int x) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->setOverlaysX(x);
this->ui->graphicsView_Map->scene()->update();
void MapView::setY(int y, int layer) {
this->getOverlay(layer)->setY(y);
this->scene()->update();
}
void MainWindow::setOverlaysY(int y) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->setOverlaysY(y);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, set y of all layers
void MapView::setY(int y) {
foreach (Overlay * layer, this->overlayMap)
layer->setY(y);
this->scene()->update();
}
QJSValue MainWindow::getOverlayPosition(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return QJSValue();
Overlay * overlay = this->ui->graphicsView_Map->getOverlay(layer);
QJSValue MapView::getPosition(int layer) {
Overlay * overlay = this->getOverlay(layer);
return Scripting::position(overlay->getX(), overlay->getY());
}
void MainWindow::setOverlayPosition(int x, int y, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->setPosition(x, y);
this->ui->graphicsView_Map->scene()->update();
void MapView::setPosition(int x, int y, int layer) {
this->getOverlay(layer)->setPosition(x, y);
this->scene()->update();
}
void MainWindow::setOverlaysPosition(int x, int y) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->setOverlaysPosition(x, y);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, set position of all layers
void MapView::setPosition(int x, int y) {
foreach (Overlay * layer, this->overlayMap)
layer->setPosition(x, y);
this->scene()->update();
}
void MainWindow::moveOverlay(int deltaX, int deltaY, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->move(deltaX, deltaY);
this->ui->graphicsView_Map->scene()->update();
void MapView::move(int deltaX, int deltaY, int layer) {
this->getOverlay(layer)->move(deltaX, deltaY);
this->scene()->update();
}
void MainWindow::moveOverlays(int deltaX, int deltaY) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->moveOverlays(deltaX, deltaY);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, move all layers
void MapView::move(int deltaX, int deltaY) {
foreach (Overlay * layer, this->overlayMap)
layer->move(deltaX, deltaY);
this->scene()->update();
}
int MainWindow::getOverlayOpacity(int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return 0;
return this->ui->graphicsView_Map->getOverlay(layer)->getOpacity();
int MapView::getOpacity(int layer) {
return this->getOverlay(layer)->getOpacity();
}
void MainWindow::setOverlayOpacity(int opacity, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->setOpacity(opacity);
this->ui->graphicsView_Map->scene()->update();
void MapView::setOpacity(int opacity, int layer) {
this->getOverlay(layer)->setOpacity(opacity);
this->scene()->update();
}
void MainWindow::setOverlaysOpacity(int opacity) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->setOverlaysOpacity(opacity);
this->ui->graphicsView_Map->scene()->update();
// Overload. No layer provided, set opacity of all layers
void MapView::setOpacity(int opacity) {
foreach (Overlay * layer, this->overlayMap)
layer->setOpacity(opacity);
this->scene()->update();
}
void MainWindow::addText(QString text, int x, int y, QString color, int fontSize, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->addText(text, x, y, color, fontSize);
this->ui->graphicsView_Map->scene()->update();
void MapView::addText(QString text, int x, int y, QString color, int fontSize, int layer) {
this->getOverlay(layer)->addText(text, x, y, color, fontSize);
this->scene()->update();
}
void MainWindow::addRect(int x, int y, int width, int height, QString color, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->addRect(x, y, width, height, color, false);
this->ui->graphicsView_Map->scene()->update();
void MapView::addRect(int x, int y, int width, int height, QString color, int layer) {
this->getOverlay(layer)->addRect(x, y, width, height, color, false);
this->scene()->update();
}
void MainWindow::addFilledRect(int x, int y, int width, int height, QString color, int layer) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
this->ui->graphicsView_Map->getOverlay(layer)->addRect(x, y, width, height, color, true);
this->ui->graphicsView_Map->scene()->update();
void MapView::addFilledRect(int x, int y, int width, int height, QString color, int layer) {
this->getOverlay(layer)->addRect(x, y, width, height, color, true);
this->scene()->update();
}
void MainWindow::addImage(int x, int y, QString filepath, int layer, bool useCache) {
if (!this->ui || !this->ui->graphicsView_Map)
return;
if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, useCache))
this->ui->graphicsView_Map->scene()->update();
void MapView::addImage(int x, int y, QString filepath, int layer, bool useCache) {
if (this->getOverlay(layer)->addImage(x, y, filepath, useCache))
this->scene()->update();
}
void MainWindow::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, qreal hScale, qreal vScale, int paletteId, bool setTransparency, int layer, bool useCache) {
if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout
void MapView::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, qreal hScale, qreal vScale, int paletteId, bool setTransparency, int layer, bool useCache) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QList<QRgb> palette;
if (paletteId != -1)
palette = Tileset::getPalette(paletteId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary);
if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, filepath, useCache, width, height, offset, hScale, vScale, palette, setTransparency))
this->ui->graphicsView_Map->scene()->update();
if (this->getOverlay(layer)->addImage(x, y, filepath, useCache, width, height, offset, hScale, vScale, palette, setTransparency))
this->scene()->update();
}
void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) {
if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout
void MapView::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QImage image = getPalettedTileImage(tileId,
@ -199,17 +166,17 @@ void MainWindow::addTileImage(int x, int y, int tileId, bool xflip, bool yflip,
.mirrored(xflip, yflip);
if (setTransparency)
image.setColor(0, qRgba(0, 0, 0, 0));
if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, image))
this->ui->graphicsView_Map->scene()->update();
if (this->getOverlay(layer)->addImage(x, y, image))
this->scene()->update();
}
void MainWindow::addTileImage(int x, int y, QJSValue tileObj, bool setTransparency, int layer) {
void MapView::addTileImage(int x, int y, QJSValue tileObj, bool setTransparency, int layer) {
Tile tile = Scripting::toTile(tileObj);
this->addTileImage(x, y, tile.tileId, tile.xflip, tile.yflip, tile.palette, setTransparency, layer);
}
void MainWindow::addMetatileImage(int x, int y, int metatileId, bool setTransparency, int layer) {
if (!this->ui || !this->ui->graphicsView_Map || !this->editor || !this->editor->map || !this->editor->map->layout
void MapView::addMetatileImage(int x, int y, int metatileId, bool setTransparency, int layer) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QImage image = getMetatileImage(static_cast<uint16_t>(metatileId),
@ -219,6 +186,6 @@ void MainWindow::addMetatileImage(int x, int y, int metatileId, bool setTranspar
this->editor->map->metatileLayerOpacity);
if (setTransparency)
image.setColor(0, qRgba(0, 0, 0, 0));
if (this->ui->graphicsView_Map->getOverlay(layer)->addImage(x, y, image))
this->ui->graphicsView_Map->scene()->update();
if (this->getOverlay(layer)->addImage(x, y, image))
this->scene()->update();
}

View file

@ -33,6 +33,7 @@ Scripting::Scripting(MainWindow *mainWindow) {
this->engine = new QJSEngine(mainWindow);
this->engine->installExtensions(QJSEngine::ConsoleExtension);
this->engine->globalObject().setProperty("map", this->engine->newQObject(mainWindow));
this->engine->globalObject().setProperty("overlay", this->engine->newQObject(mainWindow->getMapView()));
for (QString script : projectConfig.getCustomScripts()) {
this->filepaths.append(script);
}

View file

@ -41,44 +41,14 @@ void MapView::drawForeground(QPainter *painter, const QRectF&) {
editor->cursorMapTileRect->paint(painter, &option, this);
}
void MapView::clearOverlays() {
foreach (Overlay * overlay, this->overlayMap) {
overlay->clearItems();
delete overlay;
void MapView::clearOverlayMap() {
foreach (Overlay * layer, this->overlayMap) {
layer->clearItems();
delete layer;
}
this->overlayMap.clear();
}
void MapView::setOverlaysHidden(bool hidden) {
foreach (Overlay * overlay, this->overlayMap)
overlay->setHidden(hidden);
}
void MapView::setOverlaysX(int x) {
foreach (Overlay * overlay, this->overlayMap)
overlay->setX(x);
}
void MapView::setOverlaysY(int y) {
foreach (Overlay * overlay, this->overlayMap)
overlay->setY(y);
}
void MapView::setOverlaysPosition(int x, int y) {
foreach (Overlay * overlay, this->overlayMap)
overlay->setPosition(x, y);
}
void MapView::setOverlaysOpacity(int opacity) {
foreach (Overlay * overlay, this->overlayMap)
overlay->setOpacity(opacity);
}
void MapView::moveOverlays(int deltaX, int deltaY) {
foreach (Overlay * overlay, this->overlayMap)
overlay->move(deltaX, deltaY);
}
Overlay * MapView::getOverlay(int layer) {
Overlay * overlay = this->overlayMap.value(layer, nullptr);
if (!overlay) {