Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.DisplayObject;
- import flash.display.MovieClip;
- import flash.events.MouseEvent;
- import flash.geom.Rectangle;
- /**
- * ...
- * @author Samuel Jacob Walker
- */
- public class EditableObject extends MovieClip
- {
- private var dragType:String;
- private var prevMouseX:int;
- private var prevMouseY:int;
- public function EditableObject()
- {
- //side right
- rightside_mc.addEventListener(MouseEvent.MOUSE_DOWN, rightMouseDown);
- rightside_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- //side bottom
- bottomside_mc.addEventListener(MouseEvent.MOUSE_DOWN, bottomMouseDown);
- bottomside_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- //Manual scale code
- //bottomright
- bottomright_mc.addEventListener(MouseEvent.MOUSE_DOWN, bottomRightDown);
- bottomright_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- //Basic drag
- center_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
- }
- public function addDisplay(displayObject:DisplayObject) //Parent uses addDisplay instead of addChild
- {
- //Proper size and positioning of all objects
- leftside_mc.height = displayObject.height;
- rightside_mc.height = displayObject.height;
- rightside_mc.x = displayObject.width;
- topside_mc.width = displayObject.width;
- bottomside_mc.width = displayObject.width;
- bottomside_mc.y = displayObject.height;
- center_mc.bg_mc.width = displayObject.width;
- center_mc.bg_mc.height = displayObject.height;
- bottomright_mc.x = displayObject.width - 15;
- bottomright_mc.y = displayObject.height - 13;
- center_mc.addChild(displayObject);
- }
- private function handleDrag(e:MouseEvent):void
- {
- this.startDrag();
- addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
- }
- private function stopDragHandler(e:MouseEvent):void
- {
- this.stopDrag();
- removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
- }
- private function bottomRightDown(e:MouseEvent):void
- {
- dragType = "bottomright";
- prevMouseX = e.stageX;
- prevMouseY = e.stageY;
- this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- trace("Bottom right mouse down");
- }
- private function bottomMouseDown(e:MouseEvent):void
- {
- dragType = "bottom";
- prevMouseX = e.stageX;
- prevMouseY = e.stageY;
- this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- }
- private function rightMouseDown(e:MouseEvent):void
- {
- dragType = "right";
- prevMouseX = e.stageX;
- prevMouseY = e.stageY;
- this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
- trace("Right mouse down.");
- }
- private function mouseUp(e:MouseEvent):void
- {
- this.parent.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- this.parent.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
- trace("Mouse up. Removing event listener..");
- }
- private function handleMouseMove(e:MouseEvent):void
- {
- switch(dragType)
- {
- case "right":
- trace("Right triggered.");
- this.width = e.stageX - this.x;
- break;
- case "bottom":
- trace("Bottom triggered");
- this.height = e.stageY - this.y;
- break;
- case "bottomright":
- resizeMe(this, e.stageX - this.x, this.height);
- break;
- }
- prevMouseX = e.stageX;
- prevMouseY = e.stageY;
- }
- //Author/Tutorial of resizeMe at https://circlecube.com/says/2009/01/how-to-as3-resize-a-movieclip-and-constrain-proportions-actionscript-tutorial/
- private function resizeMe(mc:MovieClip, maxW:Number, maxH:Number = 0, constrainProportions:Boolean = true):void
- {
- maxH = maxH == 0 ? maxW : maxH;
- mc.width = maxW;
- mc.height = maxH;
- if (constrainProportions)
- {
- mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement