Skip to content

Commit

Permalink
Added support for AND/OR modes
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Nov 30, 2023
1 parent feaf0db commit d1f1261
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 52 deletions.
2 changes: 1 addition & 1 deletion app/assets/css/app.min.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion app/assets/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,6 @@ textarea {
}
}
&.selected {
background-color: $bgMed;
color: white;
* {
color: white;
Expand Down
1 change: 1 addition & 0 deletions src/electron.renderer/data/DataTypes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef AutoLayerRuleGroup = {
var isOptional : Bool;
var usesWizard : Bool;
var requiredBiomeValues : Array<String>;
var biomeRequirementMode : Int;
}

typedef GridTileInfos = {
Expand Down
7 changes: 7 additions & 0 deletions src/electron.renderer/data/def/LayerDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class LayerDef {
rules: rg.rules.map( function(r) return r.toJson(this) ),
usesWizard: rg.usesWizard,
requiredBiomeValues: rg.requiredBiomeValues.copy(),
biomeRequirementMode: rg.biomeRequirementMode,
}
}

Expand All @@ -243,6 +244,7 @@ class LayerDef {
rg.collapsed = true;
rg.usesWizard = JsonTools.readBool( ruleGroupJson.usesWizard, false );
rg.requiredBiomeValues = ruleGroupJson.requiredBiomeValues!=null ? ruleGroupJson.requiredBiomeValues.copy() : [];
rg.biomeRequirementMode = JsonTools.readInt( ruleGroupJson.biomeRequirementMode, 0 );
return rg;
}

Expand Down Expand Up @@ -578,6 +580,7 @@ class LayerDef {
isOptional: false,
usesWizard: false,
requiredBiomeValues: [],
biomeRequirementMode: 0,
rules: [],
}
if( index!=null )
Expand Down Expand Up @@ -716,6 +719,10 @@ class LayerDef {
}
else
i++;

// Reset AND/OR mode
if( rg.requiredBiomeValues.length<=1 )
rg.biomeRequirementMode = 0;
}
}

Expand Down
32 changes: 20 additions & 12 deletions src/electron.renderer/data/inst/LayerInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -829,20 +829,28 @@ class LayerInstance {
if( rg.active && rg.requiredBiomeValues.length>0 ) {
var fi = level.getFieldInstanceByUid(def.biomeFieldUid, false);
if( fi!=null ) {
var matches = 0;
for( bid in rg.requiredBiomeValues ) {
for( arrayIdx in 0...fi.getArrayLength() )
if( fi.getEnumValue(arrayIdx)==bid ) {
matches++;
break;
switch rg.biomeRequirementMode {
case 0: // OR
for(idx in 0...fi.getArrayLength())
for( bid in rg.requiredBiomeValues )
if( fi.getEnumValue(idx)==bid )
return true;
return false;

case 1: // AND
var matches = 0;
for( bid in rg.requiredBiomeValues ) {
for( arrayIdx in 0...fi.getArrayLength() )
if( fi.getEnumValue(arrayIdx)==bid ) {
matches++;
break;
}
}
return matches>=rg.requiredBiomeValues.length;

case _:
return false;
}
// for(idx in 0...fi.getArrayLength()) {
// for( bid in rg.requiredBiomeValues )
// if( fi.getEnumValue(idx)==bid )
// return true;
// }
return matches>=rg.requiredBiomeValues.length;
}
}

Expand Down
65 changes: 35 additions & 30 deletions src/electron.renderer/ui/modal/ContextMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -128,60 +128,65 @@ class ContextMenu extends ui.Modal {
if( a.show!=null && !a.show() )
return new js.jquery.JQuery();

var jButton = new J('<button class="transparent"/>');
jButton.appendTo(jContent);
var isButton = a.cb!=null || a.subMenu!=null;

var jElement = isButton
? new J('<button class="transparent"/>')
: new J('<div class="title"/>');
jElement.appendTo(jContent);

if( a.jHtmlImg!=null ) {
jButton.prepend(a.label);
jButton.prepend(a.jHtmlImg);
jElement.prepend(a.label);
jElement.prepend(a.jHtmlImg);
}
else if( a.icon!=null )
jButton.prepend('<span class="icon ${a.icon}"></span> ${a.label}');
jElement.prepend('<span class="icon ${a.icon}"></span> ${a.label}');
else
jButton.html(a.label);
jElement.html(a.label);

if( a.subText!=null && a.subText!=a.label )
jButton.append('<span class="sub">${a.subText}</span>');
jElement.append('<span class="sub">${a.subText}</span>');

if( a.enable!=null && !a.enable() )
jButton.prop("disabled", true);
jElement.prop("disabled", true);

if( a.className!=null )
jButton.addClass(a.className);
jElement.addClass(a.className);

if( a.separatorBefore )
jButton.addClass("separatorBefore");
jElement.addClass("separatorBefore");

if( a.separatorAfter )
jButton.addClass("separatorAfter");
jElement.addClass("separatorAfter");

if( a.selectionTick!=null ) {
if( a.selectionTick ) {
jButton.addClass("selected");
jButton.prepend('<span class="icon selectionTick checkboxOn"></span>');
jElement.addClass("selected");
jElement.prepend('<span class="icon selectionTick checkboxOn"></span>');
}
else
jButton.prepend('<span class="icon selectionTick checkboxOff"></span>');
jElement.prepend('<span class="icon selectionTick checkboxOff"></span>');
}

// Button action
jButton.click( (_)->{
if( a.subMenu==null )
closeAll();
else {
addClass("subMenuOpen");
var c = new ContextMenu(jButton, true);
c.onCloseCb = ()->removeClass("subMenuOpen");

for(subAction in a.subMenu())
c.add(subAction);
}

if( a.cb!=null )
a.cb();
});
if( isButton )
jElement.click( (_)->{
if( a.subMenu==null )
closeAll();
else {
addClass("subMenuOpen");
var c = new ContextMenu(jElement, true);
c.onCloseCb = ()->removeClass("subMenuOpen");

for(subAction in a.subMenu())
c.add(subAction);
}

if( a.cb!=null )
a.cb();
});

applyAnchor();
return jButton;
return jElement;
}
}
47 changes: 39 additions & 8 deletions src/electron.renderer/ui/modal/panel/EditAllAutoLayerRules.hx
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,21 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
else if( enumDef.values.length==0 )
actions.push({
label: L.untranslated("Enum "+enumDef.identifier+" has no value"),
enable: ()->false
});

// Biome enum values
if( enumDef!=null ) {
function _onAnyBiomeChange() {
invalidateRuleGroup(rg);
editor.ge.emit( LayerRuleGroupChanged(rg) );
openBiomePicker(rg);
}

// Biome enum values
actions.push({
label: L.untranslated("Any biome"),
cb: ()->{
rg.requiredBiomeValues = [];
invalidateRuleGroup(rg);
editor.ge.emit( LayerRuleGroupChanged(rg) );
openBiomePicker(rg);
_onAnyBiomeChange();
},
selectionTick: rg.requiredBiomeValues.length==0 ? true : false,
});
Expand All @@ -551,16 +554,44 @@ class EditAllAutoLayerRules extends ui.modal.Panel {
rg.requiredBiomeValues.remove(ev.id);
else
rg.requiredBiomeValues.push(ev.id);
invalidateRuleGroup(rg);
editor.ge.emit( LayerRuleGroupChanged(rg) );
openBiomePicker(rg);
_onAnyBiomeChange();
},
selectionTick: rg.requiredBiomeValues.contains(ev.id) ? true : false,
jHtmlImg: ev.tileRect!=null ? project.resolveTileRectAsHtmlImg(ev.tileRect) : null,
});
}


// AND/OR modes
if( rg.requiredBiomeValues.length>1 ) {
actions.push({
separatorBefore: true,
label: L.t._("Condition type"),
});

actions.push({
label: L.t._("OR"),
subText: L.t._("At least one the checked values is required."),
cb: ()->{
rg.biomeRequirementMode = 0;
_onAnyBiomeChange();
},
selectionTick: rg.biomeRequirementMode==0,
});

actions.push({
label: L.t._("AND"),
subText: L.t._("All checked values are required."),
cb: ()->{
rg.biomeRequirementMode = 1;
_onAnyBiomeChange();
},
selectionTick: rg.biomeRequirementMode==1,
});
}
}


// Create menu
var jTarget = jContent.find("ul.ruleGroups").children('[groupUid=${rg.uid}]').find(".biome");
var ctx = new ContextMenu(jTarget);
Expand Down

0 comments on commit d1f1261

Please sign in to comment.