diff --git a/include/mainwindow.h b/include/mainwindow.h index 0ee96c69..131250e4 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -101,13 +101,9 @@ public: Q_INVOKABLE QJSValue getSecondaryTilesetPalettePreview(int paletteIndex); Q_INVOKABLE QJSValue getSecondaryTilesetPalettesPreview(); Q_INVOKABLE int getNumPrimaryTilesetMetatiles(); - Q_INVOKABLE int getMaxPrimaryTilesetMetatiles(); Q_INVOKABLE int getNumSecondaryTilesetMetatiles(); - Q_INVOKABLE int getMaxSecondaryTilesetMetatiles(); Q_INVOKABLE int getNumPrimaryTilesetTiles(); - Q_INVOKABLE int getMaxPrimaryTilesetTiles(); Q_INVOKABLE int getNumSecondaryTilesetTiles(); - Q_INVOKABLE int getMaxSecondaryTilesetTiles(); Q_INVOKABLE bool isPrimaryTileset(QString tilesetName); Q_INVOKABLE bool isSecondaryTileset(QString tilesetName); Q_INVOKABLE QString getPrimaryTileset(); @@ -161,10 +157,6 @@ public: Q_INVOKABLE void setMetatileTiles(int metatileId, QJSValue tilesObj, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE void setMetatileTiles(int metatileId, int tileId, bool xflip, bool yflip, int palette, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE QJSValue getTilePixels(int tileId); - Q_INVOKABLE int getNumTilesInMetatile(); - Q_INVOKABLE int getNumMetatileLayers(); - Q_INVOKABLE QString getBaseGameVersion(); - Q_INVOKABLE QJSValue getPorymapVersion(); Q_INVOKABLE QList getCustomScripts(); Q_INVOKABLE int getMainTab(); Q_INVOKABLE void setMainTab(int index); diff --git a/include/scripting.h b/include/scripting.h index 9b15f018..4558f0e0 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -29,16 +29,9 @@ class Scripting { public: Scripting(MainWindow *mainWindow); - static QJSValue fromBlock(Block block); - static QJSValue fromTile(Tile tile); - static Tile toTile(QJSValue obj); - static QJSValue version(QList versionNums); - static QJSValue dimensions(int width, int height); - static QJSValue position(int x, int y); - static QJSEngine *getEngine(); - static QImage getImage(QString filepath); - static QJSValue dialogInput(QJSValue input, bool selectedOk); static void init(MainWindow *mainWindow); + static void populateGlobalObject(MainWindow *mainWindow); + static QJSEngine *getEngine(); static void registerAction(QString functionName, QString actionName); static int numRegisteredActions(); static void invokeAction(QString actionName); @@ -57,6 +50,14 @@ public: static void cb_MapViewTabChanged(int oldTab, int newTab); static void cb_BorderVisibilityToggled(bool visible); static bool tryErrorJS(QJSValue js); + static QJSValue fromBlock(Block block); + static QJSValue fromTile(Tile tile); + static Tile toTile(QJSValue obj); + static QJSValue version(QList versionNums); + static QJSValue dimensions(int width, int height); + static QJSValue position(int x, int y); + static QImage getImage(QString filepath); + static QJSValue dialogInput(QJSValue input, bool selectedOk); private: QJSEngine *engine; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 474c2315..16a51e9b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -946,6 +946,8 @@ bool MainWindow::loadDataStructures() { && project->readEventGraphics() && project->readSongNames(); + Scripting::populateGlobalObject(this); + return success && loadProjectCombos(); } diff --git a/src/script_api/api_map.cpp b/src/script_api/api_map.cpp index e813f24e..602be5a4 100644 --- a/src/script_api/api_map.cpp +++ b/src/script_api/api_map.cpp @@ -691,7 +691,7 @@ void MainWindow::setMetatileAttributes(int metatileId, int attributes) { } int MainWindow::calculateTileBounds(int * tileStart, int * tileEnd) { - int maxNumTiles = this->getNumTilesInMetatile(); + int maxNumTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; if (*tileEnd >= maxNumTiles || *tileEnd < 0) *tileEnd = maxNumTiles - 1; if (*tileStart >= maxNumTiles || *tileStart < 0) diff --git a/src/script_api/api_util.cpp b/src/script_api/api_util.cpp index 3fe69bf2..6f52434f 100644 --- a/src/script_api/api_util.cpp +++ b/src/script_api/api_util.cpp @@ -2,7 +2,6 @@ #include "ui_mainwindow.h" #include "scripting.h" #include "config.h" -#include "aboutporymap.h" void MainWindow::registerAction(QString functionName, QString actionName, QString shortcut) { if (!this->ui || !this->ui->menuTools) @@ -206,55 +205,6 @@ bool MainWindow::isSecondaryTileset(QString tilesetName) { return this->editor->project->tilesetLabels["secondary"].contains(tilesetName); } - -//================================= -// Here below should be constants -//================================= - - -int MainWindow::getMaxPrimaryTilesetMetatiles() { - if (!this->editor || !this->editor->project) - return 0; - return this->editor->project->getNumMetatilesPrimary(); -} - -int MainWindow::getMaxSecondaryTilesetMetatiles() { - if (!this->editor || !this->editor->project) - return 0; - return this->editor->project->getNumMetatilesTotal() - this->editor->project->getNumMetatilesPrimary(); -} - -int MainWindow::getMaxPrimaryTilesetTiles() { - if (!this->editor || !this->editor->project) - return 0; - return this->editor->project->getNumTilesPrimary(); -} - -int MainWindow::getMaxSecondaryTilesetTiles() { - if (!this->editor || !this->editor->project) - return 0; - return this->editor->project->getNumTilesTotal() - this->editor->project->getNumTilesPrimary(); -} - -int MainWindow::getNumTilesInMetatile() { - return projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; -} - -int MainWindow::getNumMetatileLayers() { - return projectConfig.getTripleLayerMetatilesEnabled() ? 3 : 2; -} - -QString MainWindow::getBaseGameVersion() { - return projectConfig.getBaseGameVersionString(); -} - -QJSValue MainWindow::getPorymapVersion() { - AboutPorymap *window = new AboutPorymap(this); - QJSValue version = Scripting::version(window->getVersionNumbers()); - delete window; - return version; -} - QList MainWindow::getCustomScripts() { return projectConfig.getCustomScripts(); } diff --git a/src/script_api/scripting.cpp b/src/script_api/scripting.cpp index c32aac71..5a3fd097 100644 --- a/src/script_api/scripting.cpp +++ b/src/script_api/scripting.cpp @@ -1,5 +1,7 @@ #include "scripting.h" #include "log.h" +#include "config.h" +#include "aboutporymap.h" QMap callbackFunctions = { {OnProjectOpened, "onProjectOpened"}, @@ -32,8 +34,6 @@ void Scripting::init(MainWindow *mainWindow) { 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); } @@ -54,6 +54,47 @@ void Scripting::loadModules(QStringList moduleFiles) { } } +void Scripting::populateGlobalObject(MainWindow *mainWindow) { + if (!instance || !instance->engine) return; + + instance->engine->globalObject().setProperty("map", instance->engine->newQObject(mainWindow)); + instance->engine->globalObject().setProperty("overlay", instance->engine->newQObject(mainWindow->getMapView())); + + QJSValue constants = instance->engine->newObject(); + + // Get basic tile/metatile information + int numTilesPrimary = Project::getNumTilesPrimary(); + int numTilesTotal = Project::getNumTilesTotal(); + int numMetatilesPrimary = Project::getNumMetatilesPrimary(); + int numMetatilesTotal = Project::getNumMetatilesTotal(); + bool tripleLayerEnabled = projectConfig.getTripleLayerMetatilesEnabled(); + + // Invisibly create an "About" window to read Porymap version + AboutPorymap *about = new AboutPorymap(mainWindow); + if (about) { + QJSValue version = Scripting::version(about->getVersionNumbers()); + constants.setProperty("version", version); + delete about; + } else { + logError("Failed to read Porymap version for API"); + } + constants.setProperty("max_primary_tiles", numTilesPrimary); + constants.setProperty("max_secondary_tiles", numTilesTotal - numTilesPrimary); + constants.setProperty("max_primary_metatiles", numMetatilesPrimary); + constants.setProperty("max_secondary_metatiles", numMetatilesTotal - numMetatilesPrimary); + constants.setProperty("layers_per_metatile", tripleLayerEnabled ? 3 : 2); + constants.setProperty("tiles_per_metatile", tripleLayerEnabled ? 12 : 8); + constants.setProperty("base_game_version", projectConfig.getBaseGameVersionString()); + + instance->engine->globalObject().setProperty("constants", constants); + + // Prevent changes to the object properties of the global object + instance->engine->evaluate("Object.freeze(map);"); + instance->engine->evaluate("Object.freeze(overlay);"); + instance->engine->evaluate("Object.freeze(constants.version);"); + instance->engine->evaluate("Object.freeze(constants);"); +} + bool Scripting::tryErrorJS(QJSValue js) { if (!js.isError()) return false; diff --git a/src/ui/aboutporymap.cpp b/src/ui/aboutporymap.cpp index 4a47aca8..98e07ec0 100644 --- a/src/ui/aboutporymap.cpp +++ b/src/ui/aboutporymap.cpp @@ -1,5 +1,6 @@ #include "aboutporymap.h" #include "ui_aboutporymap.h" +#include "log.h" AboutPorymap::AboutPorymap(QWidget *parent) : QMainWindow(parent), @@ -19,7 +20,9 @@ QList AboutPorymap::getVersionNumbers() // Get the version string "#.#.#" QRegularExpression regex("Version (\\d+)\\.(\\d+)\\.(\\d+)"); QRegularExpressionMatch match = regex.match(ui->label_Version->text()); - if (!match.hasMatch()) + if (!match.hasMatch()) { + logError("Failed to locate Porymap version text"); return QList({0, 0, 0}); + } return QList({match.captured(1).toInt(), match.captured(2).toInt(), match.captured(3).toInt()}); }