From eed1a3e30ab74f6241c3b9529188ad17f95342b5 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Thu, 14 Sep 2023 15:05:02 +0200 Subject: [PATCH] [haxe] AssetsTextureLoader is now StarlingTextureLoader, remove TextureAtlas.from, change SkeletonData.from to not use OpenFL Assets. --- spine-haxe/example/src/BasicExample.hx | 8 +++++--- spine-haxe/example/src/SequenceExample.hx | 6 ++++-- spine-haxe/spine-haxe/spine/SkeletonData.hx | 15 +++++++-------- spine-haxe/spine-haxe/spine/atlas/TextureAtlas.hx | 11 ----------- .../StarlingTextureLoader.hx} | 12 ++++++++---- 5 files changed, 24 insertions(+), 28 deletions(-) rename spine-haxe/spine-haxe/spine/{atlas/AssetsTextureLoader.hx => starling/StarlingTextureLoader.hx} (90%) diff --git a/spine-haxe/example/src/BasicExample.hx b/spine-haxe/example/src/BasicExample.hx index 473a061fa..b59a7034d 100644 --- a/spine-haxe/example/src/BasicExample.hx +++ b/spine-haxe/example/src/BasicExample.hx @@ -27,20 +27,22 @@ * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ +import openfl.utils.Assets; import spine.SkeletonData; import spine.animation.AnimationStateData; import spine.atlas.TextureAtlas; import spine.starling.SkeletonSprite; +import spine.starling.StarlingTextureLoader; import starling.core.Starling; import starling.events.TouchEvent; import starling.events.TouchPhase; class BasicExample extends Scene { - var loadBinary = false; + var loadBinary = true; public function load():Void { - var atlas = TextureAtlas.fromAssets("assets/raptor.atlas"); - var skeletondata = SkeletonData.fromAssets("assets/raptor-pro" + (loadBinary ? ".skel" : ".json"), atlas); + var atlas = new TextureAtlas(Assets.getText("assets/raptor.atlas"), new StarlingTextureLoader("assets/raptor-pro.atlas")); + var skeletondata = SkeletonData.from(loadBinary ? Assets.getBytes("assets/raptor-pro.skel") : Assets.getText("assets/raptor-pro.json"), atlas); var animationStateData = new AnimationStateData(skeletondata); animationStateData.defaultMix = 0.25; diff --git a/spine-haxe/example/src/SequenceExample.hx b/spine-haxe/example/src/SequenceExample.hx index 507881979..721d044fc 100644 --- a/spine-haxe/example/src/SequenceExample.hx +++ b/spine-haxe/example/src/SequenceExample.hx @@ -27,10 +27,12 @@ * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ +import openfl.utils.Assets; import spine.SkeletonData; import spine.animation.AnimationStateData; import spine.atlas.TextureAtlas; import spine.starling.SkeletonSprite; +import spine.starling.StarlingTextureLoader; import starling.core.Starling; import starling.events.TouchEvent; import starling.events.TouchPhase; @@ -39,8 +41,8 @@ class SequenceExample extends Scene { var loadBinary = false; public function load():Void { - var atlas = TextureAtlas.fromAssets("assets/dragon.atlas"); - var skeletondata = SkeletonData.fromAssets("assets/dragon-ess" + (loadBinary ? ".skel" : ".json"), atlas); + var atlas = new TextureAtlas(Assets.getText("assets/dragon.atlas"), new StarlingTextureLoader("assets/dragon.atlas")); + var skeletondata = SkeletonData.from(loadBinary ? Assets.getBytes("assets/dragon-ess.skel") : Assets.getText("assets/dragon-ess.json"), atlas); var animationStateData = new AnimationStateData(skeletondata); animationStateData.defaultMix = 0.25; diff --git a/spine-haxe/spine-haxe/spine/SkeletonData.hx b/spine-haxe/spine-haxe/spine/SkeletonData.hx index a672d45af..7bb0ed4d8 100644 --- a/spine-haxe/spine-haxe/spine/SkeletonData.hx +++ b/spine-haxe/spine-haxe/spine/SkeletonData.hx @@ -29,6 +29,7 @@ package spine; +import haxe.io.Bytes; import openfl.utils.Assets; import spine.animation.Animation; import spine.atlas.TextureAtlas; @@ -57,19 +58,17 @@ class SkeletonData { public var imagesPath:String; public var audioPath:String; - public static function fromAssets(path:String, atlas:TextureAtlas, scale:Float = 1.0):SkeletonData { - if (StringTools.endsWith(path, ".skel")) { - var byteData = Assets.getBytes(path); + public static function from(data:Dynamic, atlas:TextureAtlas, scale:Float = 1.0):SkeletonData { + if (Std.isOfType(data, Bytes)) { var loader = new SkeletonBinary(new AtlasAttachmentLoader(atlas)); loader.scale = scale; - return loader.readSkeletonData(byteData); - } else if (StringTools.endsWith(path, ".json")) { - var jsonData = Assets.getText(path); + return loader.readSkeletonData(cast(data, Bytes)); + } else if (Std.isOfType(data, String)) { var loader = new SkeletonJson(new AtlasAttachmentLoader(atlas)); loader.scale = scale; - return loader.readSkeletonData(jsonData); + return loader.readSkeletonData(cast(data, String)); } else { - throw new SpineException("Path of skeleton data file must end with .json or .skel"); + throw new SpineException("Data must either be a String (.json) or Bytes (.skel) instance."); } } diff --git a/spine-haxe/spine-haxe/spine/atlas/TextureAtlas.hx b/spine-haxe/spine-haxe/spine/atlas/TextureAtlas.hx index c1b328963..d60bd3204 100644 --- a/spine-haxe/spine-haxe/spine/atlas/TextureAtlas.hx +++ b/spine-haxe/spine-haxe/spine/atlas/TextureAtlas.hx @@ -37,17 +37,6 @@ class TextureAtlas { private var regions = new Array(); private var textureLoader:TextureLoader; - public static function fromAssets(path:String) { - var basePath = ""; - var slashIndex = path.lastIndexOf("/"); - if (slashIndex != -1) { - basePath = path.substring(0, slashIndex); - } - - var textureLoader = new AssetsTextureLoader(basePath); - return new TextureAtlas(Assets.getText(path), textureLoader); - } - /** @param object A String or ByteArray. */ public function new(atlasText:String, textureLoader:TextureLoader) { if (atlasText == null) { diff --git a/spine-haxe/spine-haxe/spine/atlas/AssetsTextureLoader.hx b/spine-haxe/spine-haxe/spine/starling/StarlingTextureLoader.hx similarity index 90% rename from spine-haxe/spine-haxe/spine/atlas/AssetsTextureLoader.hx rename to spine-haxe/spine-haxe/spine/starling/StarlingTextureLoader.hx index a99fbb54e..44e91c559 100644 --- a/spine-haxe/spine-haxe/spine/atlas/AssetsTextureLoader.hx +++ b/spine-haxe/spine-haxe/spine/starling/StarlingTextureLoader.hx @@ -27,18 +27,22 @@ * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ -package spine.atlas; +package spine.starling; import starling.textures.Texture; import spine.atlas.TextureAtlasRegion; import spine.atlas.TextureAtlasPage; import spine.atlas.TextureLoader; -class AssetsTextureLoader implements TextureLoader { +class StarlingTextureLoader implements TextureLoader { private var basePath:String; - public function new(basePath:String) { - this.basePath = basePath; + public function new(atlasPath:String) { + basePath = ""; + var slashIndex = atlasPath.lastIndexOf("/"); + if (slashIndex != -1) { + basePath = atlasPath.substring(0, slashIndex); + } } public function loadPage(page:TextureAtlasPage, path:String) {