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

Entity flips revisited #982

Open
wants to merge 9 commits into
base: master
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
17 changes: 17 additions & 0 deletions app/assets/tpl/editEntityDefs.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ <h2> <div class="icon entity"></div> Project entities </h2>
Keep aspect ratio
</label>
</dd>
<dd>
<span>Flippable:</span>
<label for="flippableX">
<input type="checkbox" id="flippableX" title="If checked, the entity instances will be horizontally flippable."/>
Horizontal
</label>

<label for="flippableY">
<input type="checkbox" id="flippableY" title="If checked, the entity instances will be vertically flippable."/>
Vertical
</label>

<label for="flipAroundPivot">
<input type="checkbox" id="flipAroundPivot" title="If checked, the entity instances will flip around their pivot instead of just within their existing bounds."/>
Flip around pivot
</label>
</dd>

<dt>
<label>Editor visual</label>
Expand Down
18 changes: 17 additions & 1 deletion app/assets/tpl/entityInstanceEditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
<span class="unit" title="Change coordinates unit"></span>
</dd>

<dt>
Flips
<info>How this Entity instance is oriented on the X and Y axes.</info>
</dt>
<dd class="flips">
<label>
<input type="checkbox" name="x"/>
Horizontal
</label>

<label>
<input type="checkbox" name="y"/>
Vertical
</label>
</dd>

<dt class="refs">
References to this entity
<info>This is a list of all other Entities having a Reference field pointing to this Entity.</info>
Expand All @@ -42,4 +58,4 @@
<div class="customFieldsWrapper"></div>
</div>

<div class="resizeBar"></div>
<div class="resizeBar"></div>
6 changes: 3 additions & 3 deletions src/electron.renderer/GenericLevelElementGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ class GenericLevelElementGroup {
case Entity(li, ei):
selectRender.beginFill(c, alpha);
selectRender.drawRect(
li.pxParallaxX + ( ei.x - ei.width * ei.def.pivotX ) * li.def.getScale(),
li.pxParallaxY + ( ei.y - ei.height * ei.def.pivotY ) * li.def.getScale(),
li.pxParallaxX + ( ei.x - ei.width * ei.getAdjustedPivotX() ) * li.def.getScale(),
li.pxParallaxY + ( ei.y - ei.height * ei.getAdjustedPivotY() ) * li.def.getScale(),
ei.width * li.def.getScale(),
ei.height * li.def.getScale()
);
Expand Down Expand Up @@ -863,4 +863,4 @@ class GenericLevelElementGroup {
renderSelection();
}
}
}
}
21 changes: 20 additions & 1 deletion src/electron.renderer/data/def/EntityDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class EntityDef {

public var resizableX : Bool;
public var resizableY : Bool;
public var flippableX : Bool;
public var flippableY : Bool;
public var flipAroundPivot : Bool;
public var minWidth : Null<Int>;
public var maxWidth : Null<Int>;
public var minHeight : Null<Int>;
Expand Down Expand Up @@ -66,6 +69,8 @@ class EntityDef {
identifier = "Entity"+uid;
setPivot(0.5,1);
resizableX = resizableY = false;
flippableX = flippableY = false;
flipAroundPivot = false;
keepAspectRatio = false;
hollow = false;
tags = new Tags();
Expand Down Expand Up @@ -97,6 +102,14 @@ class EntityDef {
return null;
}

public inline function getFlippedPivotX() {
return flipAroundPivot ? ((width - 1) / width) - pivotX : pivotX;
}

public inline function getFlippedPivotY() {
return flipAroundPivot ? ((height - 1) / height) - pivotY : pivotY;
}

function set_identifier(id:String) {
return identifier = Project.isValidIdentifier(id) ? Project.cleanupIdentifier(id, _project.identifierStyle) : identifier;
}
Expand Down Expand Up @@ -149,6 +162,9 @@ class EntityDef {
o.height = JsonTools.readInt( json.height, 16 );
o.resizableX = JsonTools.readBool( json.resizableX, false );
o.resizableY = JsonTools.readBool( json.resizableY, false );
o.flippableX = JsonTools.readBool( json.flippableX, false );
o.flippableY = JsonTools.readBool( json.flippableY, false );
o.flipAroundPivot = JsonTools.readBool( json.flipAroundPivot, false );
o.minWidth = JsonTools.readNullableInt( json.minWidth );
o.maxWidth = JsonTools.readNullableInt( json.maxWidth );
o.minHeight = JsonTools.readNullableInt( json.minHeight );
Expand Down Expand Up @@ -207,6 +223,9 @@ class EntityDef {
height: height,
resizableX: resizableX,
resizableY: resizableY,
flippableX: flippableX,
flippableY: flippableY,
flipAroundPivot: flipAroundPivot,
minWidth: minWidth,
maxWidth: maxWidth,
minHeight: minHeight,
Expand Down Expand Up @@ -329,4 +348,4 @@ class EntityDef {
for(fd in fieldDefs)
fd.tidy(p);
}
}
}
30 changes: 22 additions & 8 deletions src/electron.renderer/data/inst/EntityInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EntityInstance {
public var worldY(get,never) : Int;
public var customWidth : Null<Int>;
public var customHeight: Null<Int>;
public var flips : Int;

public var width(get,never) : Int;
inline function get_width() return customWidth!=null ? customWidth : def.width;
Expand All @@ -24,25 +25,26 @@ class EntityInstance {

public var fieldInstances : Map<Int, data.inst.FieldInstance> = new Map();

public var left(get,never) : Int; inline function get_left() return M.round( x - width*def.pivotX );
public var left(get,never) : Int; inline function get_left() return M.round( x - width*getAdjustedPivotX() );
public var right(get,never) : Int; inline function get_right() return left + width;
public var top(get,never) : Int; inline function get_top() return M.round( y - height*def.pivotY );
public var top(get,never) : Int; inline function get_top() return M.round( y - height*getAdjustedPivotY() );
public var bottom(get,never) : Int; inline function get_bottom() return top + height;


public function new(p:Project, li:LayerInstance, entityDefUid:Int, iid:String) {
public function new(p:Project, li:LayerInstance, entityDefUid:Int, iid:String, flips:Int = 0) {
_project = p;
_li = li;
defUid = entityDefUid;
this.iid = iid;
this.flips = flips;
}

@:keep public function toString() {
return 'EntityInst "${def.identifier}" @$x,$y';
}

inline function get_centerX() return M.round( x + (0.5-def.pivotX)*width );
inline function get_centerY() return M.round( y + (0.5-def.pivotY)*height );
inline function get_centerX() return M.round( x + (0.5-getAdjustedPivotX())*width );
inline function get_centerY() return M.round( y + (0.5-getAdjustedPivotY())*height );

inline function get_worldX() return Std.int( x + _li.level.worldX );
inline function get_worldY() return Std.int( y + _li.level.worldY );
Expand Down Expand Up @@ -70,6 +72,7 @@ class EntityInstance {
height: height,
defUid: defUid,
px: [x,y],
f: flips,
fieldInstances: {
var all = [];
for(fd in def.fieldDefs)
Expand All @@ -90,6 +93,7 @@ class EntityInstance {
layer: _li.def.identifier,
x : x,
y : y,
f: flips,
width: width,
height: height,
color: getSmartColor(false),
Expand All @@ -111,6 +115,8 @@ class EntityInstance {
ei.x = JsonTools.readInt( json.px[0], 0 );
ei.y = JsonTools.readInt( json.px[1], 0 );

ei.flips = JsonTools.readInt( json.f, 0 );

ei.customWidth = JsonTools.readNullableInt( json.width );
if( ei.customWidth==ei.def.width )
ei.customWidth = null;
Expand All @@ -127,12 +133,20 @@ class EntityInstance {
return ei;
}

public inline function getAdjustedPivotX() {
return (def.flipAroundPivot && M.hasBit(flips, 0)) ? ((width - 1) / width) - def.pivotX : def.pivotX;
}

public inline function getAdjustedPivotY() {
return (def.flipAroundPivot && M.hasBit(flips, 1)) ? ((height - 1) / height) - def.pivotY : def.pivotY;
}

public inline function getCx(ld:data.def.LayerDef) {
return Std.int( ( x + (def.pivotX==1 ? -1 : 0) ) / ld.gridSize );
return Std.int( ( x + (getAdjustedPivotX()==1 ? -1 : 0) ) / ld.gridSize );
}

public inline function getCy(ld:data.def.LayerDef) {
return Std.int( ( y + (def.pivotY==1 ? -1 : 0) ) / ld.gridSize );
return Std.int( ( y + (getAdjustedPivotY()==1 ? -1 : 0) ) / ld.gridSize );
}

public inline function getPointOriginX(ld:data.def.LayerDef) {
Expand Down Expand Up @@ -365,4 +379,4 @@ class EntityInstance {
}


}
}
Loading