Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package zpartan.generic.loading;
  2.  
  3.  
  4. import flash.display.Loader;
  5. import flash.system.LoaderContext;
  6. import flash.system.ApplicationDomain;
  7. import flash.net.URLRequest;
  8. import flash.events.Event;
  9. import flash.events.ProgressEvent;
  10. import flash.events.IOErrorEvent;
  11.  
  12.  
  13. // hsl signal classes
  14. import hsl.haxe.Signaler;
  15. import hsl.haxe.null.NullSignaler;
  16. import hsl.haxe.direct.DirectSignaler;
  17. import StdTypes;
  18.  
  19. enum Domain
  20. {
  21.    
  22.     Same;
  23.     New;
  24.    
  25. }
  26.  
  27. class Loading
  28. {
  29.    
  30.    
  31.    
  32.    
  33.     // Private properties
  34.    
  35.     /**  The loader instance                                                                                        */
  36.     private var _loader:                    Loader;
  37.    
  38.     /**  Loader context not really used for the carousel                                                            */
  39.     private var _loaderContext:             LoaderContext;
  40.    
  41.     /**  Image or swf file location                                                                                 */
  42.     private var _location:                  String;
  43.    
  44.    
  45.     private var _loadedApplicationDomain:   ApplicationDomain;
  46.    
  47.    
  48.    
  49.    
  50.     // Public properties
  51.    
  52.     /** Store any error information here                                                                            */
  53.     private var _errorEvent:                IOErrorEvent;
  54.    
  55.     public var errorEvent( _get_errorEvent, null ): IOErrorEvent;
  56.    
  57.     private function _get_errorEvent(): IOErrorEvent
  58.     {
  59.        
  60.         return _errorEvent;
  61.        
  62.     }
  63.    
  64.     private function _set_errorEvent( e: IOErrorEvent )
  65.     {
  66.        
  67.         _errorEvent = e;
  68.         errorSignaler.dispatch();
  69.        
  70.     }
  71.    
  72.    
  73.    
  74.     /**  A reference for the loaded so that it can be stored                                                        */
  75.     private var _content:                               Dynamic;
  76.    
  77.     public var content( _get_content, _set_content ):   Dynamic;
  78.    
  79.     private function _get_content(): Dynamic { return _content; }
  80.    
  81.     private function _set_content( content_: Dynamic ): Dynamic
  82.     {
  83.        
  84.         _content = content_;
  85.         return _content;
  86.        
  87.     }
  88.    
  89.    
  90.    
  91.     /**  A reference for the loaded so that it can be stored                                                        */
  92.     private var _ref:                       Dynamic;
  93.    
  94.     public var ref( _get_ref, null ):       Dynamic;
  95.    
  96.     private function _get_ref(): Dynamic{ return _ref; }
  97.    
  98.    
  99.    
  100.     private var _progress:                  Int;
  101.    
  102.     public var progress( _get_progress, null ):  Int;
  103.    
  104.     private function _get_progress(): Int { return _progress; }
  105.    
  106.     private function _set_progress( progress_: Int ): Int
  107.     {
  108.        
  109.         _progress = Math.floor( progress_*100/ _total );
  110.         return _progress;
  111.        
  112.     }
  113.    
  114.    
  115.    
  116.     /**  Total bytes loaded  ( can be used for progress bars)                                                       */
  117.     private var _total:                     Int;
  118.    
  119.     public var total( _get_total, null ):   Int;
  120.    
  121.     private function _get_total(): Int { return _total; }
  122.    
  123.     private function _set_total( total_: Int ): Int
  124.     {
  125.        
  126.         _total = total_;
  127.         return _total;
  128.        
  129.     }
  130.    
  131.    
  132.    
  133.     private var _domain:                    Domain;
  134.    
  135.     public var domain( _get_domain, _set_domain ): Domain;
  136.    
  137.     private function _get_domain():Domain
  138.     {
  139.        
  140.         if( _domain == null )
  141.         {
  142.            
  143.             _set_domain( Domain.Same );
  144.            
  145.         }
  146.         return _domain;
  147.        
  148.     }
  149.    
  150.     private function _set_domain( domain_: Domain )
  151.     {
  152.        
  153.         switch( domain_ )
  154.         {
  155.            
  156.             case New:
  157.                 _loaderContext.applicationDomain    = new ApplicationDomain();
  158.                 _loadedApplicationDomain            = _loaderContext.applicationDomain;
  159.                 _domain = domain_;
  160.                 return domain_;
  161.                
  162.             case Same:
  163.                 _loaderContext.applicationDomain    = ApplicationDomain.currentDomain;
  164.                 _loadedApplicationDomain            = _loaderContext.applicationDomain;
  165.                 _domain = domain_;
  166.                 return domain_;
  167.                
  168.         }
  169.        
  170.     }
  171.    
  172.    
  173.    
  174.     // HSL Signals
  175.     public var progressSignaler(    default, null ):     Signaler<Void>;
  176.     public var loadedSignaler(      default, null ):     Signaler<Void>;
  177.     public var errorSignaler(       default, null ):     Signaler<Void>;
  178.    
  179.    
  180.     /**
  181.      *    @Constructor
  182.      */
  183.     public function new(
  184.                             ref_,
  185.                             location_:      String,
  186.                             ?checkPolicy:   Null<Bool> = false
  187.                         )
  188.     {
  189.        
  190.         _loader                             = new Loader();
  191.         _ref                                = ref_;
  192.         _location                           = location_;
  193.         _loaderContext                      = new LoaderContext( checkPolicy );
  194.        
  195.         progressSignaler        = new DirectSignaler( this, false );
  196.         loadedSignaler          = new DirectSignaler( this, false );
  197.         errorSignaler           = new DirectSignaler( this, false );
  198.        
  199.         _loader.contentLoaderInfo.addEventListener( Event.INIT,                 loaded                  );
  200.         _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS,     downloadProgressFirst   );
  201.         _loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR,      _set_errorEvent         );
  202.        
  203.     }
  204.    
  205.    
  206.     /**
  207.      *  Starts loading, separate to allow setting of listeners.
  208.      */
  209.     public function load()
  210.     {
  211.        
  212.         // make sure domain is set ( defaults to Same )
  213.         domain;
  214.         var url: URLRequest =  new URLRequest( _location );
  215.         _loader.load( url, _loaderContext );
  216.        
  217.     }
  218.    
  219.    
  220.     /**
  221.      *
  222.      * Lets you know when a library has loaded.
  223.      *  
  224.      *  @pram e Event that the movie has loaded
  225.      *  
  226.      *
  227.      */
  228.     private function loaded( e: Event )
  229.     {
  230.        
  231.         _loader.contentLoaderInfo.removeEventListener(     ProgressEvent.PROGRESS,     downloadProgress   );
  232.         content = e.target.content;
  233.         loadedSignaler.dispatch();
  234.        
  235.     }
  236.    
  237.    
  238.     /**
  239.      *  Used in providing progress information
  240.      *  @param e
  241.      */
  242.     private function downloadProgressFirst( e: ProgressEvent )
  243.     {
  244.        
  245.         _set_total( e.bytesTotal );
  246.        
  247.         _loader.contentLoaderInfo.removeEventListener(  ProgressEvent.PROGRESS,     downloadProgressFirst   );
  248.         downloadProgress( e );
  249.         _loader.contentLoaderInfo.addEventListener(     ProgressEvent.PROGRESS,     downloadProgress   );
  250.        
  251.     }
  252.    
  253.    
  254.     /**
  255.      *  Allows progress of download to be tracked
  256.      *  @param e Standard progress event
  257.      */
  258.     private function downloadProgress( e: ProgressEvent )
  259.     {
  260.        
  261.         _set_progress( e.bytesLoaded );
  262.         progressSignaler.dispatch();
  263.        
  264.     }
  265.    
  266.    
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement