diff --git a/CHANGELOG.md b/CHANGELOG.md index ed489d72..eac8dfd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The **"Breaking Changes"** listed below are changes that have been made in the d ## [Unreleased] ### Added +- Add `getMetatileBehaviorName` and `setMetatileBehaviorName` to the API. - Add `metatile_behaviors`, `num_primary_palettes`, and `num_secondary_palettes` to `constants` in the API. ### Changed diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index 9507b048..fe47f67f 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -1069,6 +1069,26 @@ All tileset functions are callable via the global ``map`` object. :param behavior: the behavior :type behavior: number +.. js:function:: map.getMetatileBehaviorName(metatileId) + + Gets the behavior name for the specified metatile. Returns an empty string if the metatile's behavior value has no name. + + :param metatileId: id of target metatile + :type metatileId: number + :returns: the behavior name + :rtype: string + +.. js:function:: map.setMetatileBehaviorName(metatileId, behavior) + + Sets the behavior name for the specified metatile. Does nothing if there is no metatile behavior define with the specified name. + + **Warning:** This function writes directly to the tileset. There is no undo for this. + + :param metatileId: id of target metatile + :type metatileId: number + :param behavior: the behavior name + :type behavior: string + .. js:function:: map.getMetatileAttributes(metatileId) Gets the raw attributes value for the specified metatile. diff --git a/include/mainwindow.h b/include/mainwindow.h index 8eb9cefb..bc6dface 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -123,6 +123,8 @@ public: Q_INVOKABLE void setMetatileTerrainType(int metatileId, int terrainType); Q_INVOKABLE int getMetatileBehavior(int metatileId); Q_INVOKABLE void setMetatileBehavior(int metatileId, int behavior); + Q_INVOKABLE QString getMetatileBehaviorName(int metatileId); + Q_INVOKABLE void setMetatileBehaviorName(int metatileId, QString behavior); Q_INVOKABLE int getMetatileAttributes(int metatileId); Q_INVOKABLE void setMetatileAttributes(int metatileId, int attributes); Q_INVOKABLE QJSValue getMetatileTile(int metatileId, int tileIndex); diff --git a/src/scriptapi/apimap.cpp b/src/scriptapi/apimap.cpp index 6fb436d2..3741092e 100644 --- a/src/scriptapi/apimap.cpp +++ b/src/scriptapi/apimap.cpp @@ -687,6 +687,25 @@ void MainWindow::setMetatileBehavior(int metatileId, int behavior) { this->saveMetatileAttributesByMetatileId(metatileId); } +QString MainWindow::getMetatileBehaviorName(int metatileId) { + Metatile * metatile = this->getMetatile(metatileId); + if (!metatile || !this->editor->project) + return QString(); + return this->editor->project->metatileBehaviorMapInverse.value(metatile->behavior(), QString()); +} + +void MainWindow::setMetatileBehaviorName(int metatileId, QString behavior) { + Metatile * metatile = this->getMetatile(metatileId); + if (!metatile || !this->editor->project) + return; + if (!this->editor->project->metatileBehaviorMap.contains(behavior)) { + logError(QString("Unknown metatile behavior '%1'").arg(behavior)); + return; + } + metatile->setBehavior(this->editor->project->metatileBehaviorMap.value(behavior)); + this->saveMetatileAttributesByMetatileId(metatileId); +} + int MainWindow::getMetatileAttributes(int metatileId) { Metatile * metatile = this->getMetatile(metatileId); if (!metatile)