Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flixel/FlxG.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions flixel/FlxGame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions flixel/graphics/FlxGraphic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion flixel/graphics/frames/FlxBitmapFont.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion flixel/system/FlxAssets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitmapData>, 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.
Expand Down
13 changes: 12 additions & 1 deletion flixel/system/frontEnds/AssetFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
46 changes: 24 additions & 22 deletions flixel/system/frontEnds/BitmapFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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<FlxGraphic>
{
return _cache.get(key);
}
Expand All @@ -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<String>
{
for (key in _cache.keys())
for (key => obj in _cache)
{
var obj = _cache.get(key);
if (obj != null && obj.bitmap == bmd)
return key;
}
Expand All @@ -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<String>, userKey:Null<String>, unique = false):String
{
var key:String = userKey;
if (key == null)
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion flixel/text/FlxText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading