In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Rotate and Resize

@author Mj Mendoza IV
Get Adobe Flash player
by creek23 18 Sep 2009
    Embed
/**
 * Copyright creek23 ( http://wonderfl.net/user/creek23 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ghtO
 */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * @author Mj Mendoza IV
	 */
	public class Test extends Sprite {
		private var mcMain:Sprite;
		private var mcRect:Sprite;
		private var mcTri:Sprite;
		
		private var traceBox:TextField;
		
		private var rotationValue:int;
		
		private var do_resize:Boolean;
		private var do_rotate:Boolean;
		private var do_drag:Boolean;
		
		private var main_Rect:Rectangle;
		private var main_prevRect:Rectangle;
		
		private var mcRect_start:Point;		
		private var mcTri_start:Point;
		
		public function Test():void {
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		private function addedToStage(e:Event):void  {
			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			
			mcMain = new Sprite();
			
			mcRect = new Sprite();
				mcMain.addChild(mcRect);
			
			mcTri = new Sprite();
				mcMain.addChild(mcTri);
			
			mcRect.addEventListener(MouseEvent.MOUSE_DOWN, mcRect_onMouseDown);
			mcRect.addEventListener(MouseEvent.MOUSE_UP, mcRect_onMouseUp);
			
			mcTri.addEventListener(MouseEvent.MOUSE_DOWN, mcTri_onMouseDown);
			mcTri.addEventListener(MouseEvent.MOUSE_UP, mcTri_onMouseUp);
			
			mcMain.addEventListener(MouseEvent.MOUSE_DOWN, mcMain_onMouseDown);
			mcMain.addEventListener(MouseEvent.MOUSE_UP, mcMain_onMouseUp);
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
			
			var rect_pane:Graphics = mcRect.graphics;
				rect_pane.beginFill(0x336699, 1);
					rect_pane.drawRect(0, 0, 10, 10);
				rect_pane.endFill();
			
			var tri_pane:Graphics = mcTri.graphics;
				tri_pane.beginFill(0x6699CC, 1);
					tri_pane.moveTo(5,0);
					tri_pane.lineTo(12, 10);
					tri_pane.lineTo( -2, 10);
					tri_pane.lineTo(5, 0);
				tri_pane.endFill();
			
			main_Rect = new Rectangle(0,0,50,25);
			
			addChild(mcMain);
			
			traceBox = new TextField();
				addChild(traceBox);
			
			mcMain.x = 200;
			mcMain.y = 200;
			
			rotationValue = 0;
			
			tryRender();
		}
		private function mcRect_onMouseDown(e:MouseEvent):void {
			do_resize = true;
			
			mcRect_start = new Point(mouseX,mouseY);
			main_prevRect = new Rectangle(0,0,main_Rect.width,main_Rect.height);
			
			echo("start resize");
		}
		private function mcRect_onMouseUp(e:MouseEvent):void {
			do_resize = false;
			mcMain_onMouseUp(e);
			echo("stop resize");
		}
		private function mcTri_onMouseDown(e:MouseEvent):void {
			do_rotate = true;
			
			mcTri_start = new Point(mouseX, mouseY);
			
			echo("start rotate");
		}
		private function mcTri_onMouseUp(e:MouseEvent):void {
			do_rotate = false;
			mcMain_onMouseUp(e);
			echo("stop rotate");
		}
		private function mcMain_onMouseDown(e:MouseEvent):void {
			do_drag = true;
			
			//mcTri_start = new Point(mouseX, mouseY);
			
			echo("start drag");
		}
		private function mcMain_onMouseUp(e:MouseEvent):void {
			do_drag = false;
			echo("stop drag");
		}
		private function stage_onMouseMove(e:MouseEvent):void {
			if (do_resize) {
				resizeWindow();
			} else if (do_rotate) {
				rotateWindow();
			} else if (do_drag) {
				dragWindow();
			}
		}
		private function stage_onMouseUp(e:MouseEvent):void {
			mcRect_onMouseUp(e);
			mcTri_onMouseUp(e);
			echo();
		}
			private function resizeWindow():void {
				main_Rect.width = main_prevRect.width + (mouseX - mcRect_start.x);
				main_Rect.height = main_prevRect.height + (mouseY - mcRect_start.y);
				
				tryRender();
			}
			private function rotateWindow():void {
				var dx:int = mouseX - mcMain.x;
				var dy:int = mouseY - mcMain.y;
				rotationValue = Math.atan2(dy, dx) * 180 / Math.PI;
				tryRender();
			}
			private function dragWindow():void {
				mcMain.x = mouseX;
				mcMain.y = mouseY;
				tryRender();
			}
			
		private function tryRender():void {
			try {
				render();
			} catch (e:Error) {
				echo("can't render");
			}
		}
			private function render():void {
				var main_pane:Graphics = mcMain.graphics;
					main_pane.clear();
					main_pane.beginFill(0x99CCFF, 1);
						main_pane.drawRect( -main_Rect.width, -main_Rect.height, (main_Rect.width*2), (main_Rect.height*2));
					main_pane.endFill();
					
					//place resizer at the bottom right
					mcRect.x = main_Rect.width - 5;
					mcRect.y = main_Rect.height - 5;
					
					//place rotator the upper right
					mcTri.x = main_Rect.width - 5;
					mcTri.y = - main_Rect.height - 5;
				
				mcMain.rotation = rotationValue;
			}
		private function echo(msg:String = null):void {
			if (msg == null) {
				traceBox.text = "";
			} else {
				traceBox.text = msg + "\n" + traceBox.text;
			}
		}
	}
}