diff --git a/flixel/FlxG.hx b/flixel/FlxG.hx index 99559cddc7..10f7d72e86 100644 --- a/flixel/FlxG.hx +++ b/flixel/FlxG.hx @@ -27,6 +27,7 @@ import openfl.Lib; import openfl.display.DisplayObject; import openfl.display.Stage; import openfl.display.StageDisplayState; +import openfl.events.Event; import openfl.net.URLRequest; #if FLX_TOUCH import flixel.input.touch.FlxTouchManager; @@ -529,6 +530,7 @@ class FlxG * Called by `FlxGame` to set up `FlxG` during `FlxGame`'s constructor. */ @:allow(flixel.FlxGame.new) + @:haxe.warning("-WDeprecated") static function init(game:FlxGame, width:Int, height:Int):Void { if (width < 0) @@ -586,6 +588,8 @@ class FlxG #if FLX_SOUND_SYSTEM sound = new SoundFrontEnd(); #end + + assets.onChange.add(()->bitmap.onAssetsReload(new Event(Event.CHANGE))); } static function initRenderMethod():Void diff --git a/flixel/FlxGame.hx b/flixel/FlxGame.hx index 6f9eb08b3b..e8de1b1c8e 100644 --- a/flixel/FlxGame.hx +++ b/flixel/FlxGame.hx @@ -5,7 +5,6 @@ import flixel.system.FlxSplash; import flixel.util.FlxArrayUtil; import flixel.util.FlxDestroyUtil; import flixel.util.typeLimit.NextState; -import openfl.Assets; import openfl.Lib; import openfl.display.Sprite; import openfl.display.StageAlign; @@ -351,8 +350,6 @@ class FlxGame extends Sprite // make sure the cursor etc are properly scaled from the start resizeGame(FlxG.stage.stageWidth, FlxG.stage.stageHeight); - - Assets.addEventListener(Event.CHANGE, FlxG.bitmap.onAssetsReload); } function onFocus(_):Void diff --git a/flixel/graphics/FlxGraphic.hx b/flixel/graphics/FlxGraphic.hx index 656a0ace93..163afcb691 100644 --- a/flixel/graphics/FlxGraphic.hx +++ b/flixel/graphics/FlxGraphic.hx @@ -25,9 +25,9 @@ class FlxGraphic implements IFlxDestroyable public static var defaultPersist:Bool = false; /** - * Creates and caches FlxGraphic object from openfl.Assets key string. + * Creates and caches FlxGraphic object from asset key string. * - * @param Source `openfl.Assets` key string. For example: `"assets/image.png"`. + * @param Source Asset key string. For example: `"assets/image.png"`. * @param Unique Ensures that the `BitmapData` uses a new slot in the cache. * If `true`, then `BitmapData` for this `FlxGraphic` will be cloned, which means extra memory. * @param Key Force the cache to use a specific key to index the bitmap. diff --git a/flixel/graphics/frames/FlxBitmapFont.hx b/flixel/graphics/frames/FlxBitmapFont.hx index 6ba8b53785..a166b171db 100644 --- a/flixel/graphics/frames/FlxBitmapFont.hx +++ b/flixel/graphics/frames/FlxBitmapFont.hx @@ -9,7 +9,6 @@ import flixel.math.FlxRect; import flixel.system.FlxAssets; import flixel.util.FlxColor; import haxe.xml.Access; -import openfl.Assets; import openfl.display.BitmapData; import openfl.geom.Point; import openfl.geom.Rectangle; diff --git a/flixel/system/FlxAssets.hx b/flixel/system/FlxAssets.hx index 70d1486415..5191d4f9e3 100644 --- a/flixel/system/FlxAssets.hx +++ b/flixel/system/FlxAssets.hx @@ -318,7 +318,7 @@ class FlxAssets * Takes Dynamic object as a input and tries to convert it to BitmapData: * 1) if the input is BitmapData, then it will return this BitmapData; * 2) if the input is Class, then it will create BitmapData from this class; - * 3) if the input is String, then it will get BitmapData from openfl.Assets; + * 3) if the input is String, then it will get BitmapData from FlxG.assets; * 4) it will return null in any other case. * * @param graphic input data to get BitmapData object for. diff --git a/flixel/system/frontEnds/AssetFrontEnd.hx b/flixel/system/frontEnds/AssetFrontEnd.hx index bb44118cbf..3cdf013540 100644 --- a/flixel/system/frontEnds/AssetFrontEnd.hx +++ b/flixel/system/frontEnds/AssetFrontEnd.hx @@ -3,6 +3,7 @@ package flixel.system.frontEnds; import flixel.FlxG; import flixel.system.FlxAssets; import flixel.system.debug.log.LogStyle; +import flixel.util.FlxSignal; import haxe.Json; import haxe.io.Bytes; import haxe.io.Path; @@ -45,6 +46,11 @@ using StringTools; */ class AssetFrontEnd { + /** + * Dispatched whenever the availability or the contents of assets change + */ + public final onChange = new FlxSignal(); + #if FLX_CUSTOM_ASSETS_DIRECTORY /** * The target directory @@ -63,6 +69,7 @@ class AssetFrontEnd // Verify valid directory if (sys.FileSystem.exists(directory) == false) throw 'Error finding custom asset directory:"$directory" from given path: $rawPath'; + // remove final "/assets" since the id typically contains it final split = directory.split("/"); split.pop(); @@ -82,7 +89,10 @@ class AssetFrontEnd return id.startsWith("flixel/") || id.contains(':'); } #else - public function new () {} + public function new () + { + lime.utils.Assets.onChange.add(()->onChange.dispatch()); + } #end #if (FLX_DEFAULT_SOUND_EXT == "1" || FLX_NO_DEFAULT_SOUND_EXT) @@ -246,6 +256,7 @@ class AssetFrontEnd #else if (useOpenflAssets(id)) return Assets.exists(id, type.toOpenFlType()); + // Can't verify contents match expected type without return sys.FileSystem.exists(getPath(id)); #end diff --git a/flixel/system/frontEnds/BitmapFrontEnd.hx b/flixel/system/frontEnds/BitmapFrontEnd.hx index 8e17339206..75c2ec6844 100644 --- a/flixel/system/frontEnds/BitmapFrontEnd.hx +++ b/flixel/system/frontEnds/BitmapFrontEnd.hx @@ -6,8 +6,8 @@ import flixel.math.FlxPoint; import flixel.math.FlxRect; import flixel.system.FlxAssets; import flixel.util.FlxColor; -import openfl.Assets; import openfl.display.BitmapData; +import openfl.utils.Assets; #if FLX_OPENGL_AVAILABLE import lime.graphics.opengl.GL; #end @@ -17,6 +17,7 @@ import lime.graphics.opengl.GL; * * Accessed via `FlxG.bitmap`. */ +@:autoBuild(flixel.system.macros.FlxMacroUtil.deprecateOverride("onAssetsReload", "onAssetsReload is deprecated, use onAssetsChange, instead")) class BitmapFrontEnd { #if FLX_OPENGL_AVAILABLE @@ -48,15 +49,18 @@ class BitmapFrontEnd reset(); } + @:deprecated("onAssetsReload is deprecated, use onAssetsChange, instead") public function onAssetsReload(_):Void { - for (key in _cache.keys()) + onAssetsChange(); + } + + public function onAssetsChange():Void + { + for (key => obj in _cache) { - var obj = _cache.get(key); if (obj != null && obj.canBeRefreshed) - { obj.onAssetsReload(); - } } } @@ -126,7 +130,7 @@ class BitmapFrontEnd * @param key The FlxGraphics key (or name). * @return The FlxGraphic with the specified key, or null if the object doesn't exist. */ - public inline function get(key:String):FlxGraphic + public inline function get(key:String):Null { return _cache.get(key); } @@ -137,11 +141,10 @@ class BitmapFrontEnd * @param bmd BitmapData to find in the cache. * @return The BitmapData's key or null if there isn't such BitmapData in cache. */ - public function findKeyForBitmap(bmd:BitmapData):String + public function findKeyForBitmap(bmd:BitmapData):Null { - for (key in _cache.keys()) + for (key => obj in _cache) { - var obj = _cache.get(key); if (obj != null && obj.bitmap == bmd) return key; } @@ -167,7 +170,7 @@ class BitmapFrontEnd * @param unique Whether generated key should be unique or not. * @return Created key. */ - public function generateKey(systemKey:String, userKey:String, unique = false):String + public function generateKey(systemKey:Null, userKey:Null, unique = false):String { var key:String = userKey; if (key == null) @@ -187,9 +190,11 @@ class BitmapFrontEnd */ public function getUniqueKey(?baseKey:String):String { - if (baseKey == null) - baseKey = "pixels"; - + return getUniqueKeyHelper(baseKey != null ? baseKey : "pixels"); + } + + function getUniqueKeyHelper(baseKey = "pixels"):String + { if (!checkCache(baseKey)) return baseKey; @@ -283,10 +288,9 @@ class BitmapFrontEnd _cache = new Map(); return; } - - for (key in _cache.keys()) + + for (key => obj in _cache) { - var obj = get(key); if (obj != null && !obj.persist && obj.useCount <= 0) { removeKey(key); @@ -314,10 +318,9 @@ class BitmapFrontEnd _cache = new Map(); return; } - - for (key in _cache.keys()) + + for (key => obj in _cache) { - var obj = get(key); removeKey(key); if (obj != null) @@ -331,9 +334,8 @@ class BitmapFrontEnd */ public function clearUnused():Void { - for (key in _cache.keys()) + for (key => obj in _cache) { - var obj = _cache.get(key); if (obj != null && obj.useCount <= 0 && !obj.persist && obj.destroyOnNoUse) { removeByKey(key); @@ -359,7 +361,7 @@ class BitmapFrontEnd if (_whitePixel == null) { var bd = new BitmapData(10, 10, true, FlxColor.WHITE); - var graphic:FlxGraphic = FlxG.bitmap.add(bd, true, "whitePixels"); + var graphic:FlxGraphic = add(bd, true, "whitePixels"); graphic.persist = true; _whitePixel = graphic.imageFrame.frame; } diff --git a/flixel/text/FlxText.hx b/flixel/text/FlxText.hx index b3dad3d00f..f8a0d47f3b 100644 --- a/flixel/text/FlxText.hx +++ b/flixel/text/FlxText.hx @@ -13,7 +13,6 @@ import flixel.system.FlxAssets; import flixel.util.FlxColor; import flixel.util.FlxDestroyUtil; import flixel.util.helpers.FlxRange; -import openfl.Assets; import openfl.display.BitmapData; import openfl.geom.ColorTransform; import openfl.text.TextField;