From faa7c496272ef528ef6ee4c57cd564686afcb65f Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sat, 11 Jan 2014 18:05:50 +0100 Subject: [PATCH] Multi page atlas support for Flash and Starling. http://www.esotericsoftware.com/forum/viewtopic.php?p=9409#p9409 --- spine-as3/spine-as3-example/src/Main.as | 4 +- ...TextureLoader.as => FlashTextureLoader.as} | 45 +++++++++++----- .../src/AtlasExample.as | 4 +- ...tureLoader.as => StarlingTextureLoader.as} | 53 +++++++++++++------ 4 files changed, 72 insertions(+), 34 deletions(-) rename spine-as3/spine-as3/src/spine/flash/{SingleTextureLoader.as => FlashTextureLoader.as} (65%) rename spine-starling/spine-starling/src/spine/starling/{SingleTextureLoader.as => StarlingTextureLoader.as} (69%) diff --git a/spine-as3/spine-as3-example/src/Main.as b/spine-as3/spine-as3-example/src/Main.as index 40e453139..091ea0038 100644 --- a/spine-as3/spine-as3-example/src/Main.as +++ b/spine-as3/spine-as3-example/src/Main.as @@ -41,7 +41,7 @@ import spine.SkeletonJson; import spine.animation.AnimationStateData; import spine.atlas.Atlas; import spine.attachments.AtlasAttachmentLoader; -import spine.flash.SingleTextureLoader; +import spine.flash.FlashTextureLoader; import spine.flash.SkeletonAnimation; [SWF(width = "640", height = "480", frameRate = "60", backgroundColor = "#dddddd")] @@ -58,7 +58,7 @@ public class Main extends Sprite { private var skeleton:SkeletonAnimation; public function Main () { - var atlas:Atlas = new Atlas(new SpineboyAtlas(), new SingleTextureLoader(new SpineboyAtlasTexture())); + var atlas:Atlas = new Atlas(new SpineboyAtlas(), new FlashTextureLoader(new SpineboyAtlasTexture())); var json:SkeletonJson = new SkeletonJson(new AtlasAttachmentLoader(atlas)); var skeletonData:SkeletonData = json.readSkeletonData(new SpineboyJson()); diff --git a/spine-as3/spine-as3/src/spine/flash/SingleTextureLoader.as b/spine-as3/spine-as3/src/spine/flash/FlashTextureLoader.as similarity index 65% rename from spine-as3/spine-as3/src/spine/flash/SingleTextureLoader.as rename to spine-as3/spine-as3/src/spine/flash/FlashTextureLoader.as index 0c303f38c..a2d9aa7ff 100644 --- a/spine-as3/spine-as3/src/spine/flash/SingleTextureLoader.as +++ b/spine-as3/spine-as3/src/spine/flash/FlashTextureLoader.as @@ -39,23 +39,42 @@ import spine.atlas.AtlasPage; import spine.atlas.AtlasRegion; import spine.atlas.TextureLoader; -public class SingleTextureLoader implements TextureLoader { - private var pageBitmapData:BitmapData; +public class FlashTextureLoader implements TextureLoader { + public var bitmapDatas:Object = {}; + public var singleBitmapData:BitmapData; - /** @param object A Bitmap or BitmapData. */ - public function SingleTextureLoader (object:*) { - if (object is BitmapData) - pageBitmapData = BitmapData(object); - else if (object is Bitmap) - pageBitmapData = Bitmap(object).bitmapData; - else - throw new ArgumentError("object must be a Bitmap or BitmapData."); + /** @param bitmaps A Bitmap or BitmapData for an atlas that has only one page, or for a multi page atlas an object where the + * key is the image path and the value is the Bitmap or BitmapData. */ + public function FlashTextureLoader (bitmaps:Object) { + if (bitmaps is BitmapData) { + singleBitmapData = BitmapData(bitmaps); + return; + } + if (bitmaps is Bitmap) { + singleBitmapData = Bitmap(bitmaps).bitmapData; + return; + } + + for (var path:* in bitmaps) { + var object:* = bitmaps[path]; + var bitmapData:BitmapData; + if (object is BitmapData) + bitmapData = BitmapData(object); + else if (object is Bitmap) + bitmapData = Bitmap(object).bitmapData; + else + throw new ArgumentError("Object for path \"" + path + "\" must be a Bitmap or BitmapData: " + object); + bitmapDatas[path] = bitmapData; + } } public function loadPage (page:AtlasPage, path:String) : void { - page.rendererObject = pageBitmapData; - page.width = pageBitmapData.width; - page.height = pageBitmapData.height; + var bitmapData:BitmapData = singleBitmapData || bitmapDatas[path]; + if (!bitmapData) + throw new ArgumentError("BitmapData not found with name: " + path); + page.rendererObject = bitmapData; + page.width = bitmapData.width; + page.height = bitmapData.height; } public function loadRegion (region:AtlasRegion) : void { diff --git a/spine-starling/spine-starling-example/src/AtlasExample.as b/spine-starling/spine-starling-example/src/AtlasExample.as index df43a5a2f..88acb8b50 100644 --- a/spine-starling/spine-starling-example/src/AtlasExample.as +++ b/spine-starling/spine-starling-example/src/AtlasExample.as @@ -6,7 +6,7 @@ import spine.SkeletonJson; import spine.animation.AnimationStateData; import spine.atlas.Atlas; import spine.attachments.AtlasAttachmentLoader; -import spine.starling.SingleTextureLoader; +import spine.starling.StarlingTextureLoader; import spine.starling.SkeletonAnimation; import spine.starling.StarlingAtlasAttachmentLoader; @@ -31,7 +31,7 @@ public class AtlasExample extends Sprite { private var skeleton:SkeletonAnimation; public function AtlasExample () { - var atlas:Atlas = new Atlas(new SpineboyAtlasFile(), new SingleTextureLoader(new SpineboyAtlasTexture())); + var atlas:Atlas = new Atlas(new SpineboyAtlasFile(), new StarlingTextureLoader(new SpineboyAtlasTexture())); var json:SkeletonJson = new SkeletonJson(new AtlasAttachmentLoader(atlas)); var skeletonData:SkeletonData = json.readSkeletonData(new SpineboyJson()); diff --git a/spine-starling/spine-starling/src/spine/starling/SingleTextureLoader.as b/spine-starling/spine-starling/src/spine/starling/StarlingTextureLoader.as similarity index 69% rename from spine-starling/spine-starling/src/spine/starling/SingleTextureLoader.as rename to spine-starling/spine-starling/src/spine/starling/StarlingTextureLoader.as index 33c282a92..db78de068 100644 --- a/spine-starling/spine-starling/src/spine/starling/SingleTextureLoader.as +++ b/spine-starling/spine-starling/src/spine/starling/StarlingTextureLoader.as @@ -44,23 +44,42 @@ import spine.atlas.TextureLoader; import starling.textures.SubTexture; import starling.textures.Texture; -public class SingleTextureLoader implements TextureLoader { - private var pageBitmapData:BitmapData; - - /** @param object A Bitmap or BitmapData. */ - public function SingleTextureLoader (object:*) { - if (object is BitmapData) - pageBitmapData = BitmapData(object); - else if (object is Bitmap) - pageBitmapData = Bitmap(object).bitmapData; - else - throw new ArgumentError("object must be a Bitmap or BitmapData."); +public class StarlingTextureLoader implements TextureLoader { + public var bitmapDatas:Object = {}; + public var singleBitmapData:BitmapData; + + /** @param bitmaps A Bitmap or BitmapData for an atlas that has only one page, or for a multi page atlas an object where the + * key is the image path and the value is the Bitmap or BitmapData. */ + public function StarlingTextureLoader (bitmaps:Object) { + if (bitmaps is BitmapData) { + singleBitmapData = BitmapData(bitmaps); + return; + } + if (bitmaps is Bitmap) { + singleBitmapData = Bitmap(bitmaps).bitmapData; + return; + } + + for (var path:* in bitmaps) { + var object:* = bitmaps[path]; + var bitmapData:BitmapData; + if (object is BitmapData) + bitmapData = BitmapData(object); + else if (object is Bitmap) + bitmapData = Bitmap(object).bitmapData; + else + throw new ArgumentError("Object for path \"" + path + "\" must be a Bitmap or BitmapData: " + object); + bitmapDatas[path] = bitmapData; + } } - + public function loadPage (page:AtlasPage, path:String) : void { - page.rendererObject = Texture.fromBitmapData(pageBitmapData); - page.width = pageBitmapData.width; - page.height = pageBitmapData.height; + var bitmapData:BitmapData = singleBitmapData || bitmapDatas[path]; + if (!bitmapData) + throw new ArgumentError("BitmapData not found with name: " + path); + page.rendererObject = Texture.fromBitmapData(bitmapData); + page.width = bitmapData.width; + page.height = bitmapData.height; } public function loadRegion (region:AtlasRegion) : void { @@ -78,9 +97,9 @@ public class SingleTextureLoader implements TextureLoader { } region.rendererObject = image; } - + public function unloadPage (page:AtlasPage) : void { - BitmapData(pageBitmapData).dispose(); + BitmapData(page.rendererObject).dispose(); } }