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

Spritesheet #205

Open
wants to merge 2 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
89 changes: 69 additions & 20 deletions src/flambe/Entity.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ using Lambda;

/** This entity's first component. */
public var firstComponent (default, null) :Component = null;

//Use setZOrder() to set this value instead of zOrder = x
public var zOrder : Int = 0;
public var orderOfArrival : Int = 1;

public static var globalOrderOfArrival : Int = 1;

public function new ()
{
Expand Down Expand Up @@ -165,39 +171,82 @@ using Lambda;
return untyped _compMap[name];
}

/**
/**
* Adds a child to this entity.
* @param append Whether to add the entity to the end or beginning of the child list.
* @returns This instance, for chaining.
*/
public function addChild (entity :Entity, append :Bool=true)
public function addChild (entity :Entity, append :Bool = true, ?zOrder : Int)
{
//trace("entity.addChild = " + zOrder);
if (entity.parent != null) {
entity.parent.removeChild(entity);
}
entity.parent = this;

if (append) {
// Append it to the child list
var tail = null, p = firstChild;
while (p != null) {
tail = p;
p = p.next;
}
if (tail != null) {
tail.next = entity;
} else {
firstChild = entity;
}

} else {
// Prepend it to the child list
entity.next = firstChild;
firstChild = entity;
}
if (append) {
var tail = null, p = firstChild;

while (p != null) {
tail = p;
p = p.next;
}
if (tail != null) {
if (zOrder == null) {
zOrder = tail.zOrder;
}
if (tail.zOrder <= zOrder) {
tail.next = entity;
} else {
var p = firstChild;
var pre : Entity = null;
while (p != null) {
if (p.zOrder > zOrder) {
if (pre != null) {
pre.next = entity;
entity.next = p;
} else {
entity.next = firstChild;
firstChild = entity;
}
break;
} else {
pre = p;
p = p.next;
}

}
}
} else {
firstChild = entity;
if (zOrder == null) {
zOrder = 0;
}
}
} else {
if (firstChild == null) {
zOrder = 0;
} else {
zOrder = firstChild.zOrder - 1;
}
entity.next = firstChild;
firstChild = entity;
}

entity.zOrder = zOrder;

return this;
}

public function setZOrder(z : Int) {
if (this.zOrder == z) {
return;
} else {
this.zOrder = z;
this.parent.addChild(this, true, this.zOrder);
}

}

public function removeChild (entity :Entity)
{
Expand Down
41 changes: 41 additions & 0 deletions src/flambe/display/PlistEntry.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package flambe.display;

/**
* ...
* @author Ang Li(李昂)
*/
class PlistEntry
{

public var name : String;
public var x : Float;
public var y : Float;
public var width : Float;
public var height : Float;
public var sourceColorX : Float;
public var sourceColorY : Float;
public var rotated : Bool;


public function new(?entry : PlistEntry) {
if (entry == null) {
return;
}
this.name = entry.name;
this.x = entry.x;
this.y = entry.y;
this.width = entry.width;
this.height = entry.height;
this.sourceColorX = entry.sourceColorX;
this.sourceColorY = entry.sourceColorY;
this.rotated = entry.rotated;
}

public function toString() : String {
var ret : String = name + "," + Std.string(x) + "," + Std.string(y) + "," +
Std.string(sourceColorX) + "," + Std.string(sourceColorY) + "," + Std.string(rotated);
return ret;

}

}
93 changes: 93 additions & 0 deletions src/flambe/display/PlistParser.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package flambe.display;
import flambe.asset.AssetPack;

/**
* Parser for Texture Packer
* @author Ang Li(李昂)
*/
class PlistParser
{
public function new()
{
}


public static function parse(xmlDoc : Xml) : Array<PlistEntry> {
var plist = new Array<PlistEntry>();
var frames : Xml = null;
var metadata : Xml = null;
var index : Int = 0;
for (x in xmlDoc.firstElement().firstElement().elements()) {
if (x.firstChild().nodeValue == "frames") {
index = 1;
} else if (x.nodeName == "dict" && index == 1) {
frames = x;
} else if (x.firstChild().nodeValue == "metadata") {
index = 2;
} else if (x.nodeName == "dict" && index == 2) {
metadata = x;
}
}

index = 1;
var tempEntry : PlistEntry = new PlistEntry();
var tempKey : String = "";
for (x in frames.elements()) {
if (x.nodeName == "key" && index == 1) {

tempEntry.name = x.firstChild().nodeValue;
index = 2;
} else if (x.nodeName == "dict" && index == 2) {
index = 1;
for (info in x.elements()) {
if (info.nodeName == "key") {
tempKey = info.firstChild().nodeValue;
} else {
switch(tempKey) {
case "frame" :
var s : Array<Float> = parseString(info.firstChild().nodeValue);
tempEntry.x = s[0];
tempEntry.y = s[1];
tempEntry.width = s[2];
tempEntry.height = s[3];

case "sourceColorRect":
var s : Array<Float> = parseString(info.firstChild().nodeValue);
tempEntry.sourceColorX = s[0];
tempEntry.sourceColorY = s[1];

case "rotated":
if (info.nodeName == "true") {
tempEntry.rotated = true;
} else {
tempEntry.rotated = false;
}
}
}
}
plist.push(new PlistEntry(tempEntry));
}
}
return plist;
}

public static function parseString(str : String) : Array<Float> {
var ret : Array<Float> = new Array<Float>();
var index : Int;
var temp : String;
var buf : StringBuf = new StringBuf();

for (i in 0...str.length) {
if (str.charAt(i) != "{" && str.charAt(i) != "}") {
buf.addSub(str.charAt(i), 0);
}
}

var newString : String = buf.toString();
for (i in newString.split(",")) {
ret.push(Std.parseFloat(i));
}

return ret;
}
}
130 changes: 130 additions & 0 deletions src/flambe/display/SpriteFrame.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package flambe.display;

import flambe.display.Texture;
import flambe.math.Point;
import flambe.math.Rectangle;
import flambe.math.Size;

/**
* ...
* @author Ang Li(李昂)
*/
class SpriteFrame
{

var _offset : Point;
var _originalSize : Size;
var _rectInPixels : Rectangle;
var _rotated : Bool;
var _rect : Rectangle;
var _offsetInPixels : Point;
var _originalSizeInPixels : Size;
var _texture : Texture;
var _textureFilname : String;

public function new() {
this._offset = new Point(0, 0);
this._offsetInPixels = new Point(0, 0);
this._originalSize = new Size(0, 0);
this._rectInPixels = new Rectangle(0, 0, 0, 0);
this._rect = new Rectangle(0, 0, 0, 0);
this._originalSizeInPixels = new Size(0, 0);
this._textureFilname = "";
}

public function getRectInPixels() : Rectangle {
return this._rectInPixels;
}

public function setRectInPixels(rectInPixels : Rectangle) {
this._rectInPixels = rectInPixels;
this._rect = rectInPixels;
}

public function isRotated() : Bool {
return this._rotated;
}

public function setRotated(bRotated : Bool) {
this._rotated = bRotated;
}

public function getRect() : Rectangle {
return this._rect;
}

public function setRect(rect :Rectangle) {
this._rect = rect;
this._rectInPixels = rect;
}

public function getOffsetInPixels() : Point {
return new Point(this._offsetInPixels.x, this._offsetInPixels.y);
}

public function setOffsetInPixels(offsetInPixels : Point) {
this._offsetInPixels = offsetInPixels;
this._offset = offsetInPixels;
}

public function getOriginalSizeInPixels() : Size {
return this._originalSizeInPixels;
}

public function setOriginalSizePixels(sizeInPixels : Size) {
this._originalSizeInPixels = sizeInPixels;
}

public function getOriginalSize() : Size {
return new Size(this._originalSize.width, this._originalSize.height);
}

public function setOriginalSize(sizeInPixels : Size) {
this._originalSize = sizeInPixels;
}

public function getTexture() : Texture {
if (this._texture != null) {
return this._texture;
}
return null;
}

public function setTexture(texture : Texture) {
if (this._texture != texture) {
this._texture = texture;
}
}

public function getOffset() : Point {
return new Point(this._offset.x, this._offset.y);
}

public function setOffset(offsets : Point) {
this._offset = offsets;
}

public function initWithTexture(texture : Texture, rect : Rectangle, rotated : Bool, offset : Point, originalSize : Size) : Bool{
this._texture = texture;
this._rectInPixels = rect;
this._rect = rect;
this._offsetInPixels = offset;
this._offset = offset;
this._originalSizeInPixels = originalSize;
this._originalSize = originalSize;
this._rotated = rotated;
return true;
}

public function toString() : String {
var ret : String = _offset.x + "," + _offset.y + "," + isRotated() + "," + _rect.x + "," + _rect.y + "," + _rect.width + "," + _rect.height;
return ret;
}

public static function createWithTexture(texture : Texture, rect : Rectangle, ?rotated : Bool, ?offset : Point, ?originalSize : Size) : SpriteFrame {
var spriteFrame : SpriteFrame = new SpriteFrame();
spriteFrame.initWithTexture(texture, rect, rotated, offset, originalSize);
return spriteFrame;
}

}
Loading