Skip to content

Commit

Permalink
Fix tilemap object edge cases causing crashes (#6945)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreSi authored Sep 12, 2024
1 parent 3a84ed7 commit 3b9a612
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 91 deletions.
111 changes: 64 additions & 47 deletions Extensions/TileMap/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -1896,8 +1896,10 @@ module.exports = {
* Renderer for instances of SimpleTileMap inside the IDE.
*/
class RenderedSimpleTileMapInstance extends RenderedInstance {
_getStartedText = 'Select this instance\nto start painting';
_noAtlasText = 'Set up an atlas image\nin the tilemap object.';
_placeholderTextPixiObject = new PIXI.Text(
'Select this instance\nto start painting',
'',
new PIXI.TextStyle({
fontFamily: 'Arial',
fontSize: 16,
Expand Down Expand Up @@ -2046,6 +2048,8 @@ module.exports = {
.getProperties()
.get('atlasImage')
.getValue();
if (!atlasImageResourceName) return;

const tilemapAsJSObject = JSON.parse(
this._instance.getRawStringProperty('tilemap') || '{}'
);
Expand Down Expand Up @@ -2082,51 +2086,56 @@ module.exports = {
const manager = TilemapHelper.TileMapManager.getManager(
this._project
);
manager.getOrLoadSimpleTileMap(
tilemapAsJSObject,
this._objectName,
tileSize,
columnCount,
rowCount,
(tileMap) => {
if (!tileMap) {
this._onLoadingError();
console.error('Could not parse tilemap.');
return;
}

this._editableTileMap = tileMap;

manager.getOrLoadSimpleTileMapTextureCache(
(textureName) =>
this._pixiResourcesLoader.getPIXITexture(
this._project,
textureName
),
atlasImageResourceName,
tileSize,
columnCount,
rowCount,
(
/** @type {TileMapHelper.TileTextureCache | null} */
textureCache
) => {
this._onLoadingSuccess();
if (!this._editableTileMap) return;

this.width = this._editableTileMap.getWidth();
this.height = this._editableTileMap.getHeight();
TilemapHelper.PixiTileMapHelper.updatePixiTileMap(
this.tileMapPixiObject,
this._editableTileMap,
textureCache,
'all', // No notion of visibility on simple tile maps.
0 // Only one layer is used on simple tile maps.
);
try {
manager.getOrLoadSimpleTileMap(
tilemapAsJSObject,
this._objectName,
tileSize,
columnCount,
rowCount,
(tileMap) => {
if (!tileMap) {
this._onLoadingError();
console.error('Could not parse tilemap.');
return;
}
);
}
);

this._editableTileMap = tileMap;

manager.getOrLoadSimpleTileMapTextureCache(
(textureName) =>
this._pixiResourcesLoader.getPIXITexture(
this._project,
textureName
),
atlasImageResourceName,
tileSize,
columnCount,
rowCount,
(
/** @type {TileMapHelper.TileTextureCache | null} */
textureCache
) => {
this._onLoadingSuccess();
if (!this._editableTileMap) return;

this.width = this._editableTileMap.getWidth();
this.height = this._editableTileMap.getHeight();
TilemapHelper.PixiTileMapHelper.updatePixiTileMap(
this.tileMapPixiObject,
this._editableTileMap,
textureCache,
'all', // No notion of visibility on simple tile maps.
0 // Only one layer is used on simple tile maps.
);
}
);
}
);
} catch (error) {
this._onLoadingError();
console.error('Could not load tilemap:', error);
}
};

if (atlasTexture.valid) {
Expand Down Expand Up @@ -2203,13 +2212,21 @@ module.exports = {
* This is called to update the PIXI object on the scene editor
*/
update() {
const atlasImageResourceName = this._associatedObjectConfiguration
.getProperties()
.get('atlasImage')
.getValue();

const isTileMapEmpty = this._editableTileMap
? this._editableTileMap.isEmpty()
: false;
let objectToChange;
if (isTileMapEmpty) {
this._placeholderPixiObject.visible = true;
if (isTileMapEmpty || !atlasImageResourceName) {
this.tileMapPixiObject.visible = false;
this._placeholderPixiObject.visible = true;
this._placeholderTextPixiObject.text = !atlasImageResourceName
? this._noAtlasText
: this._getStartedText;
objectToChange = this._placeholderPixiObject;
} else {
this._placeholderPixiObject.visible = false;
Expand Down
2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/dts/model/TileMapModel.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions SharedLibs/TileMapHelper/src/model/TileMapModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export class EditableTileMap {
): EditableTileMap {
const tileSet = new Map<number, TileDefinition>();

if (
!Number.isInteger(tileSetColumnCount) ||
tileSetColumnCount <= 0 ||
!Number.isInteger(tileSetRowCount) ||
tileSetRowCount <= 0
) {
throw new Error(
`Tilemap object badly configured. Tile size ${tileSize} is not compatible with atlas image dimensions, resulting in having ${tileSetColumnCount} columns and ${tileSetRowCount} rows.`
);
}
// TODO: Actually save and load tile set when useful.
new Array(tileSetColumnCount * tileSetRowCount)
.fill(0)
Expand Down
Loading

0 comments on commit 3b9a612

Please sign in to comment.