PageRenderTime 214ms CodeModel.GetById 131ms app.highlight 8ms RepoModel.GetById 72ms app.codeStats 1ms

/src/away3d/materials/BitmapFileMaterial.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 115 lines | 71 code | 18 blank | 26 comment | 10 complexity | 7f69f6fcbc83ec70dfd9c08f4cf93522 MD5 | raw file
  1package away3d.materials
  2{
  3	import away3d.arcane;
  4	import away3d.events.LoaderEvent;
  5
  6	import flash.display.Bitmap;
  7	import flash.display.BitmapData;
  8	import flash.display.Loader;
  9	import flash.display.Sprite;
 10	import flash.events.Event;
 11	import flash.events.IOErrorEvent;
 12	import flash.events.ProgressEvent;
 13	import flash.net.*;
 14	import flash.system.LoaderContext;
 15
 16	use namespace arcane;
 17	
 18	/**
 19	 * Dispatched when the material completes a file load successfully.
 20	 * 
 21	 * @eventType away3d.events.ResourceEvent
 22	 */
 23	[Event(name="resourceComplete",type="away3d.events.LoaderEvent")]
 24	
 25	/**
 26	 * Dispatched when the material fails to load a file.
 27	 * 
 28	 * @eventType away3d.events.LoaderEvent
 29	 */
 30	[Event(name="loadError",type="away3d.events.LoaderEvent")]
 31	
 32	/**
 33	 * Dispatched every frame the material is loading.
 34	 * 
 35	 * @eventType flash.events.ProgressEvent
 36	 */
 37	[Event(name="progress",type="flash.events.ProgressEvent")]
 38	
 39	/**
 40	 * Bitmap material that loads it's texture from an external bitmapasset file.
 41	 */
 42	public class BitmapFileMaterial extends BitmapMaterial
 43	{
 44		private var _loader:Loader;
 45		private var _dispatcher:Sprite;
 46		private var _uri:String;
 47		
 48		/**
 49		 * Creates a new <code>BitmapFileMaterial</code> object.
 50		 *
 51		 * @param	url					The location of the bitmapasset to load.
 52		 */
 53		public function BitmapFileMaterial( url :String = "", checkPolicy:Boolean = false)
 54		{
 55			super(new BitmapData(64,64, false, 0xFFFFFF));
 56			
 57			url = (url.substring(0,7) == "/file:/")? url.substring(7,url.length) : url;
 58			
 59			if(url.substring(0,2) == "//"){
 60				url = url.toLowerCase();
 61				var charvar:int = 97;
 62				while(charvar<123){
 63					if(url.charCodeAt(2) == charvar){
 64						url = (url.substring(0,5) == "//"+String.fromCharCode(charvar)+":/")? url.substring(2,url.length) : url;
 65						break;
 66					}
 67					charvar++; 
 68				}
 69			}
 70			
 71			if(url.charCodeAt(url.length-1) < 49)
 72				url = url.substring(0, url.length-1);
 73			
 74			_dispatcher = new Sprite();
 75			_loader = new Loader();
 76			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
 77			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
 78			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
 79			
 80			var context:LoaderContext = new LoaderContext();
 81			context.checkPolicyFile = checkPolicy;
 82			_uri = unescape(url);
 83			_loader.load(new URLRequest(_uri), context);
 84		}
 85		
 86		/**
 87		 * @return	the loader used for this material
 88		 */
 89		public function get loader():Loader
 90		{
 91			return _loader;
 92		}
 93		
 94		private function onError(e:IOErrorEvent):void
 95		{
 96			if(_dispatcher.hasEventListener(LoaderEvent.LOAD_ERROR))
 97				_dispatcher.dispatchEvent(new LoaderEvent(LoaderEvent.LOAD_ERROR, _uri, e.text ));
 98		}
 99		
100		private function onProgress(e:ProgressEvent):void
101		{
102			if(_dispatcher.hasEventListener(ProgressEvent.PROGRESS))
103				_dispatcher.dispatchEvent(e);
104		}
105		
106		private function onComplete(e:Event):void
107		{
108			bitmapData = Bitmap(_loader.content).bitmapData;
109			
110			if(_dispatcher.hasEventListener(LoaderEvent.RESOURCE_COMPLETE))
111				_dispatcher.dispatchEvent(new LoaderEvent(LoaderEvent.RESOURCE_COMPLETE, _uri));
112		}
113	}
114}
115