diff --git a/flixel/FlxG.hx b/flixel/FlxG.hx index ccf5a18af5..296faf606a 100644 --- a/flixel/FlxG.hx +++ b/flixel/FlxG.hx @@ -11,6 +11,7 @@ import flixel.math.FlxRandom; import flixel.math.FlxRect; import flixel.system.FlxQuadTree; import flixel.system.FlxVersion; +import flixel.system.frontEnds.AssetsFrontEnd; import flixel.system.frontEnds.BitmapFrontEnd; import flixel.system.frontEnds.BitmapLogFrontEnd; import flixel.system.frontEnds.CameraFrontEnd; @@ -334,6 +335,12 @@ class FlxG */ public static var signals(default, null):SignalFrontEnd = new SignalFrontEnd(); + /** + * Contains helper functions relating to retrieving assets + * @since 5.6.0 + */ + public static var assets(default, null):AssetsFrontEnd = new AssetsFrontEnd(); + /** * Resizes the game within the window by reapplying the current scale mode. */ diff --git a/flixel/graphics/FlxGraphic.hx b/flixel/graphics/FlxGraphic.hx index b3140b93e2..5ef510df5a 100644 --- a/flixel/graphics/FlxGraphic.hx +++ b/flixel/graphics/FlxGraphic.hx @@ -40,7 +40,7 @@ class FlxGraphic implements IFlxDestroyable if (!Cache) { - bitmap = FlxAssets.getBitmapData(Source); + bitmap = FlxG.assets.getBitmapDataWarn(Source); if (bitmap == null) return null; return createGraphic(bitmap, Key, Unique, Cache); @@ -51,7 +51,7 @@ class FlxGraphic implements IFlxDestroyable if (graphic != null) return graphic; - bitmap = FlxAssets.getBitmapData(Source); + bitmap = FlxG.assets.getBitmapDataWarn(Source); if (bitmap == null) return null; @@ -546,7 +546,7 @@ class FlxGraphic implements IFlxDestroyable if (assetsClass != null) newBitmap = FlxAssets.getBitmapFromClass(assetsClass); else if (assetsKey != null) - newBitmap = FlxAssets.getBitmapData(assetsKey); + newBitmap = FlxG.assets.getBitmapDataWarn(assetsKey); if (newBitmap != null) return FlxGraphic.getBitmap(newBitmap, unique); diff --git a/flixel/graphics/frames/FlxAtlasFrames.hx b/flixel/graphics/frames/FlxAtlasFrames.hx index 0c890dbde4..b69131e569 100644 --- a/flixel/graphics/frames/FlxAtlasFrames.hx +++ b/flixel/graphics/frames/FlxAtlasFrames.hx @@ -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; /** @@ -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. @@ -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 @@ -135,7 +134,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. */ @@ -155,8 +154,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.getText(description); var pack:String = StringTools.trim(description); var lines:Array = pack.split("\n"); @@ -343,7 +342,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. */ @@ -363,8 +362,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.getText(Description); var pack = StringTools.trim(Description); var lines:Array = pack.split("\n"); diff --git a/flixel/input/mouse/FlxMouse.hx b/flixel/input/mouse/FlxMouse.hx index 53c3ae090f..e68992924d 100644 --- a/flixel/input/mouse/FlxMouse.hx +++ b/flixel/input/mouse/FlxMouse.hx @@ -267,7 +267,7 @@ class FlxMouse extends FlxPointer implements IFlxInputManager } else if ((Graphic is String)) { - cursor = new Bitmap(FlxAssets.getBitmapData(Graphic)); + cursor = new Bitmap(FlxG.assets.getBitmapDataWarn(Graphic, false)); } else { diff --git a/flixel/sound/FlxSound.hx b/flixel/sound/FlxSound.hx index 177e305b83..e46aabb7af 100644 --- a/flixel/sound/FlxSound.hx +++ b/flixel/sound/FlxSound.hx @@ -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; @@ -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. @@ -357,8 +353,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.getSound(EmbeddedSound); else FlxG.log.error('Could not find a Sound asset with an ID of \'$EmbeddedSound\'.'); } diff --git a/flixel/system/FlxAssets.hx b/flixel/system/FlxAssets.hx index b8e1fabec1..12cde355e2 100644 --- a/flixel/system/FlxAssets.hx +++ b/flixel/system/FlxAssets.hx @@ -2,9 +2,6 @@ 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; @@ -12,12 +9,14 @@ import flixel.graphics.atlas.TexturePackerAtlas; import flixel.graphics.frames.FlxAtlasFrames; import flixel.graphics.frames.FlxFrame; import flixel.graphics.frames.FlxFramesCollection; +import flixel.system.frontEnds.AssetsFrontEnd; import flixel.util.typeLimit.OneOfFour; import flixel.util.typeLimit.OneOfThree; import flixel.util.typeLimit.OneOfTwo; -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; @@ -53,7 +52,7 @@ abstract FlxXmlAsset(OneOfTwo) 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); @@ -64,12 +63,12 @@ abstract FlxXmlAsset(OneOfTwo) from Xml from String static inline function fromPath(path:String):Xml { - return fromXmlString(Assets.getText(path)); + return FlxG.assets.getXml(path); } static inline function fromXmlString(data:String):Xml { - return Xml.parse(data); + return FlxG.assets.parseXml(data); } } @@ -80,7 +79,7 @@ abstract FlxJsonAsset(OneOfTwo) 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); @@ -91,12 +90,12 @@ abstract FlxJsonAsset(OneOfTwo) from T from String static inline function fromPath(path:String):T { - return fromDataString(Assets.getText(path)); + return cast FlxG.assets.getJson(path); } static inline function fromDataString(data:String):T { - return cast Json.parse(data); + return cast FlxG.assets.parseJson(data); } } @@ -264,20 +263,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, false); - FlxG.log.error('Could not find a BitmapData asset with ID \'$id\'.'); - return null; + return FlxG.assets.getBitmapDataWarn(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):BitmapData { @@ -291,22 +292,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.getBitmapDataWarn(graphic); } return null; @@ -319,30 +320,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; } @@ -353,12 +352,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.6.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.getSound(id, useCache); } public static function getVirtualInputFrames():FlxAtlasFrames diff --git a/flixel/system/FlxSplash.hx b/flixel/system/FlxSplash.hx index 8ff49d9f10..919df4def0 100644 --- a/flixel/system/FlxSplash.hx +++ b/flixel/system/FlxSplash.hx @@ -80,7 +80,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 } diff --git a/flixel/system/frontEnds/AssetsFrontEnd.hx b/flixel/system/frontEnds/AssetsFrontEnd.hx new file mode 100644 index 0000000000..a8cb126878 --- /dev/null +++ b/flixel/system/frontEnds/AssetsFrontEnd.hx @@ -0,0 +1,398 @@ +package flixel.system.frontEnds; + +import flixel.FlxG; +import flixel.system.FlxAssets; +import flixel.system.debug.log.LogStyle; +import haxe.io.Bytes; +import haxe.Json; +import haxe.xml.Access; +import openfl.display.BitmapData; +import openfl.media.Sound; +import openfl.utils.Assets; +import openfl.utils.AssetType; +import openfl.utils.Future; +import openfl.text.Font; + +/** + * Accessed via `FlxG.log` + * + * @since 5.6.0 + */ +class AssetsFrontEnd +{ + public function new () {} + + /** + * Used by methods like `getBitmapData`, `getText` and the like to get assets synchronously. + * Can be set to a custom function to avoid the existing asset system. + * @param id The id of the asset, usually a path + * @param type The type of asset to look for, determines the type + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public dynamic function getAsset(id:String, type:FlxAssetType, useCache = true):Any + { + return switch(type) + { + case TEXT: Assets.getText(id); + case BINARY: Assets.getBytes(id); + case IMAGE: Assets.getBitmapData(id, useCache); + case SOUND: Assets.getSound(id, useCache); + case FONT: Assets.getFont(id, useCache); + } + } + + public function getAssetAndLog(id:String, type:FlxAssetType, useCache = true, ?logStyle:LogStyle):Any + { + if (exists(id, type)) + return getAsset(id, type, useCache); + + if (logStyle == null) + logStyle = LogStyle.WARNING; + FlxG.log.advanced('Could not find a $type asset with ID \'$id\'.', logStyle); + return null; + } + + /** + * Used by methods like `loadBitmapData`, `loadText` and the like to get assets asynchronously. + * Can be set to a custom function to avoid the existing asset system. + * @param id The id of the asset, usually a path + * @param type The type of asset to look for, determines the type + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public dynamic function loadAsset(id:String, type:FlxAssetType, useCache = true):Future + { + return switch(type) + { + case TEXT: Assets.loadText(id); + case BINARY: Assets.loadBytes(id); + case IMAGE: Assets.loadBitmapData(id, useCache); + case SOUND: Assets.loadSound(id, useCache); + case FONT: Assets.loadFont(id, useCache); + } + } + + /** + * Returns whether a specific asset exists. + * @param id The ID or asset path for the asset + * @param type The asset type to match, or null to match any type + * @return Whether the requested asset ID and type exists + **/ + public dynamic function exists(id:String, ?type:FlxAssetType) + { + return Assets.exists(id, type.toOpenFlType()); + } + + /** + * Returns whether an asset is "local", and therefore can be loaded synchronously + * @param id The ID or asset path for the asset + * @param type The asset type to match, or null to match any type + * @param useCache Whether to allow use of the asset cache + * @return Whether the asset is local + */ + public inline function isLocal(id:String, ?type:FlxAssetType, useCache = true) + { + return Assets.isLocal(id, type.toOpenFlType(), useCache); + } + + /** + * Returns a list of all assets (by type) + * @param type The asset type to match, or null to match any type + * @return An array of asset ID values + **/ + public inline function list(?type:FlxAssetType) + { + return Assets.list(type.toOpenFlType()); + } + + /** + * Gets an instance of a bitmap + * @param id The ID or asset path for the bitmap + * @param useCache Whether to allow use of the asset cache + * @return A new BitmapData object + **/ + public inline function getBitmapData(id:String, useCache = false):BitmapData + { + return cast getAsset(id, IMAGE, useCache); + } + + /** + * Gets an instance of a bitmap, logs when the asset is not found. + * @param id The ID or asset path for the bitmap + * @param useCache Whether to allow use of the asset cache + * @return A new BitmapData object + **/ + public inline function getBitmapDataWarn(id:String, useCache = false, ?logStyle:LogStyle):BitmapData + { + return cast getAssetAndLog(id, IMAGE, useCache, logStyle); + } + + /** + * Gets an instance of a sound + * @param id The ID or asset path for the sound + * @param useCache Whether to allow use of the asset cache + * @return A new `Sound` object Note: Dos not return a `FlxSound` + */ + public inline function getSound(id:String, useCache = true):Sound + { + return cast getAsset(id, SOUND, useCache); + } + + /** + * Gets an instance of a sound, logs when the asset is not found. + * @param id The ID or asset path for the sound + * @param useCache Whether to allow use of the asset cache + * @param logStyle Determines how the missing asset message is logged + * @return A new `Sound` object Note: Dos not return a `FlxSound` + */ + public inline function getSoundWarn(id:String, useCache = true, ?logStyle:LogStyle):Sound + { + return cast getAssetAndLog(id, SOUND, useCache, logStyle); + } + + /** + * Gets the contents of a text-based asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public inline function getText(id:String, useCache = true):String + { + return cast getAsset(id, TEXT, useCache); + } + + /** + * Gets the contents of a text-based asset, logs when the asset is not found. + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @param logStyle Determines how the missing asset message is logged + */ + public inline function getTextWarn(id:String, useCache = true, ?logStyle:LogStyle):String + { + return cast getAssetAndLog(id, TEXT, useCache, logStyle); + } + + /** + * Parses the contents of a xml-based asset into an `Xml` object + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public inline function getXml(id:String, useCache = true) + { + final text = getText(id, useCache); + return text != null ? parseXml(text) : null; + } + + /** + * Parses the contents of a xml-based asset into an `Xml` object + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @param logStyle Determines how the missing asset message is logged + */ + public inline function getXmlWarn(id:String, useCache = true, ?logStyle:LogStyle) + { + final text = getTextWarn(id, useCache, logStyle); + return text != null ? parseXml(text) : null; + } + + /** + * Gets the contents of a xml-based asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public inline function getJson(id:String, useCache = true) + { + final text = getText(id, useCache); + return text != null ? parseJson(text) : null; + } + + /** + * Gets the contents of a xml-based asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @param logStyle Determines how the missing asset message is logged + */ + public inline function getJsonWarn(id:String, useCache = true, ?logStyle:LogStyle) + { + final text = getTextWarn(id, useCache, logStyle); + return text != null ? parseJson(text) : null; + } + + /** + * Gets the contents of a binary asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + */ + public inline function getBytes(id:String, useCache = true):Bytes + { + return cast getAsset(id, BINARY, useCache); + } + + /** + * Gets the contents of a binary asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @param logStyle Determines how the missing asset message is logged + */ + public inline function getBytesWarn(id:String, useCache = true, ?logStyle:LogStyle):Bytes + { + return cast getAssetAndLog(id, BINARY, useCache); + } + + /** + * Gets the contents of a font asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache + */ + public inline function getFont(id:String, useCache = true):Font + { + return cast getAsset(id, FONT, useCache); + } + + /** + * Gets the contents of a font asset + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache + * @param logStyle Determines how the missing asset message is logged + */ + public inline function getFontWarn(id:String, useCache = true, ?logStyle:LogStyle):Font + { + return cast getAssetAndLog(id, FONT, useCache, logStyle); + } + + /** + * Loads an bitmap asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadBitmapData(id:String, useCache = false):Future + { + return cast loadAsset(id, IMAGE, useCache); + } + + /** + * Loads a sound asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadSound(id:String, useCache = true):Future + { + return cast loadAsset(id, SOUND, useCache); + } + + /** + * Loads a text asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadText(id:String, useCache = true):Future + { + return cast loadAsset(id, TEXT, useCache); + } + + /** + * Loads a text asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadXml(id:String, useCache = true):Future + { + return wrapFuture(loadText(id, useCache), parseXml); + } + + /** + * Loads a text asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadJson(id:String, useCache = true):Future + { + return wrapFuture(loadText(id, useCache), parseJson); + } + + /** + * Loads a binary asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache (if one exists) + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadBytes(id:String, useCache = true):Future + { + return cast loadAsset(id, BINARY, useCache); + } + + /** + * Loads a font asset asynchronously + * @param id The ID or asset path for the asset + * @param useCache Whether to allow use of the asset cache + * @return Returns a `Future` which allows listeners to be added via methods like `onComplete` + */ + public inline function loadFont(id:String, useCache = true):Future + { + return cast loadAsset(id, FONT, useCache); + } + + /** + * Parses a json string, creates and returns a struct + */ + public inline function parseJson(jsonText:String) + { + return Json.parse(jsonText); + } + + /** + * Parses an xml string, creates and returns an Xml object + */ + public inline function parseXml(xmlText:String) + { + return Xml.parse(xmlText); + } + + inline function wrapFuture(future:Future, converter:(T1)->T2):Future + { + final promise = new lime.app.Promise(); + + future.onComplete((data)->promise.complete(converter(data))); + future.onError((error)->promise.error(error)); + future.onProgress((progress, total)->promise.progress(progress, total)); + + return promise.future; + } +} + +/** + * The AssetType enum lists the core set of available + * asset types from the OpenFL command-line tools. + */ +enum abstract FlxAssetType(String) +{ + /** Binary assets (data that is not readable as text) */ + public var BINARY = "binary"; + + /** Font assets, such as *.ttf or *.otf files */ + public var FONT = "font"; + + /** Image assets, such as *.png or *.jpg files */ + public var IMAGE ="image"; + + /** Audio assets, such as *.ogg or *.wav files */ + public var SOUND = "sound"; + + /** Text assets */ + public var TEXT = "text"; + + public function toOpenFlType() + { + return switch((cast this:FlxAssetType)) + { + case BINARY: AssetType.BINARY; + case FONT: AssetType.FONT; + case IMAGE: AssetType.IMAGE; + case SOUND: AssetType.SOUND; + case TEXT: AssetType.TEXT; + } + } +} diff --git a/flixel/system/frontEnds/BitmapFrontEnd.hx b/flixel/system/frontEnds/BitmapFrontEnd.hx index 51fecd7459..ff854c022d 100644 --- a/flixel/system/frontEnds/BitmapFrontEnd.hx +++ b/flixel/system/frontEnds/BitmapFrontEnd.hx @@ -5,7 +5,7 @@ import flixel.graphics.FlxGraphic; import flixel.graphics.frames.FlxFrame; import flixel.math.FlxPoint; import flixel.math.FlxRect; -import flixel.system.FlxAssets.FlxGraphicAsset; +import flixel.system.FlxAssets; import flixel.util.FlxColor; import openfl.Assets; #if !flash @@ -216,7 +216,7 @@ class BitmapFrontEnd /** * Creates string key for further caching. * - * @param systemKey The first string key to use as a base for a new key. It's usually a key from openfl.Assets ("assets/image.png"). + * @param systemKey The first string key to use as a base for a new key. It's usually an asset key ("assets/image.png"). * @param userKey The second string key to use as a base for a new key. It's usually a key provided by the user * @param unique Whether generated key should be unique or not. * @return Created key. diff --git a/flixel/system/frontEnds/LogFrontEnd.hx b/flixel/system/frontEnds/LogFrontEnd.hx index 05765bb39b..99f3141f3d 100644 --- a/flixel/system/frontEnds/LogFrontEnd.hx +++ b/flixel/system/frontEnds/LogFrontEnd.hx @@ -63,7 +63,7 @@ class LogFrontEnd #if (FLX_SOUND_SYSTEM && !FLX_UNIT_TEST) if (style.errorSound != null) { - final sound = FlxAssets.getSound(style.errorSound); + final sound = FlxAssets.getSoundAddExtension(style.errorSound); if (sound != null) FlxG.sound.load(sound).play(); } diff --git a/flixel/system/frontEnds/SoundFrontEnd.hx b/flixel/system/frontEnds/SoundFrontEnd.hx index 8aed9ea036..2240b222cd 100644 --- a/flixel/system/frontEnds/SoundFrontEnd.hx +++ b/flixel/system/frontEnds/SoundFrontEnd.hx @@ -5,15 +5,11 @@ import flixel.FlxG; import flixel.group.FlxGroup; import flixel.input.keyboard.FlxKey; import flixel.math.FlxMath; -import flixel.system.FlxAssets; import flixel.sound.FlxSound; import flixel.sound.FlxSoundGroup; +import flixel.system.FlxAssets; import flixel.system.ui.FlxSoundTray; -import openfl.Assets; import openfl.media.Sound; -#if (openfl >= "8.0.0") -import openfl.utils.AssetType; -#end /** * Accessed via `FlxG.sound`. @@ -201,8 +197,8 @@ class SoundFrontEnd public inline function cache(embeddedSound:String):Sound { // load the sound into the OpenFL assets cache - if (Assets.exists(embeddedSound, AssetType.SOUND) || Assets.exists(embeddedSound, AssetType.MUSIC)) - return Assets.getSound(embeddedSound, true); + if (FlxG.assets.exists(embeddedSound, SOUND)) + return FlxG.assets.getSound(embeddedSound, true); FlxG.log.error('Could not find a Sound asset with an ID of \'$embeddedSound\'.'); return null; } @@ -213,7 +209,7 @@ class SoundFrontEnd */ public function cacheAll():Void { - for (id in Assets.list(AssetType.SOUND)) + for (id in FlxG.assets.list(SOUND)) { cache(id); } diff --git a/flixel/system/ui/FlxSoundTray.hx b/flixel/system/ui/FlxSoundTray.hx index a5492bca4d..e04212ad84 100644 --- a/flixel/system/ui/FlxSoundTray.hx +++ b/flixel/system/ui/FlxSoundTray.hx @@ -148,7 +148,7 @@ class FlxSoundTray extends Sprite { if (!silent) { - var sound = FlxAssets.getSound(up ? volumeUpSound : volumeDownSound); + var sound = FlxAssets.getSoundAddExtension(up ? volumeUpSound : volumeDownSound); if (sound != null) FlxG.sound.load(sound).play(); } diff --git a/flixel/text/FlxText.hx b/flixel/text/FlxText.hx index b9f8d2a1d4..7ea9a8934e 100644 --- a/flixel/text/FlxText.hx +++ b/flixel/text/FlxText.hx @@ -1,6 +1,5 @@ package flixel.text; -import openfl.Assets; import openfl.display.BitmapData; import openfl.geom.ColorTransform; import openfl.text.TextField; @@ -27,9 +26,6 @@ using flixel.util.FlxUnicodeUtil; #if flash import openfl.geom.Rectangle; #end -#if (openfl >= "8.0.0") -import openfl.utils.AssetType; -#end // TODO: think about filters and text @@ -659,9 +655,9 @@ class FlxText extends FlxSprite if (Font != null) { var newFontName:String = Font; - if (Assets.exists(Font, AssetType.FONT)) + if (FlxG.assets.exists(Font, FONT)) { - newFontName = Assets.getFont(Font).fontName; + newFontName = FlxG.assets.getFont(Font).fontName; } _defaultFormat.font = newFontName; diff --git a/flixel/tile/FlxBaseTilemap.hx b/flixel/tile/FlxBaseTilemap.hx index a49322da9c..3cdb66c5a3 100644 --- a/flixel/tile/FlxBaseTilemap.hx +++ b/flixel/tile/FlxBaseTilemap.hx @@ -11,7 +11,6 @@ import flixel.util.FlxColor; import flixel.util.FlxCollision; import flixel.util.FlxDirectionFlags; import flixel.util.FlxStringUtil; -import openfl.Assets; import openfl.display.BitmapData; using StringTools; @@ -278,9 +277,9 @@ class FlxBaseTilemap extends FlxObject startingIndex = 0, drawIndex = 1, collideIndex = 1) { // path to map data file? - if (Assets.exists(mapData)) + if (FlxG.assets.exists(mapData)) { - mapData = Assets.getText(mapData); + mapData = FlxG.assets.getText(mapData); } // Figure out the map dimensions based on the data string diff --git a/flixel/util/FlxStringUtil.hx b/flixel/util/FlxStringUtil.hx index 13ef02ecf7..109265a1b0 100644 --- a/flixel/util/FlxStringUtil.hx +++ b/flixel/util/FlxStringUtil.hx @@ -568,7 +568,7 @@ class FlxStringUtil if ((ImageFile is String)) { - tempBitmapData = FlxAssets.getBitmapData(ImageFile); + tempBitmapData = FlxG.assets.getBitmapDataWarn(ImageFile); } else {