Skip to content

Commit

Permalink
Added biome value tidying
Browse files Browse the repository at this point in the history
Simplified biome value
  • Loading branch information
deepnight committed Nov 29, 2023
1 parent a2c8b36 commit e50b768
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/assets/tpl/editAllAutoLayerRules.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2><span class="icon rule"></span> Rules: ::layer::</h2>
<div class="text"></div>
</div>
<input type="text" class="name"/>
<button class="groupActive transparent"></button>
<button class="groupActive transparent"> <span class="icon"></span> </button>
<button class="useWizard">EDIT</button>
<button class="addRule"><span class="icon add"></span></button>
</header>
Expand Down
2 changes: 1 addition & 1 deletion src/electron.renderer/data/DataTypes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef AutoLayerRuleGroup = {
var rules : Array<data.def.AutoLayerRuleDef>;
var isOptional : Bool;
var usesWizard : Bool;
var biomeEnumValues : Array<String>;
var biomeEnumValue : Null<String>;
}

typedef GridTileInfos = {
Expand Down
10 changes: 10 additions & 0 deletions src/electron.renderer/data/Project.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,10 @@ class Project {
case _:
}

for(ld in defs.layers)
if( ld.biomeFieldUid!=null && ld.getBiomeEnumDef()==enumDef )
return true;

return false;
}

Expand All @@ -1131,6 +1135,12 @@ class Project {
}
}

for(ld in defs.layers)
if( ld.biomeFieldUid!=null && ld.getBiomeEnumDef()==enumDef )
for( rg in ld.autoRuleGroups )
if( rg.biomeEnumValue==val )
return true;

return false;
}

Expand Down
14 changes: 13 additions & 1 deletion src/electron.renderer/data/def/EnumDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class EnumDef {
if( values[i].id==from ) {
values[i].id = to;

// Fix existing fields
// Fix existing field instances
_project.iterateAllFieldInstances( F_Enum(uid), function(fi) {
for(i in 0...fi.getArrayLength())
if( fi.getEnumValue(i)==from ) {
Expand All @@ -197,6 +197,18 @@ class EnumDef {
}
});

// Fix rule biomes
for( ld in _project.defs.layers ) {
if( ld.getBiomeEnumDef()!=this )
continue;

for( rg in ld.autoRuleGroups )
if( rg.biomeEnumValue==from ) {
rg.biomeEnumValue = to;
App.LOG.add("tidy", "Renamed biome enum value in rule group "+rg.name+" from layer "+ld.identifier);
}
}

// Fix tileset meta-data
for(td in _project.defs.tilesets)
if( td.tagsSourceEnumUid==uid && td.enumTags.exists(from) ) {
Expand Down
37 changes: 34 additions & 3 deletions src/electron.renderer/data/def/LayerDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class LayerDef {
isOptional: rg.isOptional,
rules: rg.rules.map( function(r) return r.toJson(this) ),
usesWizard: rg.usesWizard,
biomeEnumValues: rg.biomeEnumValues,
biomeEnumValue: rg.biomeEnumValue,
}
}

Expand All @@ -242,7 +242,7 @@ class LayerDef {
});
rg.collapsed = true;
rg.usesWizard = JsonTools.readBool( ruleGroupJson.usesWizard, false );
rg.biomeEnumValues = JsonTools.readArray( ruleGroupJson.biomeEnumValues, [] );
rg.biomeEnumValue = ruleGroupJson.biomeEnumValue;
return rg;
}

Expand Down Expand Up @@ -577,7 +577,7 @@ class LayerDef {
collapsed: false,
isOptional: false,
usesWizard: false,
biomeEnumValues: [],
biomeEnumValue: null,
rules: [],
}
if( index!=null )
Expand Down Expand Up @@ -658,6 +658,21 @@ class LayerDef {
cbEachRule(r);
}

public function getBiomeEnumDef() : EnumDef {
if( biomeFieldUid==null )
return null;

var fd = _project.defs.getFieldDef(biomeFieldUid);
return fd!=null ? fd.getEnumDefinition() : null;
}

public function getBiomeFieldDef() : FieldDef {
if( biomeFieldUid==null )
return null;

return _project.defs.getFieldDef(biomeFieldUid);
}

public function tidy(p:data.Project) {
_project = p;

Expand All @@ -670,5 +685,21 @@ class LayerDef {
// Lost source intGrid layer
if( autoSourceLayerDefUid!=null && p.defs.getLayerDef(autoSourceLayerDefUid)==null )
autoSourceLayerDefUid = null;

// Lost biome field
if( biomeFieldUid!=null && getBiomeEnumDef()==null ) {
App.LOG.add("tidy", 'Removed lost biome field in $this');
biomeFieldUid = null;
}

// Invalid biome values in rule groups
if( biomeFieldUid!=null ) {
var ed = getBiomeEnumDef();
for( rg in autoRuleGroups)
if( ed.getValue(rg.biomeEnumValue)==null ) {
App.LOG.add("tidy", 'Removed lost biome value ${rg.biomeEnumValue} in $this');
rg.biomeEnumValue = null;
}
}
}
}
8 changes: 4 additions & 4 deletions src/electron.renderer/data/inst/LayerInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class LayerInstance {
}
}


case Entities:
var i = 0;
var ei = null;
Expand Down Expand Up @@ -825,13 +826,12 @@ class LayerInstance {


public function isRuleGroupActiveHere(rg:AutoLayerRuleGroup) {
if( rg.biomeEnumValues.length>0 ) {
if( rg.active && rg.biomeEnumValue!=null ) {
var fi = level.getFieldInstanceByUid(def.biomeFieldUid, false);
if( fi!=null ) {
for(idx in 0...fi.getArrayLength())
for(bid in rg.biomeEnumValues)
if( bid==fi.getEnumValue(idx) )
return true;
if( fi.getEnumValue(idx)==rg.biomeEnumValue )
return true;
return false;
}
}
Expand Down
39 changes: 9 additions & 30 deletions src/electron.renderer/ui/modal/panel/EditAllAutoLayerRules.hx
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,6 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
// Enable/disable group
var jToggle = jGroupHeader.find(".groupActive");
jToggle.click( function(ev:js.jquery.Event) {
if( rg.biomeEnumValues.length>0 )
return;

if( rg.rules.length>0 && !rg.isOptional )
invalidateRuleGroup(rg);

Expand Down Expand Up @@ -695,21 +692,21 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
subMenu.push({
label: L.untranslated("Any biome"),
cb: ()->{
rg.biomeEnumValues = [];
rg.biomeEnumValue = null;
invalidateRuleGroup(rg);
editor.ge.emit( LayerRuleGroupChanged(rg) );
},
selectionTick: rg.biomeEnumValues.length==0 ? true : null,
selectionTick: rg.biomeEnumValue==null ? true : null,
});
for(ev in enumDef.values) {
subMenu.push({
label: L.untranslated(ev.id),
cb: ()->{
rg.biomeEnumValues = [ ev.id ];
rg.biomeEnumValue = ev.id;
invalidateRuleGroup(rg);
editor.ge.emit( LayerRuleGroupChanged(rg) );
},
selectionTick: rg.biomeEnumValues.contains(ev.id) ? true : null,
selectionTick: rg.biomeEnumValue==ev.id ? true : null,
jHtmlImg: ev.tileRect!=null ? project.resolveTileRectAsHtmlImg(ev.tileRect) : null,

});
Expand Down Expand Up @@ -856,29 +853,11 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
}

// Active state icon
var jGroupActive = jGroupHeader.find(".groupActive");
if( rg.biomeEnumValues.length>0 ) {
// var ed = project.defs.getEnumDef(ld.bui)
var fd = project.defs.getFieldDef(ld.biomeFieldUid);
trace(fd);
if( fd!=null ) {
var ed = fd.getEnumDefinition();
if( ed!=null ) {
var tileRect = ed.getValue(rg.biomeEnumValues[0]).tileRect;
if( tileRect!=null )
jGroupActive.append( project.resolveTileRectAsHtmlImg(tileRect) );
}
}
}
else {
var jIcon = new J('<span class="icon"/>');
jIcon.appendTo(jGroupActive);
jIcon.addClass(
rg.isOptional
? li.isRuleGroupActiveHere(rg) ? "visible" : "hidden"
: li.isRuleGroupActiveHere(rg) ? ( allActive ? "active" : "partial" ) : "inactive"
);
}
jGroupHeader.find(".groupActive .icon").addClass(
rg.isOptional
? li.isRuleGroupActiveHere(rg) ? "visible" : "hidden"
: li.isRuleGroupActiveHere(rg) ? ( allActive ? "active" : "partial" ) : "inactive"
);


// Make individual rules sortable
Expand Down

0 comments on commit e50b768

Please sign in to comment.