Apply the "map opened" API callback to layouts, add utility functions

This commit is contained in:
GriffinR
2025-02-07 16:25:58 -05:00
parent df861b59ad
commit 01126a888a
5 changed files with 59 additions and 6 deletions

View File

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

View File

@@ -42,6 +42,9 @@ public:
Q_INVOKABLE QList<float> getMetatileLayerOpacity();
Q_INVOKABLE void setMetatileLayerOpacity(QList<float> order);
Q_INVOKABLE QList<QString> getMapNames();
Q_INVOKABLE QList<QString> getMapConstants();
Q_INVOKABLE QList<QString> getLayoutNames();
Q_INVOKABLE QList<QString> getLayoutConstants();
Q_INVOKABLE QList<QString> getTilesetNames();
Q_INVOKABLE QList<QString> getPrimaryTilesetNames();
Q_INVOKABLE QList<QString> getSecondaryTilesetNames();

View File

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

View File

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

View File

@@ -249,6 +249,27 @@ QList<QString> ScriptUtility::getMapNames() {
return window->editor->project->mapNames;
}
QList<QString> ScriptUtility::getMapConstants() {
if (!window || !window->editor || !window->editor->project)
return QList<QString>();
return window->editor->project->mapConstantsToMapNames.keys();
}
QList<QString> ScriptUtility::getLayoutNames() {
QList<QString> 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<QString> ScriptUtility::getLayoutConstants() {
if (!window || !window->editor || !window->editor->project)
return QList<QString>();
return window->editor->project->layoutIds;
}
QList<QString> ScriptUtility::getTilesetNames() {
if (!window || !window->editor || !window->editor->project)
return QList<QString>();