Skip to content

Commit

Permalink
Fixed IntGrid counts cache when resizing levels (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Jan 11, 2024
1 parent 91161cc commit f02c4a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/electron.renderer/data/Level.hx
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ class Level {
li.applyNewBounds(newPxLeft, newPxTop, newPxWid, newPxHei);
pxWid = newPxWid;
pxHei = newPxHei;
for(li in layerInstances)
li.recountAllIntGridValues();

// Remove entities out of bounds
var n = 0;
Expand Down
23 changes: 17 additions & 6 deletions src/electron.renderer/data/inst/LayerInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ class LayerInstance {
return areaIntGridUseCount.exists(iv) && areaIntGridUseCount.get(iv).get(areaCoordId(cx,cy)) > 0;
}

public function recountAllIntGridValues() {
if( def.type!=IntGrid )
return;

areaIntGridUseCount = new Map();
layerIntGridUseCount = new Map();

for(cy in 0...cHei)
for(cx in 0...cWid) {
if( hasIntGrid(cx,cy) )
increaseAreaIntGridValueCount(getIntGrid(cx,cy), cx, cy);
}
}

inline function increaseAreaIntGridValueCount(iv:Int, cx:Int, cy:Int) {
if( iv==0 || iv==null )
return;
Expand Down Expand Up @@ -353,19 +367,16 @@ class LayerInstance {

if( json.intGridCsv==null ) {
// Read old pre-CSV format
for( intGridJson in json.intGrid ) {
for( intGridJson in json.intGrid )
li.intGrid.set( intGridJson.coordId, intGridJson.v+1 );
li.increaseAreaIntGridValueCount( intGridJson.v+1, li.getCx(intGridJson.coordId), li.getCy(intGridJson.coordId) );
}
}
else {
// Read CSV format
for(coordId in 0...json.intGridCsv.length)
if( json.intGridCsv[coordId]>=0 ) {
if( json.intGridCsv[coordId]>=0 )
li.intGrid.set(coordId, json.intGridCsv[coordId]);
li.increaseAreaIntGridValueCount( json.intGridCsv[coordId], li.getCx(coordId), li.getCy(coordId) );
}
}
li.recountAllIntGridValues();

for( gridTilesJson in json.gridTiles ) {
if( dn.Version.lower(p.jsonVersion, "0.4", true) || gridTilesJson.d==null )
Expand Down
5 changes: 5 additions & 0 deletions src/electron.renderer/ui/modal/panel/EditLayerDefs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ class EditLayerDefs extends ui.modal.Panel {
}
}
i.onChange = ()->{
for(w in project.worlds)
for(l in w.levels)
for(li in l.layerInstances)
li.recountAllIntGridValues();

editor.ge.emit( LayerDefChanged(cur.uid, false) );

for(ld in project.defs.layers)
Expand Down

0 comments on commit f02c4a6

Please sign in to comment.