Merge branch 'master' of https://github.com/huderlem/porymap into slam

This commit is contained in:
garak
2024-01-09 14:52:00 -05:00
228 changed files with 28540 additions and 10561 deletions

View File

@@ -99,7 +99,7 @@ int MainWindow::getMetatileId(int x, int y) {
if (!this->editor->layout->getBlock(x, y, &block)) {
return 0;
}
return block.metatileId;
return block.metatileId();
}
void MainWindow::setMetatileId(int x, int y, int metatileId, bool forceRedraw, bool commitChanges) {
@@ -109,7 +109,7 @@ void MainWindow::setMetatileId(int x, int y, int metatileId, bool forceRedraw, b
if (!this->editor->layout->getBlock(x, y, &block)) {
return;
}
this->editor->layout->setBlock(x, y, Block(metatileId, block.collision, block.elevation));
this->editor->layout->setBlock(x, y, Block(metatileId, block.collision(), block.elevation()));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}
@@ -121,7 +121,7 @@ int MainWindow::getCollision(int x, int y) {
if (!this->editor->layout->getBlock(x, y, &block)) {
return 0;
}
return block.collision;
return block.collision();
}
void MainWindow::setCollision(int x, int y, int collision, bool forceRedraw, bool commitChanges) {
@@ -131,7 +131,7 @@ void MainWindow::setCollision(int x, int y, int collision, bool forceRedraw, boo
if (!this->editor->layout->getBlock(x, y, &block)) {
return;
}
this->editor->layout->setBlock(x, y, Block(block.metatileId, collision, block.elevation));
this->editor->layout->setBlock(x, y, Block(block.metatileId(), collision, block.elevation()));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}
@@ -143,7 +143,7 @@ int MainWindow::getElevation(int x, int y) {
if (!this->editor->layout->getBlock(x, y, &block)) {
return 0;
}
return block.elevation;
return block.elevation();
}
void MainWindow::setElevation(int x, int y, int elevation, bool forceRedraw, bool commitChanges) {
@@ -153,7 +153,7 @@ void MainWindow::setElevation(int x, int y, int elevation, bool forceRedraw, boo
if (!this->editor->layout->getBlock(x, y, &block)) {
return;
}
this->editor->layout->setBlock(x, y, Block(block.metatileId, block.collision, elevation));
this->editor->layout->setBlock(x, y, Block(block.metatileId(), block.collision(), elevation));
this->tryCommitMapChanges(commitChanges);
this->tryRedrawMapArea(forceRedraw);
}
@@ -585,9 +585,15 @@ void MainWindow::saveMetatileAttributesByMetatileId(int metatileId) {
if (this->editor->project && tileset)
this->editor->project->saveTilesetMetatileAttributes(tileset);
// If the Tileset Editor is currently displaying the updated metatile, refresh it
if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatileId() == metatileId)
this->tilesetEditor->onSelectedMetatileChanged(metatileId);
// If the tileset editor is open it needs to be refreshed with the new changes.
// Rather than do a full refresh (which is costly) we tell the editor it will need
// to reload the metatile from the project next time it's displayed.
// If it's currently being displayed, trigger this reload immediately.
if (this->tilesetEditor) {
this->tilesetEditor->queueMetatileReload(metatileId);
if (this->tilesetEditor->getSelectedMetatileId() == metatileId)
this->tilesetEditor->onSelectedMetatileChanged(metatileId);
}
}
Metatile * MainWindow::getMetatile(int metatileId) {
@@ -625,7 +631,7 @@ int MainWindow::getMetatileLayerType(int metatileId) {
Metatile * metatile = this->getMetatile(metatileId);
if (!metatile)
return -1;
return metatile->layerType;
return metatile->layerType();
}
void MainWindow::setMetatileLayerType(int metatileId, int layerType) {
@@ -640,7 +646,7 @@ int MainWindow::getMetatileEncounterType(int metatileId) {
Metatile * metatile = this->getMetatile(metatileId);
if (!metatile)
return -1;
return metatile->encounterType;
return metatile->encounterType();
}
void MainWindow::setMetatileEncounterType(int metatileId, int encounterType) {
@@ -655,7 +661,7 @@ int MainWindow::getMetatileTerrainType(int metatileId) {
Metatile * metatile = this->getMetatile(metatileId);
if (!metatile)
return -1;
return metatile->terrainType;
return metatile->terrainType();
}
void MainWindow::setMetatileTerrainType(int metatileId, int terrainType) {
@@ -670,7 +676,7 @@ int MainWindow::getMetatileBehavior(int metatileId) {
Metatile * metatile = this->getMetatile(metatileId);
if (!metatile)
return -1;
return metatile->behavior;
return metatile->behavior();
}
void MainWindow::setMetatileBehavior(int metatileId, int behavior) {
@@ -681,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)

View File

@@ -48,22 +48,20 @@ Scripting::Scripting(MainWindow *mainWindow) {
void Scripting::loadModules(QStringList moduleFiles) {
for (QString filepath : moduleFiles) {
QJSValue module = this->engine->importModule(filepath);
if (module.isError()) {
QString relativePath = QDir::cleanPath(userConfig.getProjectDir() + QDir::separator() + filepath);
module = this->engine->importModule(relativePath);
if (tryErrorJS(module)) {
QMessageBox messageBox(this->mainWindow);
messageBox.setText("Failed to load script");
messageBox.setInformativeText(QString("An error occurred while loading custom script file '%1'").arg(filepath));
messageBox.setDetailedText(getMostRecentError());
messageBox.setIcon(QMessageBox::Warning);
messageBox.addButton(QMessageBox::Ok);
messageBox.exec();
continue;
}
}
QString validPath = Project::getExistingFilepath(filepath);
if (!validPath.isEmpty()) filepath = validPath; // Otherwise allow it to fail with the original path
QJSValue module = this->engine->importModule(filepath);
if (tryErrorJS(module)) {
QMessageBox messageBox(this->mainWindow);
messageBox.setText("Failed to load script");
messageBox.setInformativeText(QString("An error occurred while loading custom script file '%1'").arg(filepath));
messageBox.setDetailedText(getMostRecentError());
messageBox.setIcon(QMessageBox::Warning);
messageBox.addButton(QMessageBox::Ok);
messageBox.exec();
continue;
}
logInfo(QString("Successfully loaded custom script file '%1'").arg(filepath));
this->modules.append(module);
}
@@ -78,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) {
@@ -93,28 +85,52 @@ 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);");
}
bool Scripting::tryErrorJS(QJSValue js) {
if (!js.isError()) return false;
if (!js.isError())
return false;
// Get properties of the error
QFileInfo file(js.property("fileName").toString());
QString fileName = file.fileName();
QString lineNumber = js.property("lineNumber").toString();
QString errStr = js.toString();
// The script engine is interrupted during project reopen, during which
// all script modules intentionally return as error objects.
// We don't need to report these "errors" to the user.
if (errStr == "Error: Interrupted")
return false;
// Convert properties to message strings
QString fileErrStr = fileName == "undefined" ? "" : QString(" '%1'").arg(fileName);
@@ -123,7 +139,7 @@ bool Scripting::tryErrorJS(QJSValue js) {
logError(QString("Error in custom script%1%2: '%3'")
.arg(fileErrStr)
.arg(lineErrStr)
.arg(js.toString()));
.arg(errStr));
return true;
}
@@ -305,9 +321,9 @@ void Scripting::cb_BorderVisibilityToggled(bool visible) {
QJSValue Scripting::fromBlock(Block block) {
QJSValue obj = instance->engine->newObject();
obj.setProperty("metatileId", block.metatileId);
obj.setProperty("collision", block.collision);
obj.setProperty("elevation", block.elevation);
obj.setProperty("metatileId", block.metatileId());
obj.setProperty("collision", block.collision());
obj.setProperty("elevation", block.elevation());
obj.setProperty("rawValue", block.rawValue());
return obj;
}
@@ -369,11 +385,22 @@ QJSEngine *Scripting::getEngine() {
return instance->engine;
}
QImage Scripting::getImage(QString filepath) {
const QImage * image = instance->imageCache.value(filepath, nullptr);
if (!image) {
image = new QImage(filepath);
instance->imageCache.insert(filepath, image);
const QImage * Scripting::getImage(const QString &inputFilepath, bool useCache) {
if (inputFilepath.isEmpty())
return nullptr;
const QImage * image;
if (useCache) {
// Try to retrieve image from the cache
image = instance->imageCache.value(inputFilepath, nullptr);
if (image) return image;
}
return QImage(*image);
const QString filepath = Project::getExistingFilepath(inputFilepath);
if (filepath.isEmpty())
return nullptr;
image = new QImage(filepath);
instance->imageCache.insert(inputFilepath, image);
return image;
}