Add some constants to the API

This commit is contained in:
GriffinR 2024-01-03 16:34:55 -05:00
parent eecdd8c331
commit d2a0d9299f
3 changed files with 33 additions and 8 deletions

View file

@ -7,6 +7,9 @@ and this project somewhat adheres to [Semantic Versioning](https://semver.org/sp
The **"Breaking Changes"** listed below are changes that have been made in the decompilation projects (e.g. pokeemerald), which porymap requires in order to work properly. It also includes changes to the scripting API that may change the behavior of existing porymap scripts. If porymap is used with a project or API script that is not up-to-date with the breaking changes, then porymap will likely break or behave improperly.
## [Unreleased]
### Added
- Add `metatile_behaviors`, `num_primary_palettes`, and `num_secondary_palettes` to `constants` in the API.
### Changed
- The API functions `addImage` and `createImage` now support project-relative paths.
- Metatile ID strings are now padded to their current max, not the overall max.

View file

@ -2082,6 +2082,14 @@ All constants are accessible via the global ``constants`` object.
The maximum number of metatiles in a secondary tileset.
.. js:attribute:: constants.num_primary_palettes
The number of palettes in a primary tileset.
.. js:attribute:: constants.num_secondary_palettes
The number of palettes in a secondary tileset.
.. js:attribute:: constants.layers_per_metatile
The number of tile layers used in each metatile. This will either be ``2`` or ``3``, depending on the config setting ``enable_triple_layer_metatiles``.
@ -2090,6 +2098,10 @@ All constants are accessible via the global ``constants`` object.
The number of tiles in each metatile. This will either be ``8`` or ``12``, depending on the config setting ``enable_triple_layer_metatiles``.
.. js:attribute:: constants.metatile_behaviors
An object mapping metatile behavior names to their values. For example, ``constants.metatile_behaviors["MB_TALL_GRASS"]`` would normally be ``2``.
.. js:attribute:: constants.base_game_version
The string value of the config setting ``base_game_version``. This will either be ``pokeruby``, ``pokefirered``, or ``pokeemerald``.

View file

@ -76,12 +76,6 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) {
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();
// Invisibly create an "About" window to read Porymap version
AboutPorymap *about = new AboutPorymap(mainWindow);
if (about) {
@ -91,17 +85,33 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) {
} else {
logError("Failed to read Porymap version for API");
}
// Get basic tileset information
int numTilesPrimary = Project::getNumTilesPrimary();
int numMetatilesPrimary = Project::getNumMetatilesPrimary();
int numPalettesPrimary = Project::getNumPalettesPrimary();
constants.setProperty("max_primary_tiles", numTilesPrimary);
constants.setProperty("max_secondary_tiles", numTilesTotal - numTilesPrimary);
constants.setProperty("max_secondary_tiles", Project::getNumTilesTotal() - numTilesPrimary);
constants.setProperty("max_primary_metatiles", numMetatilesPrimary);
constants.setProperty("max_secondary_metatiles", numMetatilesTotal - numMetatilesPrimary);
constants.setProperty("max_secondary_metatiles", Project::getNumMetatilesTotal() - numMetatilesPrimary);
constants.setProperty("num_primary_palettes", numPalettesPrimary);
constants.setProperty("num_secondary_palettes", Project::getNumPalettesTotal() - numPalettesPrimary);
constants.setProperty("layers_per_metatile", projectConfig.getNumLayersInMetatile());
constants.setProperty("tiles_per_metatile", projectConfig.getNumTilesInMetatile());
constants.setProperty("base_game_version", projectConfig.getBaseGameVersionString());
// Read out behavior values into constants object
QJSValue behaviorsArray = instance->engine->newObject();
const QMap<QString, uint32_t> * map = &mainWindow->editor->project->metatileBehaviorMap;
for (auto i = map->cbegin(), end = map->cend(); i != end; i++)
behaviorsArray.setProperty(i.key(), i.value());
constants.setProperty("metatile_behaviors", behaviorsArray);
instance->engine->globalObject().setProperty("constants", constants);
// Prevent changes to the constants object
instance->engine->evaluate("Object.freeze(constants.metatile_behaviors);");
instance->engine->evaluate("Object.freeze(constants.version);");
instance->engine->evaluate("Object.freeze(constants);");
}