Skip to content

Instantly share code, notes, and snippets.

@starburst997
Created February 9, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starburst997/166373ceb9daccbcbff7 to your computer and use it in GitHub Desktop.
Save starburst997/166373ceb9daccbcbff7 to your computer and use it in GitHub Desktop.
package fs.loaders;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Loader;
import openfl.display.LoaderInfo;
import openfl.events.ErrorEvent;
import openfl.events.Event;
import openfl.events.IOErrorEvent;
import openfl.events.SecurityErrorEvent;
import openfl.net.URLRequest;
/**
* Load a BitmapData from a URL
*/
class BitmapLoader extends Loader
{
/* Static */
// If there is currently file loading
private static var fileLoading:Bool = false;
// When files are all loaded
private static var fileHandler:Void->Void = null;
// Files counter
private static var fileCounter:Int = 0;
// Makes sure we don't get garbage collected
private static var loaders:Array<BitmapLoader> = [];
// Stop all loading
public static function stopAll():Void
{
for ( loader in loaders )
{
loader.handler = null;
}
}
// Is loading file?
public static function isLoading():Bool
{
return fileCounter > 0;
}
// Load an external binary file
public static function loadFile( url:String, handler:BitmapData->Void, failSilently:Bool = true ):BitmapLoader
{
fileCounter++;
var loader = new BitmapLoader( handler, failSilently );
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, completeHandler );
loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, errorHandler1 );
loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, errorHandler2 );
loader.contentLoaderInfo.addEventListener( ErrorEvent.ERROR, errorHandler3 );
//loader.contentLoaderInfo.addEventListener( AsyncErrorEvent.ASYNC_ERROR, errorHandler4 );
loaders.push( loader );
trace("loading file: " + url);
try
{
loader.load( new URLRequest( url ) );
}
catch ( e:Dynamic )
{
loaders.remove( loader );
trace("ERROR!: " + e);
return null;
}
return loader;
}
private static function errorHandler1( event:SecurityErrorEvent ):Void
{
trace("error1: " + event.text);
checkFileHandler( cast( event.target, BitmapLoader ) );
}
private static function errorHandler2( event:IOErrorEvent ):Void
{
trace("error2: " + event.text);
checkFileHandler( cast( event.target, BitmapLoader ) );
}
private static function errorHandler3( event:ErrorEvent ):Void
{
trace("error3: " + event.text);
checkFileHandler( cast( event.target, BitmapLoader ) );
}
/*public static function errorHandler4( event:AsyncErrorEvent ):Void
{
trace("error4: " + event.text);
checkFileHandler( cast( event.target, BitmapLoader ) );
}*/
private static function completeHandler( event:Event ):Void
{
var loader = cast( cast( event.target, LoaderInfo ).loader, BitmapLoader );
trace("Complete file");
if ( loader.handler != null )
{
var bitmap = cast( loader.content, Bitmap );
loader.handler( bitmap.bitmapData );
loader.handler = null;
}
checkFileHandler( loader, true );
}
private static function checkFileHandler( loader:BitmapLoader = null, success:Bool = false ):Void
{
if ( loader != null )
{
// Clean listeners
loader.clearListeners();
loaders.remove( loader );
// Remove counter
fileCounter--;
// Mark as used
loader.completed = true;
// Fail?
if ( !success && !loader.failSilently && (loader.handler != null) )
{
loader.handler( null );
}
loader.handler = null;
}
if ( fileCounter <= 0 )
{
fileCounter = 0;
if ( fileHandler != null )
{
var handler = fileHandler;
fileHandler = null;
handler();
}
}
}
public static function setFileHandler( handler:Void->Void ):Void
{
fileHandler = handler;
checkFileHandler();
}
/* Class */
// Handler when loader is complete
public var handler:BitmapData->Void = null;
// Call handler when fail
public var failSilently:Bool = true;
// No longer usefull
public var completed:Bool = false;
// Simple loader
public function new( handler:BitmapData->Void = null, failSilently:Bool = true )
{
super();
this.failSilently = failSilently;
this.handler = handler;
}
// Clear listeners
public function clearListeners():Void
{
contentLoaderInfo.removeEventListener( Event.COMPLETE, BitmapLoader.completeHandler );
contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, BitmapLoader.errorHandler1 );
contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, BitmapLoader.errorHandler2 );
contentLoaderInfo.removeEventListener( ErrorEvent.ERROR, BitmapLoader.errorHandler3 );
//contentLoaderInfo.removeEventListener( AsyncErrorEvent.ASYNC_ERROR, BitmapLoader.errorHandler4 );
}
// Clear
public function clear():Void
{
clearListeners();
if ( !completed )
{
BitmapLoader.fileCounter--;
loaders.remove( this );
}
handler = null;
completed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment