Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package display;
  2. import com.blazingmammothgames.util.ThreadPool;
  3. import event.CustomEvent;
  4. import gif.AnimatedGif;
  5. import haxe.io.Bytes;
  6. import openfl.display.Sprite;
  7. import openfl.errors.Error;
  8. import openfl.events.Event;
  9. import openfl.events.IOErrorEvent;
  10. import openfl.events.MouseEvent;
  11. import openfl.events.ProgressEvent;
  12. import openfl.net.URLLoader;
  13. import openfl.net.URLRequest;
  14. import openfl.net.URLLoaderDataFormat;
  15. import thread.ThreadsTools;
  16. import utils.Utils;
  17.  
  18. class GifImage extends Sprite
  19. {
  20.     public static inline var COMPLETE:String = "load_complete";
  21.    
  22.     //Loader object
  23.     private var m_strUrl:String;
  24.     private var m_urlLoader:URLLoader;
  25.    
  26.     //Loader Progress object
  27.     private var m_spBckBarre:Sprite;
  28.     private var m_spBarre:Sprite;
  29.    
  30.     //Gif object
  31.     private var m_bytes:Bytes;
  32.     private var m_gif:AnimatedGif;
  33.    
  34.     //Thread
  35.     private var m_thread:ThreadPool;
  36.    
  37.     public function new(_url:String)
  38.     {
  39.         super();
  40.        
  41.         this.graphics.lineStyle(2.5, 0x000000);
  42.         //this.graphics.beginFill(0xE3E3E3);
  43.         this.graphics.drawRect(0, 0, Utils.stageWidth / 2  ,Utils.stageWidth / 2 );
  44.         this.graphics.endFill();
  45.        
  46.         m_strUrl = _url;
  47.        
  48.         m_urlLoader = new URLLoader();
  49.         m_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
  50.         m_urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
  51.         m_urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgressLoader);
  52.         m_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoErrorEvent);
  53.         m_urlLoader.load(new URLRequest(_url));
  54.     }
  55.     //__________________________________________________________________________________________________________
  56.     //--------------------------------------------------
  57.     // ENGINE FUNCTIONS
  58.     //--------------------------------------------------
  59.     private function onPlayGif(ev:MouseEvent):Void
  60.     {
  61.         m_gif.play();
  62.     }
  63.     private function onStopGif(ev:MouseEvent):Void
  64.     {
  65.         m_gif.stop();
  66.     }
  67.     //__________________________________________________________________________________________________________
  68.     //--------------------------------------------------
  69.     // HANDLER EVENT FUNCTIONS
  70.     //--------------------------------------------------
  71.     private function onLoadComplete(ev:Event):Void
  72.     {
  73.         m_urlLoader.removeEventListener(Event.COMPLETE, onLoadComplete);
  74.         m_urlLoader.removeEventListener(ProgressEvent.PROGRESS, onProgressLoader);
  75.         m_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIoErrorEvent);
  76.        
  77.         removeChild(m_spBarre);
  78.         removeChild(m_spBckBarre);
  79.        
  80.         m_bytes = Bytes.ofString(m_urlLoader.data);
  81.         m_gif = new AnimatedGif(m_bytes);
  82.         //m_gif.play();
  83.         addChild(m_gif);
  84.        
  85.         this.addEventListener(MouseEvent.MOUSE_OUT, onStopGif);
  86.         this.addEventListener(MouseEvent.MOUSE_OVER, onPlayGif);
  87.     }
  88.    
  89.     private function onProgressLoader(ev:ProgressEvent):Void
  90.     {
  91.         if (m_spBckBarre == null)
  92.         {
  93.             m_spBckBarre = new Sprite();
  94.             m_spBckBarre.graphics.beginFill(0x303030);
  95.             m_spBckBarre.graphics.drawRect(0, 0, this.width * 0.5, this.height * 0.10);
  96.             m_spBckBarre.x = (this.width - m_spBckBarre.width) / 2;
  97.             m_spBckBarre.y = (this.height - m_spBckBarre.height) / 2;
  98.             addChild(m_spBckBarre);
  99.            
  100.             m_spBarre = new Sprite();
  101.             m_spBarre.graphics.beginFill(0xf10013);
  102.             m_spBarre.graphics.drawRect(0, 0, m_spBckBarre.width * 0.85, m_spBckBarre.height * 0.50);
  103.             m_spBarre.x = (m_spBckBarre.width - m_spBarre.width) / 2;
  104.             m_spBarre.y = (m_spBckBarre.height - m_spBarre.height) / 2;
  105.             m_spBckBarre.addChild(m_spBarre);
  106.         }
  107.        
  108.         var percent:Float = ev.bytesLoaded / ev.bytesTotal;
  109.        
  110.         m_spBarre.graphics.clear();
  111.         m_spBarre.graphics.beginFill(0xf10013);
  112.         m_spBarre.graphics.drawRect(0, 0, (m_spBckBarre.width * 0.85) * percent, m_spBckBarre.height * 0.50);
  113.         //trace(ev.bytesLoaded, ev.bytesTotal);
  114.     }
  115.     private function onIoErrorEvent(ev:IOErrorEvent):Void
  116.     {
  117.         trace(ev.toString());
  118.     }
  119.     //__________________________________________________________________________________________________________
  120.     //--------------------------------------------------
  121.     // DISPOSE FUNCTION
  122.     //--------------------------------------------------
  123.     public function dispose():Void
  124.     {
  125.         m_urlLoader.removeEventListener(Event.COMPLETE, onLoadComplete);
  126.         m_urlLoader.removeEventListener(ProgressEvent.PROGRESS, onProgressLoader);
  127.         m_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIoErrorEvent);
  128.         m_urlLoader = null;
  129.        
  130.         removeChild(m_gif);
  131.         m_gif.stop();
  132.         m_bytes = null;
  133.         m_gif = null;
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement