flash on 2010-4-6

by lewis_c1986
This is a tuts plus flex test exercise to demonstrate 
the use of custom contextMenu Items within Flex or 
the wonderful WonderFL

box and textfield classes are examples of how to 
programatically save time generating common 
components with editable qualities
♥0 | Line 103 | Modified 2010-04-07 01:41:45 | MIT License
play

ActionScript3 source code

/**
 * Copyright lewis_c1986 ( http://wonderfl.net/user/lewis_c1986 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wtSQ
 */

/*
	This is a tuts plus flex test exercise to demonstrate 
	the use of custom contextMenu Items within Flex or 
	the wonderful WonderFL
	
	box and textfield classes are examples of how to 
	programatically save time generating common 
	components with editable qualities
*/



package {
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.events.ContextMenuEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.text.*;		// this is bad practice
    import flash.events.*;	// it makes the movieclip 
    							// bigger than it needs to be

    public class Main extends MovieClip {
    		private var builtInItems:ContextMenu = new ContextMenu();
		private var customItemEnabled:ContextMenu = new ContextMenu();
		private var customEnabled:ContextMenuItem = new ContextMenuItem('Get I.T. Done!');
		private var customItemDisabled:ContextMenu = new ContextMenu();
		private var customDisabled:ContextMenuItem = new ContextMenuItem('Disabled Item', false, false);
		private var linkedItem:ContextMenu = new ContextMenu();
		private var linkText:ContextMenuItem = new ContextMenuItem('Go to Get I.T. Done Website', true);
		private var textCM:ContextMenu = new ContextMenu();
		
		private var builtInBox:MovieClip = new MovieClip();
		private var customBox:MovieClip = new MovieClip();
		private var disabledBox:MovieClip = new MovieClip();
		private var linkBox:MovieClip = new MovieClip();
		
        public function Main()
        {
            // write as3 code here.. 
			init();
        }
        private function init():void
        {
        		//contextMenu's
			handleBuiltIn();
			handleCustom();
			handleDisabled();  
			handleLink();  
			handleClipboard();
			
			//movie Clips
			builtInBox.addChild(Box(0x000000));
			builtInBox.addChild(textLabel("no built-in","",builtInItems));
			builtInBox.x = 0; builtInBox.y = 0;
			addChild(builtInBox);
			
			customBox.addChild(Box(0x333333));
			customBox.addChild(textLabel("<b>custom item</b>", "#ff0000", customItemEnabled));
			customBox.x = stage.stageWidth - customBox.width; customBox.y = 0;
			addChild(customBox);
			
			disabledBox.addChild(Box(0x666666));
			disabledBox.addChild(textLabel("disabled item", "#ffff33", customItemDisabled));
			disabledBox.x = 0; disabledBox.y = stage.stageHeight - disabledBox.height;
			addChild(disabledBox);
			
			linkBox.addChild(Box(0x999999));
			linkBox.addChild(textLabel("link item", "#222222", linkedItem));
			linkBox.x = stage.stageWidth - customBox.width; linkBox.y = stage.stageHeight - linkBox.height;
			addChild(linkBox);
        }
        private function handleBuiltIn():void
        {
        		builtInItems.hideBuiltInItems();
        		builtInBox.contextMenu = builtInItems;
        	}
        	private function handleCustom():void
        	{
        		customItemEnabled.customItems.push(customEnabled); // Adds the customItemEnabled ContextMenuItem value to the ContextMenu instance
        		customBox.contextMenu = customItemEnabled; // Sets the ContextMenu instance to the MovieClip
        	}
        	private function handleDisabled():void
        	{
			customItemDisabled.customItems.push(customDisabled);
			disabledBox.contextMenu = customItemDisabled;
		}
		private function handleLink():void
		{
			linkedItem.customItems.push(linkText);  //remember we set the text of this link earlier
			linkText.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMenuSelect);
			linkBox.contextMenu = linkedItem;
		}
		private function onMenuSelect(e:ContextMenuEvent):void  
		{  
			navigateToURL(new URLRequest('http://www.get-it-dun.com'))  
		}
		private function handleClipboard():void
		{
			textCM.clipboardMenu = true;
			textCM.clipboardItems.selectAll = false;
			contextMenu = textCM;
		}
		private function Box(color:uint):Shape
		{
			//common grey box
			var greyBox:Shape = new Shape();
			greyBox.graphics.beginFill(color);
			
			greyBox.graphics.drawRect(0,0,100,50);
			greyBox.graphics.endFill();
			return greyBox;
		}
		private function textLabel(msg:String, color:String = "#ffffff", menu:Object = null):TextField
		{
			color = (color.length != 7) ? ("#ffffff") : (color);
			var txt:TextField = new TextField();
			txt.htmlText = "<font face=\"arial\" color=\""+color+"\" >"+msg+"<\/font>";
			txt.autoSize = TextFieldAutoSize.CENTER;			
			txt.selectable = false; // because these are labels there is no need to make the text selectable
			txt.contextMenu = ((menu != null) && (menu is ContextMenu)) ? (ContextMenu(menu)) :		 (				new ContextMenu());
			txt.y = 14;
			return txt;
		}
    }
}