Skip to content

Commit

Permalink
Unified invalidated rules rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Jan 11, 2024
1 parent 316b160 commit 06b6445
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 79 deletions.
70 changes: 69 additions & 1 deletion src/electron.renderer/page/Editor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,73 @@ class Editor extends Page {
removePendingAction("rebuildAutoLayers");
});
});
}
}



public function applyInvalidatedRulesInAllLevels() {
var ops = [];
var affectedLayers : Map<data.inst.LayerInstance,data.Level> = new Map();

for(ld in project.defs.layers)
for(rg in ld.autoRuleGroups)
for(r in rg.rules) {
if( !r.invalidated )
continue;

for( w in project.worlds )
for( l in w.levels ) {
var li = l.getLayerInstance(ld);

r.invalidated = false;

if( li.autoTilesCache==null ) {
// Run all rules
ops.push({
label: 'Initializing autoTiles cache in ${l.identifier}.${li.def.identifier}',
cb: ()->{ li.applyAllRules(); }
});
affectedLayers.set(li,l);
}
else {
// Apply rule
if( !r.isEmpty() ) {
ops.push({
label: 'Applying rule #${r.uid} in ${l.identifier}.${li.def.identifier}',
cb: ()->{ li.applyRuleToFullLayer(r, false); },
});
affectedLayers.set(li,l);
}
}
}
}

// Apply "break on match" cascading effect in changed layers
var affectedLevels : Map<data.Level, Bool> = new Map();
for(li in affectedLayers.keys()) {
affectedLevels.set( affectedLayers.get(li), true );
ops.push({
label: 'Applying break on matches on ${affectedLayers.get(li).identifier}.${li.def.identifier}',
cb: li.applyBreakOnMatchesEverywhere.bind(),
});
}

// Refresh world renders & break caches
for(l in affectedLevels.keys())
ops.push({
label: 'Refreshing world render for ${l.identifier}...',
cb: ()->{
worldRender.invalidateLevelRender(l);
invalidateLevelCache(l);
},
});

if( ops.length>0 ) {
App.LOG.general("Applying invalidated rules...");
new ui.modal.Progress(L.t._("Updating auto layers..."), ops, levelRender.renderAll);
}
}


// public function invalidateLayerRules(layerDefUid:Int) {
// // if( active )
Expand Down Expand Up @@ -2298,6 +2364,8 @@ class Editor extends Page {
case LayerRuleGroupCollapseChanged(rg):

case BeforeProjectSaving:
applyInvalidatedRulesInAllLevels();

case ProjectSaved:

case ProjectSelected:
Expand Down
14 changes: 5 additions & 9 deletions src/electron.renderer/ui/modal/DebugMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ class DebugMenu extends ui.modal.ContextMenu {
label: L.untranslated("Rebuild all auto-layers"),
show: Editor.exists,
cb: ()->{
for(w in project.worlds)
for(l in w.levels)
for(li in l.layerInstances)
li.autoTilesCache = null;
editor.checkAutoLayersCache( (_)->{
N.success("Done");
editor.levelRender.invalidateAll();
editor.worldRender.invalidateAll();
});
for(ld in project.defs.layers)
for(rg in ld.autoRuleGroups)
for(r in rg.rules)
r.invalidated = true;
editor.applyInvalidatedRulesInAllLevels();
}
});

Expand Down
70 changes: 1 addition & 69 deletions src/electron.renderer/ui/modal/panel/EditAllAutoLayerRules.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
this.li = li;
updateFullPanel();

case BeforeProjectSaving:
applyInvalidatedRulesInAllLevels();

case LayerRuleChanged(r):
invalidateRuleAndOnesBelow(r);
updateRule(r);
Expand Down Expand Up @@ -98,8 +95,8 @@ class EditAllAutoLayerRules extends ui.modal.Panel {

override function onClose() {
super.onClose();
applyInvalidatedRulesInAllLevels();
editor.levelRender.clearTemp();
editor.applyInvalidatedRulesInAllLevels();
}

inline function invalidateRule(r:data.def.AutoLayerRuleDef) {
Expand All @@ -124,71 +121,6 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
}


function applyInvalidatedRulesInAllLevels() {
var ops = [];
var affectedLayers : Map<data.inst.LayerInstance,data.Level> = new Map();

// Apply edited rules to all other levels
for(rg in ld.autoRuleGroups)
for(r in rg.rules) {
if( !r.invalidated )
continue;

for( w in project.worlds )
for( l in w.levels ) {
var li = l.getLayerInstance(ld);

r.invalidated = false;

if( li.autoTilesCache==null ) {
// Run all rules
ops.push({
label: 'Initializing autoTiles cache in ${l.identifier}.${li.def.identifier}',
cb: ()->{ li.applyAllRules(); }
});
affectedLayers.set(li,l);
}
else {
// Apply rule
if( !r.isEmpty() ) {
var r = r;
ops.push({
label: 'Applying rule #${r.uid} in ${l.identifier}.${li.def.identifier}',
cb: ()->{ li.applyRuleToFullLayer(r, false); },
});
affectedLayers.set(li,l);
}
}
}
}

// Apply "break on match" cascading effect in changed layers
var affectedLevels : Map<data.Level, Bool> = new Map();
for(li in affectedLayers.keys()) {
affectedLevels.set( affectedLayers.get(li), true );
ops.push({
label: 'Applying break on matches on ${affectedLayers.get(li).identifier}.${li.def.identifier}',
cb: li.applyBreakOnMatchesEverywhere.bind(),
});
}

// Refresh world renders & break caches
for(l in affectedLevels.keys())
ops.push({
label: 'Refreshing world render for ${l.identifier}...',
cb: ()->{
editor.worldRender.invalidateLevelRender(l);
editor.invalidateLevelCache(l);
},
});

if( ops.length>0 ) {
App.LOG.general("Applying invalidated rules...");
new Progress(L.t._("Updating auto layers..."), ops, editor.levelRender.renderAll);
}
}


function showAffectedCells(r:data.def.AutoLayerRuleDef) {
if( App.ME.isCtrlCmdDown() )
return;
Expand Down

0 comments on commit 06b6445

Please sign in to comment.