From b8d0268d5d49676a8d153db9bfad95506ccd2ace Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Fri, 8 May 2020 09:59:01 -0500 Subject: [PATCH] Move script api into separate file --- include/mainwindow.h | 1 + porymap.pro | 1 + src/mainwindow.cpp | 459 ---------------------------------- src/mainwindow_scriptapi.cpp | 462 +++++++++++++++++++++++++++++++++++ 4 files changed, 464 insertions(+), 459 deletions(-) create mode 100644 src/mainwindow_scriptapi.cpp diff --git a/include/mainwindow.h b/include/mainwindow.h index eed46769..764a0a9e 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -38,6 +38,7 @@ public: MainWindow(const MainWindow &) = delete; MainWindow & operator = (const MainWindow &) = delete; + // Scripting API Q_INVOKABLE QJSValue getBlock(int x, int y); Q_INVOKABLE void setBlock(int x, int y, int tile, int collision, int elevation); Q_INVOKABLE void setBlocksFromSelection(int x, int y); diff --git a/porymap.pro b/porymap.pro index 2a138e30..907ee041 100644 --- a/porymap.pro +++ b/porymap.pro @@ -31,6 +31,7 @@ SOURCES += src/core/block.cpp \ src/core/regionmap.cpp \ src/core/wildmoninfo.cpp \ src/lib/orderedjson.cpp \ + src/mainwindow_scriptapi.cpp \ src/ui/aboutporymap.cpp \ src/ui/bordermetatilespixmapitem.cpp \ src/ui/collisionpixmapitem.cpp \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 8fc9e8d9..f4328f1a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2563,462 +2563,3 @@ void MainWindow::closeEvent(QCloseEvent *event) { QMainWindow::closeEvent(event); } - -QJSValue MainWindow::getBlock(int x, int y) { - if (!this->editor || !this->editor->map) - return QJSValue(); - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return Scripting::fromBlock(Block()); - } - return Scripting::fromBlock(*block); -} - -void MainWindow::setBlock(int x, int y, int tile, int collision, int elevation) { - if (!this->editor || !this->editor->map) - return; - this->editor->map->setBlock(x, y, Block(tile, collision, elevation)); -} - -void MainWindow::setBlocksFromSelection(int x, int y) { - if (this->editor && this->editor->map_item) { - this->editor->map_item->paintNormal(x, y, true); - } -} - -int MainWindow::getMetatileId(int x, int y) { - if (!this->editor || !this->editor->map) - return 0; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return 0; - } - return block->tile; -} - -void MainWindow::setMetatileId(int x, int y, int metatileId) { - if (!this->editor || !this->editor->map) - return; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return; - } - this->editor->map->setBlock(x, y, Block(metatileId, block->collision, block->elevation)); -} - -int MainWindow::getCollision(int x, int y) { - if (!this->editor || !this->editor->map) - return 0; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return 0; - } - return block->collision; -} - -void MainWindow::setCollision(int x, int y, int collision) { - if (!this->editor || !this->editor->map) - return; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return; - } - this->editor->map->setBlock(x, y, Block(block->tile, collision, block->elevation)); -} - -int MainWindow::getElevation(int x, int y) { - if (!this->editor || !this->editor->map) - return 0; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return 0; - } - return block->elevation; -} - -void MainWindow::setElevation(int x, int y, int elevation) { - if (!this->editor || !this->editor->map) - return; - Block *block = this->editor->map->getBlock(x, y); - if (!block) { - return; - } - this->editor->map->setBlock(x, y, Block(block->tile, block->collision, elevation)); -} - -void MainWindow::bucketFill(int x, int y, int metatileId) { - if (!this->editor || !this->editor->map) - return; - this->editor->map_item->floodFill(x, y, metatileId, true); -} - -void MainWindow::bucketFillFromSelection(int x, int y) { - if (!this->editor || !this->editor->map) - return; - this->editor->map_item->floodFill(x, y, true); -} - -void MainWindow::magicFill(int x, int y, int metatileId) { - if (!this->editor || !this->editor->map) - return; - this->editor->map_item->magicFill(x, y, metatileId, true); -} - -void MainWindow::magicFillFromSelection(int x, int y) { - if (!this->editor || !this->editor->map) - return; - this->editor->map_item->magicFill(x, y, true); -} - -void MainWindow::shift(int xDelta, int yDelta) { - if (!this->editor || !this->editor->map) - return; - this->editor->map_item->shift(xDelta, yDelta); -} - -QJSValue MainWindow::getDimensions() { - if (!this->editor || !this->editor->map) - return QJSValue(); - return Scripting::dimensions(this->editor->map->getWidth(), this->editor->map->getHeight()); -} - -int MainWindow::getWidth() { - if (!this->editor || !this->editor->map) - return 0; - return this->editor->map->getWidth(); -} - -int MainWindow::getHeight() { - if (!this->editor || !this->editor->map) - return 0; - return this->editor->map->getHeight(); -} - -void MainWindow::setDimensions(int width, int height) { - if (!this->editor || !this->editor->map) - return; - if (!MainWindow::mapDimensionsValid(width, height)) - return; - this->editor->map->setDimensions(width, height); - this->editor->map->commit(); - this->onMapNeedsRedrawing(); -} - -void MainWindow::setWidth(int width) { - if (!this->editor || !this->editor->map) - return; - if (!MainWindow::mapDimensionsValid(width, this->editor->map->getHeight())) - return; - this->editor->map->setDimensions(width, this->editor->map->getHeight()); - this->editor->map->commit(); - this->onMapNeedsRedrawing(); -} - -void MainWindow::setHeight(int height) { - if (!this->editor || !this->editor->map) - return; - if (!MainWindow::mapDimensionsValid(this->editor->map->getWidth(), height)) - return; - this->editor->map->setDimensions(this->editor->map->getWidth(), height); - this->editor->map->commit(); - this->onMapNeedsRedrawing(); -} - -void MainWindow::clearOverlay() { - if (!this->ui || !this->ui->graphicsView_Map) - return; - this->ui->graphicsView_Map->overlay.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) - return; - this->ui->graphicsView_Map->overlay.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) - return; - this->ui->graphicsView_Map->overlay.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) - return; - this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); - this->ui->graphicsView_Map->scene()->update(); -} - -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); - this->ui->graphicsView_Map->scene()->update(); -} - -void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { - if (this->tilesetEditor) { - this->tilesetEditor->setTilesets(this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label); - } - this->editor->metatile_selector_item->draw(); - this->editor->selected_border_metatiles_item->draw(); - this->editor->map_item->draw(true); - this->editor->updateMapBorder(); - this->editor->updateMapConnections(); - this->editor->project->saveTilesetPalettes(tileset); -} - -void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout) - return; - if (paletteIndex >= tileset->palettes->size()) - return; - if (colors.size() != 16) - return; - - for (int i = 0; i < 16; i++) { - if (colors[i].size() != 3) - continue; - (*tileset->palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); - (*tileset->palettePreviews)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); - } -} - -void MainWindow::setPrimaryTilesetPalette(int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return; - this->setTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex, colors); - this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); -} - -void MainWindow::setPrimaryTilesetPalettes(QList>> palettes) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return; - for (int i = 0; i < palettes.size(); i++) { - this->setTilesetPalette(this->editor->map->layout->tileset_primary, i, palettes[i]); - } - this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); -} - -void MainWindow::setSecondaryTilesetPalette(int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return; - this->setTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex, colors); - this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); -} - -void MainWindow::setSecondaryTilesetPalettes(QList>> palettes) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return; - for (int i = 0; i < palettes.size(); i++) { - this->setTilesetPalette(this->editor->map->layout->tileset_secondary, i, palettes[i]); - } - this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); -} - -QJSValue MainWindow::getTilesetPalette(QList> *palettes, int paletteIndex) { - if (paletteIndex >= palettes->size()) - return QJSValue(); - - QList> palette; - for (auto color : palettes->value(paletteIndex)) { - palette.append(QList({qRed(color), qGreen(color), qBlue(color)})); - } - return Scripting::getEngine()->toScriptValue(palette); -} - -QJSValue MainWindow::getTilesetPalettes(QList> *palettes) { - QList>> outPalettes; - for (int i = 0; i < palettes->size(); i++) { - QList> colors; - for (auto color : palettes->value(i)) { - colors.append(QList({qRed(color), qGreen(color), qBlue(color)})); - } - outPalettes.append(colors); - } - return Scripting::getEngine()->toScriptValue(outPalettes); -} - -QJSValue MainWindow::getPrimaryTilesetPalette(int paletteIndex) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettes, paletteIndex); -} - -QJSValue MainWindow::getPrimaryTilesetPalettes() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return QJSValue(); - return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettes); -} - -QJSValue MainWindow::getSecondaryTilesetPalette(int paletteIndex) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettes, paletteIndex); -} - -QJSValue MainWindow::getSecondaryTilesetPalettes() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return QJSValue(); - return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettes); -} - -void MainWindow::refreshAfterPalettePreviewChange() { - this->editor->metatile_selector_item->draw(); - this->editor->selected_border_metatiles_item->draw(); - this->editor->map_item->draw(true); - this->editor->updateMapBorder(); - this->editor->updateMapConnections(); -} - -void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout) - return; - if (paletteIndex >= tileset->palettePreviews->size()) - return; - if (colors.size() != 16) - return; - - for (int i = 0; i < 16; i++) { - if (colors[i].size() != 3) - continue; - auto palettes = tileset->palettePreviews; - (*palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); - } -} - -void MainWindow::setPrimaryTilesetPalettePreview(int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return; - this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, paletteIndex, colors); - this->refreshAfterPalettePreviewChange(); -} - -void MainWindow::setPrimaryTilesetPalettesPreview(QList>> palettes) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return; - for (int i = 0; i < palettes.size(); i++) { - this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, i, palettes[i]); - } - this->refreshAfterPalettePreviewChange(); -} - -void MainWindow::setSecondaryTilesetPalettePreview(int paletteIndex, QList> colors) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return; - this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors); - this->refreshAfterPalettePreviewChange(); -} - -void MainWindow::setSecondaryTilesetPalettesPreview(QList>> palettes) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return; - for (int i = 0; i < palettes.size(); i++) { - this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, i, palettes[i]); - } - this->refreshAfterPalettePreviewChange(); -} - -QJSValue MainWindow::getPrimaryTilesetPalettePreview(int paletteIndex) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettePreviews, paletteIndex); -} - -QJSValue MainWindow::getPrimaryTilesetPalettesPreview() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return QJSValue(); - return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettePreviews); -} - -QJSValue MainWindow::getSecondaryTilesetPalettePreview(int paletteIndex) { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettePreviews, paletteIndex); -} - -QJSValue MainWindow::getSecondaryTilesetPalettesPreview() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return QJSValue(); - return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettePreviews); -} - -QString MainWindow::getPrimaryTileset() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) - return QString(); - return this->editor->map->layout->tileset_primary->name; -} - -QString MainWindow::getSecondaryTileset() { - if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) - return QString(); - return this->editor->map->layout->tileset_secondary->name; -} - -void MainWindow::setPrimaryTileset(QString tileset) { - this->on_comboBox_PrimaryTileset_currentTextChanged(tileset); -} - -void MainWindow::setSecondaryTileset(QString tileset) { - this->on_comboBox_SecondaryTileset_currentTextChanged(tileset); -} - -void MainWindow::setGridVisibility(bool visible) { - this->ui->checkBox_ToggleGrid->setChecked(visible); -} - -bool MainWindow::getGridVisibility() { - return this->ui->checkBox_ToggleGrid->isChecked(); -} - -void MainWindow::setBorderVisibility(bool visible) { - this->editor->toggleBorderVisibility(visible); -} - -bool MainWindow::getBorderVisibility() { - return this->ui->checkBox_ToggleBorder->isChecked(); -} - -void MainWindow::setSmartPathsEnabled(bool visible) { - this->ui->checkBox_smartPaths->setChecked(visible); -} - -bool MainWindow::getSmartPathsEnabled() { - return this->ui->checkBox_smartPaths->isChecked(); -} - -void MainWindow::registerAction(QString functionName, QString actionName, QString shortcut) { - if (!this->ui || !this->ui->menuTools) - return; - - Scripting::registerAction(functionName, actionName); - if (Scripting::numRegisteredActions() == 1) { - this->ui->menuTools->addSection("Custom Actions"); - } - QAction *action = this->ui->menuTools->addAction(actionName, [actionName](){ - Scripting::invokeAction(actionName); - }); - if (!shortcut.isEmpty()) { - action->setShortcut(QKeySequence(shortcut)); - } -} - -void MainWindow::setTimeout(QJSValue callback, int milliseconds) { - if (!callback.isCallable() || milliseconds < 0) - return; - - QTimer *timer = new QTimer(0); - connect(timer, &QTimer::timeout, [=](){ - this->invokeCallback(callback); - }); - connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater())); - timer->setSingleShot(true); - timer->start(milliseconds); -} - -void MainWindow::invokeCallback(QJSValue callback) { - callback.call(); -} diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp new file mode 100644 index 00000000..f321595e --- /dev/null +++ b/src/mainwindow_scriptapi.cpp @@ -0,0 +1,462 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "scripting.h" + +QJSValue MainWindow::getBlock(int x, int y) { + if (!this->editor || !this->editor->map) + return QJSValue(); + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return Scripting::fromBlock(Block()); + } + return Scripting::fromBlock(*block); +} + +void MainWindow::setBlock(int x, int y, int tile, int collision, int elevation) { + if (!this->editor || !this->editor->map) + return; + this->editor->map->setBlock(x, y, Block(tile, collision, elevation)); +} + +void MainWindow::setBlocksFromSelection(int x, int y) { + if (this->editor && this->editor->map_item) { + this->editor->map_item->paintNormal(x, y, true); + } +} + +int MainWindow::getMetatileId(int x, int y) { + if (!this->editor || !this->editor->map) + return 0; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return 0; + } + return block->tile; +} + +void MainWindow::setMetatileId(int x, int y, int metatileId) { + if (!this->editor || !this->editor->map) + return; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return; + } + this->editor->map->setBlock(x, y, Block(metatileId, block->collision, block->elevation)); +} + +int MainWindow::getCollision(int x, int y) { + if (!this->editor || !this->editor->map) + return 0; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return 0; + } + return block->collision; +} + +void MainWindow::setCollision(int x, int y, int collision) { + if (!this->editor || !this->editor->map) + return; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return; + } + this->editor->map->setBlock(x, y, Block(block->tile, collision, block->elevation)); +} + +int MainWindow::getElevation(int x, int y) { + if (!this->editor || !this->editor->map) + return 0; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return 0; + } + return block->elevation; +} + +void MainWindow::setElevation(int x, int y, int elevation) { + if (!this->editor || !this->editor->map) + return; + Block *block = this->editor->map->getBlock(x, y); + if (!block) { + return; + } + this->editor->map->setBlock(x, y, Block(block->tile, block->collision, elevation)); +} + +void MainWindow::bucketFill(int x, int y, int metatileId) { + if (!this->editor || !this->editor->map) + return; + this->editor->map_item->floodFill(x, y, metatileId, true); +} + +void MainWindow::bucketFillFromSelection(int x, int y) { + if (!this->editor || !this->editor->map) + return; + this->editor->map_item->floodFill(x, y, true); +} + +void MainWindow::magicFill(int x, int y, int metatileId) { + if (!this->editor || !this->editor->map) + return; + this->editor->map_item->magicFill(x, y, metatileId, true); +} + +void MainWindow::magicFillFromSelection(int x, int y) { + if (!this->editor || !this->editor->map) + return; + this->editor->map_item->magicFill(x, y, true); +} + +void MainWindow::shift(int xDelta, int yDelta) { + if (!this->editor || !this->editor->map) + return; + this->editor->map_item->shift(xDelta, yDelta); +} + +QJSValue MainWindow::getDimensions() { + if (!this->editor || !this->editor->map) + return QJSValue(); + return Scripting::dimensions(this->editor->map->getWidth(), this->editor->map->getHeight()); +} + +int MainWindow::getWidth() { + if (!this->editor || !this->editor->map) + return 0; + return this->editor->map->getWidth(); +} + +int MainWindow::getHeight() { + if (!this->editor || !this->editor->map) + return 0; + return this->editor->map->getHeight(); +} + +void MainWindow::setDimensions(int width, int height) { + if (!this->editor || !this->editor->map) + return; + if (!MainWindow::mapDimensionsValid(width, height)) + return; + this->editor->map->setDimensions(width, height); + this->editor->map->commit(); + this->onMapNeedsRedrawing(); +} + +void MainWindow::setWidth(int width) { + if (!this->editor || !this->editor->map) + return; + if (!MainWindow::mapDimensionsValid(width, this->editor->map->getHeight())) + return; + this->editor->map->setDimensions(width, this->editor->map->getHeight()); + this->editor->map->commit(); + this->onMapNeedsRedrawing(); +} + +void MainWindow::setHeight(int height) { + if (!this->editor || !this->editor->map) + return; + if (!MainWindow::mapDimensionsValid(this->editor->map->getWidth(), height)) + return; + this->editor->map->setDimensions(this->editor->map->getWidth(), height); + this->editor->map->commit(); + this->onMapNeedsRedrawing(); +} + +void MainWindow::clearOverlay() { + if (!this->ui || !this->ui->graphicsView_Map) + return; + this->ui->graphicsView_Map->overlay.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) + return; + this->ui->graphicsView_Map->overlay.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) + return; + this->ui->graphicsView_Map->overlay.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) + return; + this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); + this->ui->graphicsView_Map->scene()->update(); +} + +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); + this->ui->graphicsView_Map->scene()->update(); +} + +void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { + if (this->tilesetEditor) { + this->tilesetEditor->setTilesets(this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label); + } + this->editor->metatile_selector_item->draw(); + this->editor->selected_border_metatiles_item->draw(); + this->editor->map_item->draw(true); + this->editor->updateMapBorder(); + this->editor->updateMapConnections(); + this->editor->project->saveTilesetPalettes(tileset); +} + +void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout) + return; + if (paletteIndex >= tileset->palettes->size()) + return; + if (colors.size() != 16) + return; + + for (int i = 0; i < 16; i++) { + if (colors[i].size() != 3) + continue; + (*tileset->palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + (*tileset->palettePreviews)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + } +} + +void MainWindow::setPrimaryTilesetPalette(int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + this->setTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex, colors); + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); +} + +void MainWindow::setPrimaryTilesetPalettes(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalette(this->editor->map->layout->tileset_primary, i, palettes[i]); + } + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); +} + +void MainWindow::setSecondaryTilesetPalette(int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + this->setTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex, colors); + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); +} + +void MainWindow::setSecondaryTilesetPalettes(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalette(this->editor->map->layout->tileset_secondary, i, palettes[i]); + } + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); +} + +QJSValue MainWindow::getTilesetPalette(QList> *palettes, int paletteIndex) { + if (paletteIndex >= palettes->size()) + return QJSValue(); + + QList> palette; + for (auto color : palettes->value(paletteIndex)) { + palette.append(QList({qRed(color), qGreen(color), qBlue(color)})); + } + return Scripting::getEngine()->toScriptValue(palette); +} + +QJSValue MainWindow::getTilesetPalettes(QList> *palettes) { + QList>> outPalettes; + for (int i = 0; i < palettes->size(); i++) { + QList> colors; + for (auto color : palettes->value(i)) { + colors.append(QList({qRed(color), qGreen(color), qBlue(color)})); + } + outPalettes.append(colors); + } + return Scripting::getEngine()->toScriptValue(outPalettes); +} + +QJSValue MainWindow::getPrimaryTilesetPalette(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettes, paletteIndex); +} + +QJSValue MainWindow::getPrimaryTilesetPalettes() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettes); +} + +QJSValue MainWindow::getSecondaryTilesetPalette(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettes, paletteIndex); +} + +QJSValue MainWindow::getSecondaryTilesetPalettes() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettes); +} + +void MainWindow::refreshAfterPalettePreviewChange() { + this->editor->metatile_selector_item->draw(); + this->editor->selected_border_metatiles_item->draw(); + this->editor->map_item->draw(true); + this->editor->updateMapBorder(); + this->editor->updateMapConnections(); +} + +void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout) + return; + if (paletteIndex >= tileset->palettePreviews->size()) + return; + if (colors.size() != 16) + return; + + for (int i = 0; i < 16; i++) { + if (colors[i].size() != 3) + continue; + auto palettes = tileset->palettePreviews; + (*palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + } +} + +void MainWindow::setPrimaryTilesetPalettePreview(int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, paletteIndex, colors); + this->refreshAfterPalettePreviewChange(); +} + +void MainWindow::setPrimaryTilesetPalettesPreview(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, i, palettes[i]); + } + this->refreshAfterPalettePreviewChange(); +} + +void MainWindow::setSecondaryTilesetPalettePreview(int paletteIndex, QList> colors) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors); + this->refreshAfterPalettePreviewChange(); +} + +void MainWindow::setSecondaryTilesetPalettesPreview(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, i, palettes[i]); + } + this->refreshAfterPalettePreviewChange(); +} + +QJSValue MainWindow::getPrimaryTilesetPalettePreview(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettePreviews, paletteIndex); +} + +QJSValue MainWindow::getPrimaryTilesetPalettesPreview() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettePreviews); +} + +QJSValue MainWindow::getSecondaryTilesetPalettePreview(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettePreviews, paletteIndex); +} + +QJSValue MainWindow::getSecondaryTilesetPalettesPreview() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettePreviews); +} + +QString MainWindow::getPrimaryTileset() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QString(); + return this->editor->map->layout->tileset_primary->name; +} + +QString MainWindow::getSecondaryTileset() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QString(); + return this->editor->map->layout->tileset_secondary->name; +} + +void MainWindow::setPrimaryTileset(QString tileset) { + this->on_comboBox_PrimaryTileset_currentTextChanged(tileset); +} + +void MainWindow::setSecondaryTileset(QString tileset) { + this->on_comboBox_SecondaryTileset_currentTextChanged(tileset); +} + +void MainWindow::setGridVisibility(bool visible) { + this->ui->checkBox_ToggleGrid->setChecked(visible); +} + +bool MainWindow::getGridVisibility() { + return this->ui->checkBox_ToggleGrid->isChecked(); +} + +void MainWindow::setBorderVisibility(bool visible) { + this->editor->toggleBorderVisibility(visible); +} + +bool MainWindow::getBorderVisibility() { + return this->ui->checkBox_ToggleBorder->isChecked(); +} + +void MainWindow::setSmartPathsEnabled(bool visible) { + this->ui->checkBox_smartPaths->setChecked(visible); +} + +bool MainWindow::getSmartPathsEnabled() { + return this->ui->checkBox_smartPaths->isChecked(); +} + +void MainWindow::registerAction(QString functionName, QString actionName, QString shortcut) { + if (!this->ui || !this->ui->menuTools) + return; + + Scripting::registerAction(functionName, actionName); + if (Scripting::numRegisteredActions() == 1) { + this->ui->menuTools->addSection("Custom Actions"); + } + QAction *action = this->ui->menuTools->addAction(actionName, [actionName](){ + Scripting::invokeAction(actionName); + }); + if (!shortcut.isEmpty()) { + action->setShortcut(QKeySequence(shortcut)); + } +} + +void MainWindow::setTimeout(QJSValue callback, int milliseconds) { + if (!callback.isCallable() || milliseconds < 0) + return; + + QTimer *timer = new QTimer(0); + connect(timer, &QTimer::timeout, [=](){ + this->invokeCallback(callback); + }); + connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater())); + timer->setSingleShot(true); + timer->start(milliseconds); +} + +void MainWindow::invokeCallback(QJSValue callback) { + callback.call(); +}