Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zpartan.generic.loading;
- import flash.display.Loader;
- import flash.system.LoaderContext;
- import flash.system.ApplicationDomain;
- import flash.net.URLRequest;
- import flash.events.Event;
- import flash.events.ProgressEvent;
- import flash.events.IOErrorEvent;
- // hsl signal classes
- import hsl.haxe.Signaler;
- import hsl.haxe.null.NullSignaler;
- import hsl.haxe.direct.DirectSignaler;
- import StdTypes;
- enum Domain
- {
- Same;
- New;
- }
- class Loading
- {
- // Private properties
- /** The loader instance */
- private var _loader: Loader;
- /** Loader context not really used for the carousel */
- private var _loaderContext: LoaderContext;
- /** Image or swf file location */
- private var _location: String;
- private var _loadedApplicationDomain: ApplicationDomain;
- // Public properties
- /** Store any error information here */
- private var _errorEvent: IOErrorEvent;
- public var errorEvent( _get_errorEvent, null ): IOErrorEvent;
- private function _get_errorEvent(): IOErrorEvent
- {
- return _errorEvent;
- }
- private function _set_errorEvent( e: IOErrorEvent )
- {
- _errorEvent = e;
- errorSignaler.dispatch();
- }
- /** A reference for the loaded so that it can be stored */
- private var _content: Dynamic;
- public var content( _get_content, _set_content ): Dynamic;
- private function _get_content(): Dynamic { return _content; }
- private function _set_content( content_: Dynamic ): Dynamic
- {
- _content = content_;
- return _content;
- }
- /** A reference for the loaded so that it can be stored */
- private var _ref: Dynamic;
- public var ref( _get_ref, null ): Dynamic;
- private function _get_ref(): Dynamic{ return _ref; }
- private var _progress: Int;
- public var progress( _get_progress, null ): Int;
- private function _get_progress(): Int { return _progress; }
- private function _set_progress( progress_: Int ): Int
- {
- _progress = Math.floor( progress_*100/ _total );
- return _progress;
- }
- /** Total bytes loaded ( can be used for progress bars) */
- private var _total: Int;
- public var total( _get_total, null ): Int;
- private function _get_total(): Int { return _total; }
- private function _set_total( total_: Int ): Int
- {
- _total = total_;
- return _total;
- }
- private var _domain: Domain;
- public var domain( _get_domain, _set_domain ): Domain;
- private function _get_domain():Domain
- {
- if( _domain == null )
- {
- _set_domain( Domain.Same );
- }
- return _domain;
- }
- private function _set_domain( domain_: Domain )
- {
- switch( domain_ )
- {
- case New:
- _loaderContext.applicationDomain = new ApplicationDomain();
- _loadedApplicationDomain = _loaderContext.applicationDomain;
- _domain = domain_;
- return domain_;
- case Same:
- _loaderContext.applicationDomain = ApplicationDomain.currentDomain;
- _loadedApplicationDomain = _loaderContext.applicationDomain;
- _domain = domain_;
- return domain_;
- }
- }
- // HSL Signals
- public var progressSignaler( default, null ): Signaler<Void>;
- public var loadedSignaler( default, null ): Signaler<Void>;
- public var errorSignaler( default, null ): Signaler<Void>;
- /**
- * @Constructor
- */
- public function new(
- ref_,
- location_: String,
- ?checkPolicy: Null<Bool> = false
- )
- {
- _loader = new Loader();
- _ref = ref_;
- _location = location_;
- _loaderContext = new LoaderContext( checkPolicy );
- progressSignaler = new DirectSignaler( this, false );
- loadedSignaler = new DirectSignaler( this, false );
- errorSignaler = new DirectSignaler( this, false );
- _loader.contentLoaderInfo.addEventListener( Event.INIT, loaded );
- _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, downloadProgressFirst );
- _loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _set_errorEvent );
- }
- /**
- * Starts loading, separate to allow setting of listeners.
- */
- public function load()
- {
- // make sure domain is set ( defaults to Same )
- domain;
- var url: URLRequest = new URLRequest( _location );
- _loader.load( url, _loaderContext );
- }
- /**
- *
- * Lets you know when a library has loaded.
- *
- * @pram e Event that the movie has loaded
- *
- *
- */
- private function loaded( e: Event )
- {
- _loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, downloadProgress );
- content = e.target.content;
- loadedSignaler.dispatch();
- }
- /**
- * Used in providing progress information
- * @param e
- */
- private function downloadProgressFirst( e: ProgressEvent )
- {
- _set_total( e.bytesTotal );
- _loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, downloadProgressFirst );
- downloadProgress( e );
- _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, downloadProgress );
- }
- /**
- * Allows progress of download to be tracked
- * @param e Standard progress event
- */
- private function downloadProgress( e: ProgressEvent )
- {
- _set_progress( e.bytesLoaded );
- progressSignaler.dispatch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement