Add constants object to API

This commit is contained in:
GriffinR 2022-09-04 23:41:42 -04:00
parent a3dadadd5b
commit 31b22bfcc1
7 changed files with 60 additions and 71 deletions

View file

@ -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<QString> getCustomScripts();
Q_INVOKABLE int getMainTab();
Q_INVOKABLE void setMainTab(int index);

View file

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

View file

@ -946,6 +946,8 @@ bool MainWindow::loadDataStructures() {
&& project->readEventGraphics()
&& project->readSongNames();
Scripting::populateGlobalObject(this);
return success && loadProjectCombos();
}

View file

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

View file

@ -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<QString> MainWindow::getCustomScripts() {
return projectConfig.getCustomScripts();
}

View file

@ -1,5 +1,7 @@
#include "scripting.h"
#include "log.h"
#include "config.h"
#include "aboutporymap.h"
QMap<CallbackType, QString> 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;

View file

@ -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<int> 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<int>({0, 0, 0});
}
return QList<int>({match.captured(1).toInt(), match.captured(2).toInt(), match.captured(3).toInt()});
}