Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlxG.assets: A way to overwrite and customize the way HaxeFlixel fetches or produces assets from ids #2982

Merged
merged 12 commits into from
Nov 12, 2024
7 changes: 7 additions & 0 deletions flixel/FlxG.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import flixel.math.FlxRandom;
import flixel.math.FlxRect;
import flixel.system.FlxQuadTree;
import flixel.system.FlxVersion;
import flixel.system.frontEnds.AssetFrontEnd;
import flixel.system.frontEnds.BitmapFrontEnd;
import flixel.system.frontEnds.BitmapLogFrontEnd;
import flixel.system.frontEnds.CameraFrontEnd;
Expand Down Expand Up @@ -335,6 +336,12 @@ class FlxG
*/
public static var signals(default, null):SignalFrontEnd = new SignalFrontEnd();

/**
* Contains helper functions relating to retrieving assets
* @since 5.9.0
*/
public static var assets(default, null):AssetFrontEnd = new AssetFrontEnd();

/**
* Resizes the game within the window by reapplying the current scale mode.
*/
Expand Down
6 changes: 3 additions & 3 deletions flixel/graphics/FlxGraphic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FlxGraphic implements IFlxDestroyable

if (!Cache)
{
bitmap = FlxAssets.getBitmapData(Source);
bitmap = FlxG.assets.getBitmapData(Source);
if (bitmap == null)
return null;
return createGraphic(bitmap, Key, Unique, Cache);
Expand All @@ -51,7 +51,7 @@ class FlxGraphic implements IFlxDestroyable
if (graphic != null)
return graphic;

bitmap = FlxAssets.getBitmapData(Source);
bitmap = FlxG.assets.getBitmapData(Source);
if (bitmap == null)
return null;

Expand Down Expand Up @@ -564,7 +564,7 @@ class FlxGraphic implements IFlxDestroyable
if (assetsClass != null)
newBitmap = FlxAssets.getBitmapFromClass(assetsClass);
else if (assetsKey != null)
newBitmap = FlxAssets.getBitmapData(assetsKey);
newBitmap = FlxG.assets.getBitmapData(assetsKey);

if (newBitmap != null)
return FlxGraphic.getBitmap(newBitmap, unique);
Expand Down
17 changes: 8 additions & 9 deletions flixel/graphics/frames/FlxAtlasFrames.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.system.FlxAssets;
import haxe.xml.Access;
import openfl.Assets;
import openfl.geom.Rectangle;

/**
Expand Down Expand Up @@ -40,7 +39,7 @@ class FlxAtlasFrames extends FlxFramesCollection
*
* @param source The image source (can be `FlxGraphic`, `String`, or `BitmapData`).
* @param description Contents of JSON file with atlas description.
* You can get it with `Assets.getText(path/to/description.json)`.
* You can get it with `FlxG.assets.getText(path/to/description.json)`.
* Or you can just a pass path to the JSON file in the assets directory.
* You can also directly pass in the parsed object.
* @return Newly created `FlxAtlasFrames` collection.
Expand All @@ -56,7 +55,7 @@ class FlxAtlasFrames extends FlxFramesCollection
*
* @param source The image source (can be `FlxGraphic`, `String`, or `BitmapData`).
* @param description Contents of JSON file with atlas description.
* You can get it with `Assets.getText(path/to/description.json)`.
* You can get it with `FlxG.assets.getText(path/to/description.json)`.
* Or you can just a pass path to the JSON file in the assets directory.
* You can also directly pass in the parsed object.
* @param useFrameDuration If true, any frame durations defined in the JSON will override the
Expand Down Expand Up @@ -132,7 +131,7 @@ class FlxAtlasFrames extends FlxFramesCollection
*
* @param source The image source (can be `FlxGraphic`, `String` or `BitmapData`).
* @param description Contents of the file with atlas description.
* You can get it with `Assets.getText(path/to/description/file)`.
* You can get it with `FlxG.assets.getText(path/to/description/file)`.
* Or you can just pass path to the description file in the assets directory.
* @return Newly created `FlxAtlasFrames` collection.
*/
Expand All @@ -152,8 +151,8 @@ class FlxAtlasFrames extends FlxFramesCollection

frames = new FlxAtlasFrames(graphic);

if (Assets.exists(description))
description = Assets.getText(description);
if (FlxG.assets.exists(description))
description = FlxG.assets.getTextUnsafe(description);

var pack:String = StringTools.trim(description);
var lines:Array<String> = pack.split("\n");
Expand Down Expand Up @@ -358,7 +357,7 @@ class FlxAtlasFrames extends FlxFramesCollection
*
* @param Source The image source (can be `FlxGraphic`, `String` or `BitmapData`).
* @param Description Contents of the file with atlas description.
* You can get it with `Assets.getText(path/to/description/file)`.
* You can get it with `FlxG.assets.getText(path/to/description/file)`.
* Or you can just pass a path to the description file in the assets directory.
* @return Newly created `FlxAtlasFrames` collection.
*/
Expand All @@ -378,8 +377,8 @@ class FlxAtlasFrames extends FlxFramesCollection

frames = new FlxAtlasFrames(graphic);

if (Assets.exists(Description))
Description = Assets.getText(Description);
if (FlxG.assets.exists(Description))
Description = FlxG.assets.getTextUnsafe(Description);

var pack = StringTools.trim(Description);
var lines:Array<String> = pack.split("\n");
Expand Down
2 changes: 1 addition & 1 deletion flixel/input/mouse/FlxMouse.hx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class FlxMouse extends FlxPointer implements IFlxInputManager
}
else if ((Graphic is String))
{
cursor = new Bitmap(FlxAssets.getBitmapData(Graphic));
cursor = new Bitmap(FlxG.assets.getBitmapData(Graphic, false));
}
else
{
Expand Down
8 changes: 2 additions & 6 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import flixel.math.FlxPoint;
import flixel.system.FlxAssets.FlxSoundAsset;
import flixel.tweens.FlxTween;
import flixel.util.FlxStringUtil;
import openfl.Assets;
import openfl.events.Event;
import openfl.events.IEventDispatcher;
import openfl.media.Sound;
Expand All @@ -17,9 +16,6 @@ import openfl.net.URLRequest;
#if flash11
import openfl.utils.ByteArray;
#end
#if (openfl >= "8.0.0")
import openfl.utils.AssetType;
#end

/**
* This is the universal flixel sound object, used for streaming, music, and sound effects.
Expand Down Expand Up @@ -365,8 +361,8 @@ class FlxSound extends FlxBasic
}
else if ((EmbeddedSound is String))
{
if (Assets.exists(EmbeddedSound, AssetType.SOUND) || Assets.exists(EmbeddedSound, AssetType.MUSIC))
_sound = Assets.getSound(EmbeddedSound);
if (FlxG.assets.exists(EmbeddedSound, SOUND))
_sound = FlxG.assets.getSoundUnsafe(EmbeddedSound);
else
FlxG.log.error('Could not find a Sound asset with an ID of \'$EmbeddedSound\'.');
}
Expand Down
103 changes: 59 additions & 44 deletions flixel/system/FlxAssets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package flixel.system;

import haxe.macro.Expr;
#if !macro
import openfl.display.BitmapData;
import openfl.display.Graphics;
import openfl.media.Sound;
import flixel.FlxG;
import flixel.graphics.FlxGraphic;
import flixel.graphics.atlas.AseAtlas;
import flixel.graphics.atlas.TexturePackerAtlas;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.graphics.frames.FlxFrame;
import flixel.graphics.frames.FlxFramesCollection;
import flixel.system.frontEnds.AssetFrontEnd;
import flixel.graphics.frames.bmfont.BMFont;
import flixel.util.typeLimit.OneOfFour;
import flixel.util.typeLimit.OneOfThree;
import flixel.util.typeLimit.OneOfTwo;
import haxe.io.Bytes;
import haxe.Json;
import haxe.xml.Access;
import openfl.Assets;
import openfl.display.BitmapData;
import openfl.display.Graphics;
import openfl.media.Sound;
import openfl.utils.ByteArray;

using StringTools;
Expand Down Expand Up @@ -66,7 +66,7 @@ abstract FlxXmlAsset(OneOfTwo<Xml, String>) from Xml from String
if ((this is String))
{
final str:String = cast this;
if (Assets.exists(str))
if (FlxG.assets.exists(str))
return fromPath(str);

return fromXmlString(str);
Expand All @@ -77,12 +77,12 @@ abstract FlxXmlAsset(OneOfTwo<Xml, String>) from Xml from String

static inline function fromPath<T>(path:String):Xml
{
return fromXmlString(Assets.getText(path));
return FlxG.assets.getXmlUnsafe(path);
}

static inline function fromXmlString<T>(data:String):Xml
{
return Xml.parse(data);
return FlxG.assets.parseXml(data);
}
}

Expand All @@ -93,7 +93,7 @@ abstract FlxJsonAsset<T>(OneOfTwo<T, String>) from T from String
if ((this is String))
{
final str:String = cast this;
if (Assets.exists(str))
if (FlxG.assets.exists(str))
return fromPath(str);

return fromDataString(str);
Expand All @@ -104,12 +104,12 @@ abstract FlxJsonAsset<T>(OneOfTwo<T, String>) from T from String

static inline function fromPath<T>(path:String):T
{
return fromDataString(Assets.getText(path));
return cast FlxG.assets.getJsonUnsafe(path);
}

static inline function fromDataString<T>(data:String):T
{
return cast Json.parse(data);
return cast FlxG.assets.parseJson(data);
}
}

Expand Down Expand Up @@ -272,20 +272,22 @@ class FlxAssets
graph.lineTo(100, 100);
graph.endFill();
}


/**
* Gets an instance of a bitmap, logs when the asset is not found.
* @param id The ID or asset path for the bitmap
* @return A new BitmapData object
**/
public static inline function getBitmapData(id:String):BitmapData
{
if (Assets.exists(id))
return Assets.getBitmapData(id, true);
FlxG.log.error('Could not find a BitmapData asset with ID \'$id\'.');
return null;
return FlxG.assets.getBitmapData(id);
}

/**
* Generates BitmapData from specified class. Less typing.
*
* @param source BitmapData class to generate BitmapData object from.
* @return Newly instantiated BitmapData object.
* @param source BitmapData class to generate BitmapData object from.
* @return Newly instantiated BitmapData object.
*/
public static inline function getBitmapFromClass(source:Class<Dynamic>):BitmapData
{
Expand All @@ -299,22 +301,22 @@ class FlxAssets
* 3) if the input is String, then it will get BitmapData from openfl.Assets;
* 4) it will return null in any other case.
*
* @param Graphic input data to get BitmapData object for.
* @return BitmapData for specified Dynamic object.
* @param graphic input data to get BitmapData object for.
* @return BitmapData for specified Dynamic object.
*/
public static function resolveBitmapData(Graphic:FlxGraphicSource):BitmapData
public static function resolveBitmapData(graphic:FlxGraphicSource):BitmapData
{
if ((Graphic is BitmapData))
if ((graphic is BitmapData))
{
return cast Graphic;
return cast graphic;
}
else if ((Graphic is Class))
else if ((graphic is Class))
{
return FlxAssets.getBitmapFromClass(cast Graphic);
return getBitmapFromClass(cast graphic);
}
else if ((Graphic is String))
else if ((graphic is String))
{
return FlxAssets.getBitmapData(Graphic);
return FlxG.assets.getBitmapData(graphic);
}

return null;
Expand All @@ -327,30 +329,28 @@ class FlxAssets
* 3) if the input is String, then it will return it;
* 4) it will return null in any other case.
*
* @param Graphic input data to get string key for.
* @param Key optional key string.
* @return Key String for specified Graphic object.
* @param graphic input data to get string key for.
* @param key optional key string.
* @return Key String for specified Graphic object.
*/
public static function resolveKey(Graphic:FlxGraphicSource, ?Key:String):String
public static function resolveKey(graphic:FlxGraphicSource, ?key:String):String
{
if (Key != null)
{
return Key;
}

if ((Graphic is BitmapData))
if (key != null)
return key;

if ((graphic is BitmapData))
{
return Key;
return key;
}
else if ((Graphic is Class))
else if ((graphic is Class))
{
return FlxG.bitmap.getKeyForClass(cast Graphic);
return FlxG.bitmap.getKeyForClass(cast graphic);
}
else if ((Graphic is String))
else if ((graphic is String))
{
return Graphic;
return graphic;
}

return null;
}

Expand All @@ -361,12 +361,27 @@ class FlxAssets
* @param id The asset id of the local sound file.
* @return The sound file.
*/
public static function getSound(id:String):Sound
@:deprecated("FlxAssets.getSound is deprecated, use getSoundAddExtension, instead")
public static inline function getSound(id:String):Sound
{
return getSoundAddExtension(id);
}

/**
* Loads an OpenFL sound asset from the given asset id. If an extension not provided the
* `defaultSoundExtension` is used (defaults to "ogg" on non-flash targets).
*
* @param id The asset id of the local sound file.
* @return The sound file.
*
* @since 5.9.0
*/
public static function getSoundAddExtension(id:String, useCache = true):Sound
{
if (!id.endsWith(".mp3") && !id.endsWith(".ogg") && !id.endsWith(".wav"))
id += "." + defaultSoundExtension;

return Assets.getSound(id);
return FlxG.assets.getSoundUnsafe(id, useCache);
}

public static function getVirtualInputFrames():FlxAtlasFrames
Expand Down
2 changes: 1 addition & 1 deletion flixel/system/FlxSplash.hx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class FlxSplash extends FlxState
#if FLX_SOUND_SYSTEM
if (!muted)
{
FlxG.sound.load(FlxAssets.getSound("flixel/sounds/flixel")).play();
FlxG.sound.load(FlxAssets.getSoundAddExtension("flixel/sounds/flixel")).play();
}
#end
}
Expand Down
Loading
Loading