From e788b1ce653b755f6b979fbb1706eef0d6f65928 Mon Sep 17 00:00:00 2001 From: George Kurelic Date: Fri, 2 Feb 2024 10:14:38 -0600 Subject: [PATCH] use DynamicAccess instead of Hash (#3015) * use DynamicAccess instead of Hash * add isArray and isHash helpers * remove import --- flixel/graphics/atlas/AtlasBase.hx | 46 ++++++++++++++------- flixel/graphics/atlas/TexturePackerAtlas.hx | 2 +- flixel/graphics/frames/FlxAtlasFrames.hx | 9 ++-- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/flixel/graphics/atlas/AtlasBase.hx b/flixel/graphics/atlas/AtlasBase.hx index 92711a2018..f010411403 100644 --- a/flixel/graphics/atlas/AtlasBase.hx +++ b/flixel/graphics/atlas/AtlasBase.hx @@ -1,5 +1,7 @@ package flixel.graphics.atlas; +import haxe.DynamicAccess; + typedef AtlasBase = { frames:T @@ -62,23 +64,35 @@ typedef AtlasFrame = var spriteSourceSize:AtlasRect; } -/** - * Internal helper used to enumerate the fields of an atlas that has frame data keyed by frame names. - */ -abstract Hash(Dynamic) +abstract HashOrArray(Dynamic) from DynamicAccess from Array { - public inline function keyValueIterator():KeyValueIterator + public inline function isArray() + { + return (this is Array); + } + + public inline function isHash() + { + return !isArray(); + } + + @:to + public inline function toArray():Array + { + return this; + } + + @:to + public inline function toHash():DynamicAccess { - var keys = Reflect.fields(this).iterator(); - return { - hasNext: keys.hasNext, - next: () -> - { - final key = keys.next(); - return {key: key, value: Reflect.field(this, key)}; - } - }; + return this; + } + + public inline function iterator():Iterator + { + if (isArray()) + return toArray().iterator(); + else + return toHash().iterator(); } } - -typedef HashOrArray = flixel.util.typeLimit.OneOfTwo, Array>; diff --git a/flixel/graphics/atlas/TexturePackerAtlas.hx b/flixel/graphics/atlas/TexturePackerAtlas.hx index e29ae0dd68..c4fd42c549 100644 --- a/flixel/graphics/atlas/TexturePackerAtlas.hx +++ b/flixel/graphics/atlas/TexturePackerAtlas.hx @@ -7,6 +7,6 @@ typedef TexturePackerAtlasFrame = AtlasFrame & ?duration:Int } -typedef TexturePackerAtlasHash = AtlasBase>; +typedef TexturePackerAtlasHash = AtlasBase>; typedef TexturePackerAtlas = AtlasBase>; typedef TexturePackerAtlasArray = AtlasBase>; diff --git a/flixel/graphics/frames/FlxAtlasFrames.hx b/flixel/graphics/frames/FlxAtlasFrames.hx index 0c890dbde4..156d0970f0 100644 --- a/flixel/graphics/frames/FlxAtlasFrames.hx +++ b/flixel/graphics/frames/FlxAtlasFrames.hx @@ -81,19 +81,16 @@ class FlxAtlasFrames extends FlxFramesCollection frames = new FlxAtlasFrames(graphic); final data:TexturePackerAtlas = description.getData(); - // JSON-Array - if (data.frames is Array) + if (data.frames.isArray()) { - final frameArray:Array = data.frames; - for (frame in frameArray) + for (frame in data.frames.toArray()) texturePackerHelper(frame.filename, frame, frames, useFrameDuration); } // JSON-Hash else { - final frameHash:Hash = data.frames; - for (name=>frame in frameHash) + for (name=>frame in data.frames.toHash()) texturePackerHelper(name, frame, frames, useFrameDuration); }