Konstantin's BLog
Function Viewer
home top contents previous up next

package
{
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;

	
	public class ImageTransformatorUserControl extends UIComponent
	{
		
   		protected var sign:TextField = null;
        private var format:TextFormat = null;
        protected var drawHandler:Boolean;
        private var txtColor:int;
        private var initialX:int;
        private var initialY:int;
        private var displayChangesText:Boolean;
        protected var signInitialText:String;
        private var textWhenMouseOver:String;
        
        private var controlMe:DrawFunction;
        private var allowMove:Boolean;
        protected var colorBorderLow:int = 0x005500;
        protected var colorBodyLow:int = 0x55CC55;
        protected var colorBorderHigh:int = colorBorderLow;
        protected var colorBodyHigh:int = 0x55CC55;

        		
		public function ImageTransformatorUserControl(
		                      posX:int, posY:int, 
		                      text:String, fontSize:int, txtColor:int, 
		                      drawHandler:Boolean, displayChangesText:Boolean, 
		                      controlMe:DrawFunction, allowMove:Boolean,
		                      textWhenMouseOver:String)
		{
			this.txtColor = txtColor;
			this.drawHandler = drawHandler;
			this.displayChangesText = displayChangesText;
			this.signInitialText = text;
			this.controlMe = controlMe;
			this.allowMove = allowMove;
			this.textWhenMouseOver = textWhenMouseOver;
            x = posX; //To parent?
            y = posY;
            initialX = x;
            initialY = y;
			//this.addEventListener( mx.events.FlexEvent.
			if( allowMove )
			{
    			addEventListener( MouseEvent.MOUSE_DOWN, doMouseDown );
	    		addEventListener( MouseEvent.MOUSE_UP, doMouseUp );
	    		//We use "this...." for a clue for a reader to use Flex 3.0 GUI for intellisence:
         		this.addEventListener( MouseEvent.ROLL_OVER, doMouseOver );
		       	this.addEventListener( MouseEvent.MOUSE_OUT, doMouseOut );
	        }
            
            sign = new TextField();
            format = new TextFormat( "Arial", fontSize, 0x000000 );
            sign.x = 15; //To this?
            sign.y = 0;
            sign.defaultTextFormat = format;
            sign.textColor = txtColor;
            sign.autoSize = TextFieldAutoSize.LEFT;
            sign.selectable = false;
            sign.text = text
            addChild(sign);
            doDrawHandler(colorBorderLow, colorBodyLow);
		}
		
		//Returns change in percent units.
		//Change depends on distance from initial point.
		public function userChange():int
		{
			var a:Number = Math.abs(x - initialX);
          	var b:Number = Math.abs(y - initialY);
			return (Math.sqrt( a*a + b*b ) % 200)/2;
		}

        protected function doDrawHandler(colorBorder:int, colorBody:int):void
        {
        	if( !drawHandler ) return; 
        	with( graphics )
		    {
        	   lineStyle(1,colorBorder);
               beginFill(colorBody);
               drawCircle(0,0,5);
               sign.textColor = colorBody;
            }
        }

		
		public function doMouseDown(event:MouseEvent):void
		{
		   //var handle:UIComponent = UIComponent( event.target );   
           //handle.addEventListener( MouseEvent.MOUSE_MOVE, doMouseMove );
           //handle.startDrag();
           //Not clear why this does not work:
           addEventListener( MouseEvent.MOUSE_MOVE, doMouseMove );
	       //graphics.clear()
	       doDrawHandler(colorBorderHigh,colorBodyHigh);
           startDrag();
        }


		public function doMouseUp(event:MouseEvent):void
		{

		   //Apparently both ways can be used: this.... and handle....:
		   //However, falling on text will case run-time error.	
		   //var handle:UIComponent = UIComponent( event.target );
	       //handle.removeEventListener( MouseEvent.MOUSE_MOVE, doMouseMove );
           //handle.stopDrag();
           
           this.removeEventListener( MouseEvent.MOUSE_MOVE, doMouseMove );
           this.stopDrag();
	       if( drawHandler ) doDrawHandler(colorBorderLow,colorBodyLow);
           if( controlMe != null ) 
           { 
             controlMe.doWork( Math.PI * userChange()/100. );
           }
        }


        private function doMouseOver(event:MouseEvent):void
        {	
        	if( !drawHandler ) return;
        	doDrawHandler(0x000055,0xBBBBFF);
        	if( textWhenMouseOver != "" ) sign.text = textWhenMouseOver;
        }

        private function doMouseOut(event:MouseEvent):void
        {	
            doDrawHandler(colorBorderLow, colorBodyLow);
           	if( textWhenMouseOver != "" ) sign.text = signInitialText;
        }
	
		public function doMouseMove(event:MouseEvent):void
		{
		   //var handle:UIComponent = UIComponent( event.target );
		   //doDrawInitalHandler();
		   //graphics.clear();
		   //drawMovingHandler();
		   if( displayChangesText ) doDisplayChangeInPercents();
		   if( controlMe != null ) 
           { 
             controlMe.doWork( Math.PI * userChange()/100. );
           }
        }
        
        
    	private function doDisplayChangeInPercents():void
    	{
        	with( graphics)
			{
               var i:int = userChange();
               if( i > 10 ) 
               {
                  sign.text = String(userChange())+"%";
               }
               else
               {
               	  sign.text = signInitialText;
               }
	       }
		}
        
	}
}

Copyright (C) 2008 Landkey Computers