diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index fe47f67f..b645f49a 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -153,9 +153,9 @@ Callbacks .. js:function:: onMapOpened(mapName) - Called when a map is opened. + Called when a map or layout is opened. - :param mapName: the name of the opened map + :param mapName: the name of the opened map or layout :type mapName: string .. js:function:: onBlockChanged(x, y, prevBlock, newBlock) @@ -1995,7 +1995,28 @@ All utility functions are callable via the global ``utility`` object. Gets the list of map names. - :returns: the list of map names + :returns: the list of map names (e.g. `PetalburgCity`) + :rtype: array + +.. js:function:: utility.getMapConstants() + + Gets the list of map IDs (e.g. `MAP_PETALBURG_CITY`) + + :returns: the list of map IDs + :rtype: array + +.. js:function:: utility.getLayoutNames() + + Gets the list of layout names. + + :returns: the list of layout names (e.g. `PetalburgCity_Layout`) + :rtype: array + +.. js:function:: utility.getLayoutConstants() + + Gets the list of layout IDs (e.g. `LAYOUT_PETALBURG_CITY`) + + :returns: the list of layout IDs :rtype: array .. js:function:: utility.getTilesetNames() diff --git a/include/scriptutility.h b/include/scriptutility.h index 0d6fca75..78272eec 100644 --- a/include/scriptutility.h +++ b/include/scriptutility.h @@ -42,6 +42,9 @@ public: Q_INVOKABLE QList getMetatileLayerOpacity(); Q_INVOKABLE void setMetatileLayerOpacity(QList order); Q_INVOKABLE QList getMapNames(); + Q_INVOKABLE QList getMapConstants(); + Q_INVOKABLE QList getLayoutNames(); + Q_INVOKABLE QList getLayoutConstants(); Q_INVOKABLE QList getTilesetNames(); Q_INVOKABLE QList getPrimaryTilesetNames(); Q_INVOKABLE QList getSecondaryTilesetNames(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 74f579c9..23dbbae7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -947,6 +947,7 @@ bool MainWindow::setLayout(QString layoutId) { connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::redrawMapScene, Qt::UniqueConnection); + Scripting::cb_MapOpened(layout->name); updateTilesetEditor(); userConfig.recentMapOrLayout = layoutId; @@ -2815,8 +2816,15 @@ void MainWindow::reloadScriptEngine() { Scripting::populateGlobalObject(this); // Lying to the scripts here, simulating a project reload Scripting::cb_ProjectOpened(projectConfig.projectDir); - if (editor && editor->map) - Scripting::cb_MapOpened(editor->map->name()); // TODO: API should have equivalent for layout + if (this->editor) { + QString curName; + if (this->editor->map) + curName = this->editor->map->name(); + else if (editor->layout) + curName = this->editor->layout->name; + + Scripting::cb_MapOpened(curName); + } } void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) { diff --git a/src/scriptapi/apimap.cpp b/src/scriptapi/apimap.cpp index 2532f132..0ee3316e 100644 --- a/src/scriptapi/apimap.cpp +++ b/src/scriptapi/apimap.cpp @@ -794,7 +794,7 @@ void MainWindow::setMetatileTile(int metatileId, int tileIndex, QJSValue tileObj } QJSValue MainWindow::getTilePixels(int tileId) { - if (tileId < 0 || !this->editor || !this->editor->project || !this->editor->map || !this->editor->layout) + if (tileId < 0 || !this->editor || !this->editor->layout) return QJSValue(); QImage tileImage = getTileImage(tileId, this->editor->layout->tileset_primary, this->editor->layout->tileset_secondary); if (tileImage.isNull() || tileImage.sizeInBytes() < 64) diff --git a/src/scriptapi/apiutility.cpp b/src/scriptapi/apiutility.cpp index e5cebc54..5d2072a9 100644 --- a/src/scriptapi/apiutility.cpp +++ b/src/scriptapi/apiutility.cpp @@ -249,6 +249,27 @@ QList ScriptUtility::getMapNames() { return window->editor->project->mapNames; } +QList ScriptUtility::getMapConstants() { + if (!window || !window->editor || !window->editor->project) + return QList(); + return window->editor->project->mapConstantsToMapNames.keys(); +} + +QList ScriptUtility::getLayoutNames() { + QList names; + if (!window || !window->editor || !window->editor->project) + return names; + for (const auto &layout : window->editor->project->mapLayouts) + names.append(layout->name); + return names; +} + +QList ScriptUtility::getLayoutConstants() { + if (!window || !window->editor || !window->editor->project) + return QList(); + return window->editor->project->layoutIds; +} + QList ScriptUtility::getTilesetNames() { if (!window || !window->editor || !window->editor->project) return QList();