Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting custom IntGrid values #1136

Open
wants to merge 2 commits into
base: dev-1.5.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/tpl/editLayerDefs.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ <h2>

<xml id="intGridValue">
<div class="sortHandle"></div>
<span class="id">?</span>
<input class="id" type="text" placeholder="0"/>
<input type="color" value="#ffffff"/>
<span class="tile"></span>
<input type="text" class="name" placeholder="Optional value identifier"/>
Expand Down
13 changes: 13 additions & 0 deletions src/electron.renderer/data/def/AutoLayerRuleDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ class AutoLayerRuleDef {
explicitlyRequiredValues.push(v);
}

public function updateIntGridValueDef(prev:Int,next:Int) {
// Use get/setPattern so that the usage counts, etc. are updated.
for(cx in 0...size)
for(cy in 0...size) {
if(getPattern(cx,cy) == prev)
setPattern(cx,cy,next);
// Negative values are used to store the logical negation of a tile, so we have to update them too.
if(getPattern(cx,cy) == -prev)
setPattern(cx,cy,-next);
}
updateUsedValues();
}

public inline function hasAnyPositionOffset() {
return tileRandomXMin!=0 || tileRandomXMax!=0 || tileRandomYMin!=0 || tileRandomYMax!=0 || tileXOffset!=0 || tileYOffset!=0;
}
Expand Down
19 changes: 19 additions & 0 deletions src/electron.renderer/data/def/LayerDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ class LayerDef {
return false;
}

public function updateIntGridValueDef(prev:Int, next:Int) {
var iv = getIntGridValueDef(prev);
if(iv == null)
return;
iv.value = next;
}

public inline function getIntGridValueDef(value:Int) : Null<IntGridValueDefEditor> {
var out : Null<IntGridValueDefEditor> = null;
for(v in intGridValues)
Expand Down Expand Up @@ -449,6 +456,18 @@ class LayerDef {

public inline function countIntGridValues() return intGridValues.length;

public function isIntGridValueValid(value:Int) : Bool {
// Negative values are used to logically invert patterns, so zero and negative values are not allowed.
// AutoLayerRuleDef::isUsingUnknownIntGridValues requires the value be less than or equal to 999.
if ( value <= 0 || value > 999 )
return false;

for(v in intGridValues)
if ( v.value == value )
return false;
return true;
}

public function isIntGridValueIdentifierValid(id:Null<String>) {
if( id==null || id=="" )
return true;
Expand Down
21 changes: 21 additions & 0 deletions src/electron.renderer/data/inst/LayerInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,27 @@ class LayerInstance {
asyncErase(cx,cy);
}

public function updateIntGridValue(prev:Int, next:Int, useAsyncRender:Bool) {
requireType(IntGrid);
def.updateIntGridValueDef(prev,next);
for(cy in 0...cHei)
for(cx in 0...cWid)
if ( getIntGrid(cx,cy) == prev )
setIntGrid(cx,cy,next,useAsyncRender);

// We update our own level definition separately because we don't appear in our own autoSourceLayerDefUid.
for(rg in def.autoRuleGroups)
for(r in rg.rules)
r.updateIntGridValueDef(prev, next);

for(ld in _project.defs.layers)
if(ld.autoSourceLayerDefUid == layerDefUid)
for(rg in ld.autoRuleGroups)
for(r in rg.rules)
r.updateIntGridValueDef(prev, next);

level.invalidateJsonCache();
}

/** ENTITY INSTANCE *******************/

Expand Down
4 changes: 4 additions & 0 deletions src/electron.renderer/ui/Notification.hx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class Notification extends dn.Process {
}
}

public static function invalidValue(value:Int) {
error( Lang.t._("The value \"::value::\" isn't valid, or isn't unique.", { value:value }) );
}

public static function invalidIdentifier(id:String) {
error( Lang.t._("The identifier \"::id::\" isn't valid, or isn't unique.", { id:id }) );
}
Expand Down
27 changes: 20 additions & 7 deletions src/electron.renderer/ui/modal/panel/EditLayerDefs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,26 @@ class EditLayerDefs extends ui.modal.Panel {
jValue.attr("valueId", Std.string(intGridVal.value));
jValue.addClass("value");
jValue.appendTo(jGroup);
jValue.find(".id")
.html( Std.string(intGridVal.value) )
.css({
color: C.intToHex( C.toWhite(intGridVal.color,0.5) ),
borderColor: C.intToHex( C.toWhite(intGridVal.color,0.2) ),
backgroundColor: C.intToHex( C.toBlack(intGridVal.color,0.5) ),
});

// Value
var i = new form.input.IntInput(
jValue.find("input.id"),
function() return intGridVal.value,
function(value) {
if( value != null ) {
jValue.attr("valueId", Std.string(value));
editor.curLevel.getLayerInstance(cur).updateIntGridValue(intGridVal.value, value, false);
}
}
);
i.validityCheck = cur.isIntGridValueValid;
i.validityError = N.invalidValue;
i.onChange = editor.ge.emit.bind(LayerDefChanged(cur.uid, false));
i.jInput.css({
color: C.intToHex( C.toWhite(intGridVal.color,0.5) ),
borderColor: C.intToHex( C.toWhite(intGridVal.color,0.2) ),
backgroundColor: C.intToHex( C.toBlack(intGridVal.color,0.5) ),
});

// Tile
var jTile = jValue.find(".tile");
Expand Down