Skip to content

Commit

Permalink
use DynamicAccess instead of Hash<T> (#3015)
Browse files Browse the repository at this point in the history
* use DynamicAccess instead of Hash<T>

* add isArray and isHash helpers

* remove import
  • Loading branch information
Geokureli authored Feb 2, 2024
1 parent 36ee8d6 commit e788b1c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
46 changes: 30 additions & 16 deletions flixel/graphics/atlas/AtlasBase.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flixel.graphics.atlas;

import haxe.DynamicAccess;

typedef AtlasBase<T> =
{
frames:T
Expand Down Expand Up @@ -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<T>(Dynamic)
abstract HashOrArray<T>(Dynamic) from DynamicAccess<T> from Array<T>
{
public inline function keyValueIterator():KeyValueIterator<String, T>
public inline function isArray()
{
return (this is Array);
}

public inline function isHash()
{
return !isArray();
}

@:to
public inline function toArray():Array<T>
{
return this;
}

@:to
public inline function toHash():DynamicAccess<T>
{
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<T>
{
if (isArray())
return toArray().iterator();
else
return toHash().iterator();
}
}

typedef HashOrArray<T> = flixel.util.typeLimit.OneOfTwo<AtlasBase.Hash<T>, Array<T>>;
2 changes: 1 addition & 1 deletion flixel/graphics/atlas/TexturePackerAtlas.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ typedef TexturePackerAtlasFrame = AtlasFrame &
?duration:Int
}

typedef TexturePackerAtlasHash = AtlasBase<AtlasBase.Hash<TexturePackerAtlasFrame>>;
typedef TexturePackerAtlasHash = AtlasBase<haxe.DynamicAccess<TexturePackerAtlasFrame>>;
typedef TexturePackerAtlas = AtlasBase<HashOrArray<TexturePackerAtlasFrame>>;
typedef TexturePackerAtlasArray = AtlasBase<Array<TexturePackerAtlasFrame>>;
9 changes: 3 additions & 6 deletions flixel/graphics/frames/FlxAtlasFrames.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TexturePackerAtlasFrame> = data.frames;
for (frame in frameArray)
for (frame in data.frames.toArray())
texturePackerHelper(frame.filename, frame, frames, useFrameDuration);
}
// JSON-Hash
else
{
final frameHash:Hash<TexturePackerAtlasFrame> = data.frames;
for (name=>frame in frameHash)
for (name=>frame in data.frames.toHash())
texturePackerHelper(name, frame, frames, useFrameDuration);
}

Expand Down

0 comments on commit e788b1c

Please sign in to comment.