diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/FrameInfo.as b/FrameInfo.as index 9e2c773..f74604c 100755 --- a/FrameInfo.as +++ b/FrameInfo.as @@ -5,11 +5,16 @@ public class FrameInfo { public var frame:BitmapData; - public var rect:Rectangle = new Rectangle(); - public var sheetIndex:int = 0; - public function FrameInfo(b:BitmapData) + public var drawMatrix:Matrix; + public var sourceRect:Rectangle; + + public var rect:Rectangle; + public var sheetIndex:int; + public function FrameInfo(b:BitmapData, drawMatrix:Matrix, sourceRect:Rectangle) { frame = b; + this.drawMatrix = drawMatrix; + this.sourceRect = sourceRect; } } } \ No newline at end of file diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/FrameInfo.as b/FrameInfo.as index 9e2c773..f74604c 100755 --- a/FrameInfo.as +++ b/FrameInfo.as @@ -5,11 +5,16 @@ public class FrameInfo { public var frame:BitmapData; - public var rect:Rectangle = new Rectangle(); - public var sheetIndex:int = 0; - public function FrameInfo(b:BitmapData) + public var drawMatrix:Matrix; + public var sourceRect:Rectangle; + + public var rect:Rectangle; + public var sheetIndex:int; + public function FrameInfo(b:BitmapData, drawMatrix:Matrix, sourceRect:Rectangle) { frame = b; + this.drawMatrix = drawMatrix; + this.sourceRect = sourceRect; } } } \ No newline at end of file diff --git a/GraphicExportDef.as b/GraphicExportDef.as index 12ca505..e4202f3 100755 --- a/GraphicExportDef.as +++ b/GraphicExportDef.as @@ -24,6 +24,9 @@ private var _padding:int = 0; public function get padding():int { return _padding; } + private var _scale:Number = 0; + public function get scale():Number { return _scale; } + private var input:ByteArray; private var inputLoader:Loader = null; @@ -45,12 +48,15 @@ _exportType = parseInt(details.type); _name = "" + details.name; input = Base64.decode("" + details.input); + _padding = details.hasOwnProperty("padding") ? parseInt(details.padding) : 0; + _scale = details.hasOwnProperty("scale") ? parseFloat(details.scale) : 1; _valid = true; } public function Fail(err:String) { parentJob.FailMessage("(" + index + ")" + err); + _valid = false; if (callback != null) callback(); } @@ -110,7 +116,6 @@ } } - private var packer:FramePacker; private function FramesDone(success) { if (!success) @@ -118,18 +123,6 @@ Fail("couldn't get source frames"); return; } - packer = new FramePacker(frames.GetFrames()); - packer.Pack(PackDone); - } - - private function PackDone(success) - { - if (!success) - { - Fail("couldn't pack frames"); - return; - } - Done(); } @@ -141,5 +134,15 @@ { this.callback(); } + + public function GetFrames():Array + { + return frames.GetFrames(); + } + + public function GetData() + { + return frames.GetData(); + } } } \ No newline at end of file diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/FrameInfo.as b/FrameInfo.as index 9e2c773..f74604c 100755 --- a/FrameInfo.as +++ b/FrameInfo.as @@ -5,11 +5,16 @@ public class FrameInfo { public var frame:BitmapData; - public var rect:Rectangle = new Rectangle(); - public var sheetIndex:int = 0; - public function FrameInfo(b:BitmapData) + public var drawMatrix:Matrix; + public var sourceRect:Rectangle; + + public var rect:Rectangle; + public var sheetIndex:int; + public function FrameInfo(b:BitmapData, drawMatrix:Matrix, sourceRect:Rectangle) { frame = b; + this.drawMatrix = drawMatrix; + this.sourceRect = sourceRect; } } } \ No newline at end of file diff --git a/GraphicExportDef.as b/GraphicExportDef.as index 12ca505..e4202f3 100755 --- a/GraphicExportDef.as +++ b/GraphicExportDef.as @@ -24,6 +24,9 @@ private var _padding:int = 0; public function get padding():int { return _padding; } + private var _scale:Number = 0; + public function get scale():Number { return _scale; } + private var input:ByteArray; private var inputLoader:Loader = null; @@ -45,12 +48,15 @@ _exportType = parseInt(details.type); _name = "" + details.name; input = Base64.decode("" + details.input); + _padding = details.hasOwnProperty("padding") ? parseInt(details.padding) : 0; + _scale = details.hasOwnProperty("scale") ? parseFloat(details.scale) : 1; _valid = true; } public function Fail(err:String) { parentJob.FailMessage("(" + index + ")" + err); + _valid = false; if (callback != null) callback(); } @@ -110,7 +116,6 @@ } } - private var packer:FramePacker; private function FramesDone(success) { if (!success) @@ -118,18 +123,6 @@ Fail("couldn't get source frames"); return; } - packer = new FramePacker(frames.GetFrames()); - packer.Pack(PackDone); - } - - private function PackDone(success) - { - if (!success) - { - Fail("couldn't pack frames"); - return; - } - Done(); } @@ -141,5 +134,15 @@ { this.callback(); } + + public function GetFrames():Array + { + return frames.GetFrames(); + } + + public function GetData() + { + return frames.GetData(); + } } } \ No newline at end of file diff --git a/Job.as b/Job.as index 9963ae1..1cd632c 100755 --- a/Job.as +++ b/Job.as @@ -1,5 +1,9 @@ package { + import flash.utils.*; + import flash.geom.*; + import flash.display.*; + import com.adobe.images.PNGEncoderWithAlpha; public class Job { private var callback; @@ -30,6 +34,17 @@ Exporter.Instance.Print("Failure: job " + id + ": " + err); } + public function WarnMessage(err:String) + { + Exporter.Instance.Print("Warning: job " + id + ": " + err); + } + + private function Fail(err:String) + { + FailMessage(err); + callback(); + } + private var goIndex = -1; public function Go(callback) { @@ -49,9 +64,61 @@ } else { - Done(); + Pack(); } } + + private var allFrames:Array; //[FrameInfo] + private var packer:FramePacker; + private function Pack() + { + var allFrames:Array = new Array(); + for (var i in graphics) + { + if (graphics[i].valid) + { + allFrames = allFrames.concat(graphics[i].GetFrames()); + } + } + packer = new FramePacker(allFrames); + packer.Pack(PackDone); + } + + private var sheets:Array; //[BitmapData] + private var sheetBytes:Array; //[ByteArray] + + private function PackDone(success) + { + if (!success) + { + Fail("couldn't pack exported frames to sheets"); + return; + } + + var i; + + sheets = new Array(); + sheetBytes = new Array(); + var sheetAllocators:Array = packer.GetSheets(); + for (i in sheetAllocators) + { + var allocator:RectanglePacker = sheetAllocators[i]; + var sheet:BitmapData = new BitmapData(allocator.width, allocator.height, true, 0x0); + sheets.push(sheet); + } + for (i in allFrames) + { + var frame:FrameInfo = allFrames[i]; + var sheet:BitmapData = sheets[frame.sheetIndex]; + sheet.copyPixels(frame.frame, frame.rect, frame.rect.topLeft); + } + for (i in sheets) + { + sheetBytes[i] = PNGEncoderWithAlpha.encode(sheets[i]); + } + + Done(); + } public function Done() { @@ -59,9 +126,29 @@ this.callback(); } - public function GetCompletedData() + public function GetResources():Array //[ByteArray] { - return {id:id}; + return sheetBytes; + } + + public function GetData() + { + var ret = {}; + for (var i in graphics) + { + if (graphics[i].valid) + { + if (ret[graphics[i].name] !== undefined) + { + WarnMessage("two graphics with same name: " + graphics[i].name); + } + else + { + ret[graphics[i].name] = graphics[i].GetData(); + } + } + } + return ret; } } } \ No newline at end of file diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/FrameInfo.as b/FrameInfo.as index 9e2c773..f74604c 100755 --- a/FrameInfo.as +++ b/FrameInfo.as @@ -5,11 +5,16 @@ public class FrameInfo { public var frame:BitmapData; - public var rect:Rectangle = new Rectangle(); - public var sheetIndex:int = 0; - public function FrameInfo(b:BitmapData) + public var drawMatrix:Matrix; + public var sourceRect:Rectangle; + + public var rect:Rectangle; + public var sheetIndex:int; + public function FrameInfo(b:BitmapData, drawMatrix:Matrix, sourceRect:Rectangle) { frame = b; + this.drawMatrix = drawMatrix; + this.sourceRect = sourceRect; } } } \ No newline at end of file diff --git a/GraphicExportDef.as b/GraphicExportDef.as index 12ca505..e4202f3 100755 --- a/GraphicExportDef.as +++ b/GraphicExportDef.as @@ -24,6 +24,9 @@ private var _padding:int = 0; public function get padding():int { return _padding; } + private var _scale:Number = 0; + public function get scale():Number { return _scale; } + private var input:ByteArray; private var inputLoader:Loader = null; @@ -45,12 +48,15 @@ _exportType = parseInt(details.type); _name = "" + details.name; input = Base64.decode("" + details.input); + _padding = details.hasOwnProperty("padding") ? parseInt(details.padding) : 0; + _scale = details.hasOwnProperty("scale") ? parseFloat(details.scale) : 1; _valid = true; } public function Fail(err:String) { parentJob.FailMessage("(" + index + ")" + err); + _valid = false; if (callback != null) callback(); } @@ -110,7 +116,6 @@ } } - private var packer:FramePacker; private function FramesDone(success) { if (!success) @@ -118,18 +123,6 @@ Fail("couldn't get source frames"); return; } - packer = new FramePacker(frames.GetFrames()); - packer.Pack(PackDone); - } - - private function PackDone(success) - { - if (!success) - { - Fail("couldn't pack frames"); - return; - } - Done(); } @@ -141,5 +134,15 @@ { this.callback(); } + + public function GetFrames():Array + { + return frames.GetFrames(); + } + + public function GetData() + { + return frames.GetData(); + } } } \ No newline at end of file diff --git a/Job.as b/Job.as index 9963ae1..1cd632c 100755 --- a/Job.as +++ b/Job.as @@ -1,5 +1,9 @@ package { + import flash.utils.*; + import flash.geom.*; + import flash.display.*; + import com.adobe.images.PNGEncoderWithAlpha; public class Job { private var callback; @@ -30,6 +34,17 @@ Exporter.Instance.Print("Failure: job " + id + ": " + err); } + public function WarnMessage(err:String) + { + Exporter.Instance.Print("Warning: job " + id + ": " + err); + } + + private function Fail(err:String) + { + FailMessage(err); + callback(); + } + private var goIndex = -1; public function Go(callback) { @@ -49,9 +64,61 @@ } else { - Done(); + Pack(); } } + + private var allFrames:Array; //[FrameInfo] + private var packer:FramePacker; + private function Pack() + { + var allFrames:Array = new Array(); + for (var i in graphics) + { + if (graphics[i].valid) + { + allFrames = allFrames.concat(graphics[i].GetFrames()); + } + } + packer = new FramePacker(allFrames); + packer.Pack(PackDone); + } + + private var sheets:Array; //[BitmapData] + private var sheetBytes:Array; //[ByteArray] + + private function PackDone(success) + { + if (!success) + { + Fail("couldn't pack exported frames to sheets"); + return; + } + + var i; + + sheets = new Array(); + sheetBytes = new Array(); + var sheetAllocators:Array = packer.GetSheets(); + for (i in sheetAllocators) + { + var allocator:RectanglePacker = sheetAllocators[i]; + var sheet:BitmapData = new BitmapData(allocator.width, allocator.height, true, 0x0); + sheets.push(sheet); + } + for (i in allFrames) + { + var frame:FrameInfo = allFrames[i]; + var sheet:BitmapData = sheets[frame.sheetIndex]; + sheet.copyPixels(frame.frame, frame.rect, frame.rect.topLeft); + } + for (i in sheets) + { + sheetBytes[i] = PNGEncoderWithAlpha.encode(sheets[i]); + } + + Done(); + } public function Done() { @@ -59,9 +126,29 @@ this.callback(); } - public function GetCompletedData() + public function GetResources():Array //[ByteArray] { - return {id:id}; + return sheetBytes; + } + + public function GetData() + { + var ret = {}; + for (var i in graphics) + { + if (graphics[i].valid) + { + if (ret[graphics[i].name] !== undefined) + { + WarnMessage("two graphics with same name: " + graphics[i].name); + } + else + { + ret[graphics[i].name] = graphics[i].GetData(); + } + } + } + return ret; } } } \ No newline at end of file diff --git a/RectanglePacker.as b/RectanglePacker.as index 1a5f673..b3862ce 100755 --- a/RectanglePacker.as +++ b/RectanglePacker.as @@ -8,11 +8,14 @@ public class RectanglePacker { - protected var w; - protected var h; + protected var w:int; + protected var h:int; protected var root:RectangleNode; public var NextAllocationID = 0; + public function get width():int { return w; } + public function get height():int { return h; } + public function RectanglePacker(w, h) { this.w = w; diff --git a/AnimatedSpriteGenerator.as b/AnimatedSpriteGenerator.as index 92590f8..34b0217 100755 --- a/AnimatedSpriteGenerator.as +++ b/AnimatedSpriteGenerator.as @@ -21,7 +21,21 @@ public function GetData() { - return {}; + return { + sequences:sequences.map( + function(s:Array, i, sArr) + { + return { + frames: s.map( + function(f:AnimatedSpriteFrame, i, fArr) + { + return {rect:f.info.rect, center:f.offset}; + } + ) + }; + } + ) + }; } public function AnimatedSpriteGenerator(src:MovieClip, def:GraphicExportDef) @@ -59,12 +73,14 @@ Utils.WeirdGotoFrame(m, i + 1); Utils.RecursivelyStop(m); - var bounds:Rectangle = Utils.GetAccurateBounds(m); + var bounds:Rectangle = Utils.GetAccurateBounds(m, new Matrix(def.scale, 0, 0, def.scale)); + + var drawMatrix:Matrix = new Matrix(def.scale, 0, 0, def.scale, -bounds.left + def.padding, -bounds.top + def.padding); var b:BitmapData = new BitmapData(bounds.width + def.padding * 2, bounds.height + def.padding * 2, true, 0x0); - b.drawWithQuality(m,new Matrix(1, 0, 0, 1, -bounds.left + def.padding, -bounds.top + def.padding), null, null, bounds, false, StageQuality.BEST); + b.drawWithQuality(m, drawMatrix, null, null, bounds, false, StageQuality.BEST); - var info:FrameInfo = new FrameInfo(b); + var info:FrameInfo = new FrameInfo(b, drawMatrix, bounds); var frame:AnimatedSpriteFrame = new AnimatedSpriteFrame(); frame.info = info; diff --git a/DebugExporter.swf b/DebugExporter.swf index eb90984..8663ca9 100755 --- a/DebugExporter.swf +++ b/DebugExporter.swf Binary files differ diff --git a/Exporter.as b/Exporter.as index e754de5..12ed4a7 100644 --- a/Exporter.as +++ b/Exporter.as @@ -158,7 +158,10 @@ private function JobComplete() { Trace("Done: " + activeJob.GetID()); - DriverCommand("done", activeJob.GetCompletedData()); + + var data = activeJob.GetData(); + var resources:Array = activeJob.GetResources().map(function(r:ByteArray, i, arr) { return Base64.encode(r); }); + DriverCommand("done", {id:activeJob.GetID(), data:data, resources:resources}); activeJob = null; CheckNewJob(); @@ -203,8 +206,9 @@ try { driver.readBytes(buffer, buffer.length, driver.bytesAvailable); - //Trace(buffer.length - buffer.position); - var msg = buffer.readUTF(); + //Trace(buffer.length - buffer.position); + var len:uint = buffer.readUnsignedInt(); + var msg = buffer.readUTFBytes(len); buffer.readBytes(swpBuffer); buffer.clear(); @@ -242,14 +246,17 @@ obj[k] = data[k]; } } - DriverSend(JSON.stringify(obj)); + var str:String = JSON.stringify(obj); + Trace("Sending: " + str.length); + DriverSend(str); } private function DriverSend(str:String) { if (driver != null) { - driver.writeUTF(str); + driver.writeUnsignedInt(uint(str.length)); + driver.writeUTFBytes(str); driver.flush(); } } diff --git a/FrameInfo.as b/FrameInfo.as index 9e2c773..f74604c 100755 --- a/FrameInfo.as +++ b/FrameInfo.as @@ -5,11 +5,16 @@ public class FrameInfo { public var frame:BitmapData; - public var rect:Rectangle = new Rectangle(); - public var sheetIndex:int = 0; - public function FrameInfo(b:BitmapData) + public var drawMatrix:Matrix; + public var sourceRect:Rectangle; + + public var rect:Rectangle; + public var sheetIndex:int; + public function FrameInfo(b:BitmapData, drawMatrix:Matrix, sourceRect:Rectangle) { frame = b; + this.drawMatrix = drawMatrix; + this.sourceRect = sourceRect; } } } \ No newline at end of file diff --git a/GraphicExportDef.as b/GraphicExportDef.as index 12ca505..e4202f3 100755 --- a/GraphicExportDef.as +++ b/GraphicExportDef.as @@ -24,6 +24,9 @@ private var _padding:int = 0; public function get padding():int { return _padding; } + private var _scale:Number = 0; + public function get scale():Number { return _scale; } + private var input:ByteArray; private var inputLoader:Loader = null; @@ -45,12 +48,15 @@ _exportType = parseInt(details.type); _name = "" + details.name; input = Base64.decode("" + details.input); + _padding = details.hasOwnProperty("padding") ? parseInt(details.padding) : 0; + _scale = details.hasOwnProperty("scale") ? parseFloat(details.scale) : 1; _valid = true; } public function Fail(err:String) { parentJob.FailMessage("(" + index + ")" + err); + _valid = false; if (callback != null) callback(); } @@ -110,7 +116,6 @@ } } - private var packer:FramePacker; private function FramesDone(success) { if (!success) @@ -118,18 +123,6 @@ Fail("couldn't get source frames"); return; } - packer = new FramePacker(frames.GetFrames()); - packer.Pack(PackDone); - } - - private function PackDone(success) - { - if (!success) - { - Fail("couldn't pack frames"); - return; - } - Done(); } @@ -141,5 +134,15 @@ { this.callback(); } + + public function GetFrames():Array + { + return frames.GetFrames(); + } + + public function GetData() + { + return frames.GetData(); + } } } \ No newline at end of file diff --git a/Job.as b/Job.as index 9963ae1..1cd632c 100755 --- a/Job.as +++ b/Job.as @@ -1,5 +1,9 @@ package { + import flash.utils.*; + import flash.geom.*; + import flash.display.*; + import com.adobe.images.PNGEncoderWithAlpha; public class Job { private var callback; @@ -30,6 +34,17 @@ Exporter.Instance.Print("Failure: job " + id + ": " + err); } + public function WarnMessage(err:String) + { + Exporter.Instance.Print("Warning: job " + id + ": " + err); + } + + private function Fail(err:String) + { + FailMessage(err); + callback(); + } + private var goIndex = -1; public function Go(callback) { @@ -49,9 +64,61 @@ } else { - Done(); + Pack(); } } + + private var allFrames:Array; //[FrameInfo] + private var packer:FramePacker; + private function Pack() + { + var allFrames:Array = new Array(); + for (var i in graphics) + { + if (graphics[i].valid) + { + allFrames = allFrames.concat(graphics[i].GetFrames()); + } + } + packer = new FramePacker(allFrames); + packer.Pack(PackDone); + } + + private var sheets:Array; //[BitmapData] + private var sheetBytes:Array; //[ByteArray] + + private function PackDone(success) + { + if (!success) + { + Fail("couldn't pack exported frames to sheets"); + return; + } + + var i; + + sheets = new Array(); + sheetBytes = new Array(); + var sheetAllocators:Array = packer.GetSheets(); + for (i in sheetAllocators) + { + var allocator:RectanglePacker = sheetAllocators[i]; + var sheet:BitmapData = new BitmapData(allocator.width, allocator.height, true, 0x0); + sheets.push(sheet); + } + for (i in allFrames) + { + var frame:FrameInfo = allFrames[i]; + var sheet:BitmapData = sheets[frame.sheetIndex]; + sheet.copyPixels(frame.frame, frame.rect, frame.rect.topLeft); + } + for (i in sheets) + { + sheetBytes[i] = PNGEncoderWithAlpha.encode(sheets[i]); + } + + Done(); + } public function Done() { @@ -59,9 +126,29 @@ this.callback(); } - public function GetCompletedData() + public function GetResources():Array //[ByteArray] { - return {id:id}; + return sheetBytes; + } + + public function GetData() + { + var ret = {}; + for (var i in graphics) + { + if (graphics[i].valid) + { + if (ret[graphics[i].name] !== undefined) + { + WarnMessage("two graphics with same name: " + graphics[i].name); + } + else + { + ret[graphics[i].name] = graphics[i].GetData(); + } + } + } + return ret; } } } \ No newline at end of file diff --git a/RectanglePacker.as b/RectanglePacker.as index 1a5f673..b3862ce 100755 --- a/RectanglePacker.as +++ b/RectanglePacker.as @@ -8,11 +8,14 @@ public class RectanglePacker { - protected var w; - protected var h; + protected var w:int; + protected var h:int; protected var root:RectangleNode; public var NextAllocationID = 0; + public function get width():int { return w; } + public function get height():int { return h; } + public function RectanglePacker(w, h) { this.w = w; diff --git a/driver.py b/driver.py index 70f82e6..bba07d6 100644 --- a/driver.py +++ b/driver.py @@ -57,7 +57,7 @@ def send_json(self, jsonObj): data = json.dumps(jsonObj) - datalen = struct.pack(">H", len(data)) + datalen = struct.pack(">I", len(data)) self.conn.send(datalen) self.conn.send(data) @@ -70,11 +70,11 @@ d = self.conn.recv(4096) if len(d): buf += d - if len(buf) >= 2: - buf_len = struct.unpack_from(">H", buf, 0)[0] - if len(buf) >= 2 + buf_len: - msg = buf[2:2+buf_len] - buf = buf[2+buf_len:] + if len(buf) >= 4: + buf_len = struct.unpack_from(">I", buf, 0)[0] + if len(buf) >= 4 + buf_len: + msg = buf[4:4+buf_len] + buf = buf[4+buf_len:] if not msg_callback(msg): quit = True else: