PageRenderTime 32ms CodeModel.GetById 24ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/source/flash/src/Main.as

http://github.com/agross/netopenspace
ActionScript | 103 lines | 75 code | 21 blank | 7 comment | 9 complexity | 84d2cc854944692b5c104b88a7b92d5a MD5 | raw file
  1package 
  2{
  3	import flash.display.LoaderInfo;
  4	import flash.display.Sprite;
  5	import flash.events.Event;
  6	import flash.system.System;
  7	import flash.external.ExternalInterface;
  8	import flash.events.MouseEvent;
  9	import flash.display.Loader;
 10	import flash.net.URLRequest;
 11		
 12	/**
 13	 * ...
 14	 * @author mhoyer
 15	 */
 16	public class Main extends Sprite 
 17	{
 18		private var containerWidth:Number;
 19		private var containerHeight:Number;
 20		
 21		public function Main():void 
 22		{
 23			if (stage) init();
 24			else addEventListener(Event.ADDED_TO_STAGE, init);
 25		}
 26
 27		private function init(e:Event = null):void 
 28		{
 29			buttonMode = true;
 30			
 31			removeEventListener(Event.ADDED_TO_STAGE, init);
 32			addEventListener(MouseEvent.MOUSE_DOWN, copy);
 33			
 34			containerWidth = parseInt(getParameter("width", stage.width.toString()));
 35			containerHeight = parseInt(getParameter("height", stage.height.toString()));
 36			
 37			//trace("\n\ncontainer dimension: " + containerWidth + "x" + containerHeight);
 38
 39			addChild(loadContent(getParameter("backgroundAsset", "nos-badge-125x125.png")));
 40		}
 41		
 42		private function getParameter(name:String, defaultValue:String = null):String
 43		{
 44			var result:String = loaderInfo.parameters[name];
 45			
 46			if (result == null)
 47			{
 48				return defaultValue;
 49			}
 50
 51			return result;
 52		}
 53		
 54		private function copy(e:Event = null):void
 55		{
 56			var clipboardContent:String = getParameter("clipboard");
 57			
 58			if (clipboardContent != null)
 59			{
 60				System.setClipboard(clipboardContent);
 61			}
 62			else
 63			{
 64				System.setClipboard("No parameters set.");
 65			}
 66			
 67			executeCommand(getParameter("callback"));
 68		}
 69		
 70		private function executeCommand(callback:String) : Object
 71		{
 72			trace("callback: " + callback);
 73			
 74			if (ExternalInterface.available && callback != null)
 75				return ExternalInterface.call(
 76					"eval",
 77					callback
 78				);
 79				
 80			return null;
 81		}
 82		
 83		public function loadContent(url:String):Loader
 84		{
 85			var loader:Loader = new Loader();
 86			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_Completed);
 87			loader.load(new URLRequest(url));
 88			
 89			return loader;
 90		}
 91		
 92		private function loader_Completed(event:Event):void
 93		{
 94			var li:LoaderInfo = event.target as LoaderInfo;
 95			
 96			li.loader.x = (100 - containerWidth) / 2;
 97			li.loader.y = (100 - containerHeight) / 2;
 98			
 99			// trace("Loader: " + li.loader.x + ":" + li.loader.y + " (dim= " + li.loader.width + "x" + li.loader.height + ")");
100			// trace(stage.width);
101		}
102	}
103}