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

- fixed issue with rotated true tiles #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 41 additions & 3 deletions spritesheet/importers/TexturePackerImporter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,32 @@ class TexturePackerImporter {
public function parse(path:String, bitmapData:BitmapData, exp:EReg = null):Spritesheet {

var json = Json.parse(path);
if (!Std.is(json.frames, Array))
{
trace("Converting json frames map to array...");
json = convertFramesToArray(json);
}

var tpFrames:Array<TPFrame> = parseJsonFrames(json);
var behaviorNames = buildBehaviorMap(tpFrames, exp);
return generateSpriteSheetForBehaviors(bitmapData, behaviorNames);
}

private function convertFramesToArray(json:Dynamic):Dynamic
{
var newJson:Dynamic = {
frames: [],
meta: json.meta
};

for (field in Reflect.fields(json.frames))
{
var frame:Dynamic = Reflect.field(json.frames, field);
frame.filename = field;
newJson.frames.push(frame);
}

return newJson;
}

public function parseJsonFrames(json:Dynamic):Array<TPFrame> {
Expand Down Expand Up @@ -72,20 +94,32 @@ class TexturePackerImporter {
exp = (exp == null) ? ~// : exp;
var nameMap = new StringMap<Array<TPFrame>>();

var count:Int = 0;

for (frame in frames) {

exp.match(frame.filename);
var behaviorName:String = exp.matched(0);
if (!nameMap.exists(behaviorName)) {

nameMap.set(behaviorName, new Array<TPFrame>());

count++;
}

var behaviors:Array<TPFrame> = nameMap.get(behaviorName);
behaviors.push(frame);
}

if (count == 1)
{
var behaviorName:String = "default";
nameMap.set(behaviorName, new Array<TPFrame>());

for (frame in frames) {
nameMap.get(behaviorName).push(frame);
}
}

return nameMap;
}

Expand All @@ -103,7 +137,11 @@ class TexturePackerImporter {
for (i in 0...frames.length) {

var tpFrame:TPFrame = frames[i];
var sFrame = new SpritesheetFrame ( tpFrame.frame.x, tpFrame.frame.y, tpFrame.frame.w, tpFrame.frame.h );

var w:Int = tpFrame.rotated ? tpFrame.frame.h : tpFrame.frame.w;
var h:Int = tpFrame.rotated ? tpFrame.frame.w : tpFrame.frame.h;

var sFrame = new SpritesheetFrame ( tpFrame.frame.x, tpFrame.frame.y, w, h );
sFrame.id = i;

if( tpFrame.trimmed )
Expand All @@ -116,7 +154,7 @@ class TexturePackerImporter {

indexes.push(allFrames.length);
allFrames.push(sFrame);
allRects.push(new Rectangle(tpFrame.frame.x, tpFrame.frame.y, tpFrame.frame.w, tpFrame.frame.h));
allRects.push(new Rectangle(tpFrame.frame.x, tpFrame.frame.y, w, h));
}

if( isIgnoredBehavior(key) )
Expand Down