Guest User

SpaceObject.as

a guest
Dec 4th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sfxworks
  2. {
  3.     import flash.display.Loader;
  4.     import flash.display.MovieClip;
  5.     import flash.events.AsyncErrorEvent;
  6.     import flash.events.Event;
  7.     import flash.events.MouseEvent;
  8.     import flash.events.ProgressEvent;
  9.     import flash.filesystem.File;
  10.     import flash.filesystem.FileMode;
  11.     import flash.filesystem.FileStream;
  12.     import flash.filesystem.StorageVolume;
  13.     import flash.geom.Matrix;
  14.     import flash.geom.Rectangle;
  15.     import flash.geom.Transform;
  16.     import flash.html.HTMLLoader;
  17.     import flash.media.Video;
  18.     import flash.net.NetConnection;
  19.     import flash.net.NetStream;
  20.     import flash.text.TextField;
  21.     import flash.text.TextFieldType;
  22.     import flash.utils.ByteArray;
  23.    
  24.     /**
  25.      * ...
  26.      * @author Samuel Jacob Walker
  27.      */
  28.     public class SpaceObject extends MovieClip
  29.     {
  30.         private var _actions:String; //Actions (scripts for object)
  31.         private var _source:String;
  32.        
  33.         private var boundss:Rectangle;
  34.         private var rot:int;
  35.         private var matrix:Matrix;
  36.        
  37.         //Video Resources
  38.         private var nc:NetConnection;
  39.         private var ns:NetStream;
  40.         private var v:Video;
  41.        
  42.         //HTMLResources
  43.         private var htmlLoader:HTMLLoader;
  44.        
  45.         //Tmp
  46.         private var ba:ByteArray;
  47.        
  48.         public function SpaceObject(source:String, actions:String, bounds:Rectangle, rotation:int=0, matrixx:Matrix = null)
  49.         {
  50.             if (source != "embeddedobject")
  51.             {
  52.                 var sourceFile:File = new File(source);
  53.                 _source = source;
  54.             }
  55.             _actions = actions;
  56.             boundss = bounds;
  57.             rot = rotation;
  58.             matrix = matrixx;
  59.             //trace("Sourefile extension = " + sourceFile.extension);
  60.             if (source == "embeddedobject")
  61.             {
  62.                 htmlLoader = new HTMLLoader();
  63.                 htmlLoader.placeLoadStringContentInApplicationSandbox = true;
  64.                 htmlLoader.loadString(_actions);
  65.                 htmlLoader.addEventListener(Event.COMPLETE, handleHtmlLoadComplete);
  66.             }
  67.             else if (sourceFile.extension.toLocaleLowerCase() == "jpg" || sourceFile.extension.toLocaleLowerCase() == "gif" || sourceFile.extension.toLocaleLowerCase() == "png" || sourceFile.extension.toLocaleLowerCase() == "jpeg" || sourceFile.extension.toLocaleLowerCase() == "bmp")
  68.             {
  69.                 //It's an image
  70.                 trace("it's an image");
  71.                 //Get source file data /[add cant find file to stream if neccesary]
  72.                 fs = new FileStream();
  73.                 ba = new ByteArray();
  74.                
  75.                 var fs:FileStream = new FileStream();
  76.                 fs.open(sourceFile, FileMode.READ);
  77.                 fs.readBytes(ba, 0, sourceFile.size);
  78.                 fs.close();
  79.                
  80.                 var l:Loader = new Loader();
  81.                 l.loadBytes(ba);
  82.                 l.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoadComplete);
  83.             }
  84.             else if (sourceFile.extension == "mp4" || sourceFile.extension == "m4v" || sourceFile.extension == "f4v" || sourceFile.extension == "3gpp" || sourceFile.extension == "flv")
  85.             {
  86.                 //It's a video
  87.                 trace("It's a video");
  88.                
  89.                 var customClient:Object = new Object();
  90.                 customClient.onMetaData = metaDataHandler;
  91.                  
  92.                 nc = new NetConnection();
  93.                 nc.connect(null);
  94.                  
  95.                 ns = new NetStream(nc);
  96.                 ns.client = customClient;
  97.                 ns.play(_source);
  98.                  
  99.                 v = new Video();
  100.                 v.attachNetStream(ns);
  101.                 addChild(v);
  102.                
  103.                 //Add args to handle pause and play
  104.                 addEventListener(MouseEvent.CLICK, handleClick);
  105.             }
  106.             else
  107.             {
  108.                
  109.             }
  110.            
  111.             //this.addEventListener(Event.REMOVED, handleRemoved);
  112.             this.addEventListener(Event.REMOVED_FROM_STAGE, handleRemoved);
  113.         }
  114.        
  115.         private function handleHtmlLoadComplete(e:Event):void
  116.         {
  117.             trace("Html content load complete");
  118.             htmlLoader.removeEventListener(Event.COMPLETE, handleHtmlLoadComplete);
  119.             htmlLoader.width = htmlLoader.contentWidth;
  120.             htmlLoader.height = htmlLoader.contentHeight;
  121.             addChild(htmlLoader);
  122.         }
  123.        
  124.         private function metaDataHandler(infoObject:Object):void
  125.         {
  126.             if (boundss.width != 0 && boundss.height != 0)
  127.             {
  128.                 v.width = boundss.width;
  129.                 v.height = boundss.height;
  130.             }
  131.             else
  132.             {
  133.                 v.width = infoObject.width;
  134.                 v.height = infoObject.height;
  135.             }
  136.            
  137.             //this.width = v.width;
  138.             //this.height = v.height;
  139.         }
  140.        
  141.         private function handleAsyncError(e:AsyncErrorEvent):void
  142.         {
  143.             trace("Async error");
  144.             trace(e.text);
  145.         }
  146.        
  147.         private function handleClick(e:MouseEvent):void
  148.         {
  149.             if (nc != null)
  150.             {
  151.                 ns.togglePause();
  152.             }
  153.         }
  154.        
  155.         private function handleRemoved(e:Event):void
  156.         {
  157.             if (nc != null) //If there's a video on stage..
  158.             {
  159.                 ns.pause();
  160.             }
  161.         }
  162.        
  163.         private function handleImageLoadComplete(e:Event):void
  164.         {
  165.             addChild(e.target.content);
  166.             ba = null;
  167.             handleActions(); //For handling actions..
  168.             setPosition(); //Set position after loading object
  169.         }
  170.        
  171.         private function setPosition():void
  172.         {
  173.             if (boundss.width != 0 && boundss.height != 0)
  174.             {
  175.                 this.width = boundss.width;
  176.                 this.height = boundss.height;
  177.             }
  178.             this.x = boundss.x;
  179.             this.y = boundss.y;
  180.             this.rotation = rot;
  181.            
  182.             trace(this.transform.matrix);
  183.             trace(matrix);
  184.            
  185.             if (matrix != null)
  186.             {
  187.                 this.transform.matrix = new Matrix(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
  188.             }
  189.         }
  190.        
  191.         private function handleActions():void
  192.         {
  193.            
  194.            
  195.         }
  196.        
  197.         public function get actions():String
  198.         {
  199.             return _actions;
  200.         }
  201.        
  202.         public function set actions(value:String):void
  203.         {
  204.             _actions = value;
  205.         }
  206.        
  207.         public function get source():String
  208.         {
  209.             return _source;
  210.         }
  211.        
  212.     }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment