Advertisement
Guest User

EditableObject

a guest
Nov 19th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import flash.display.DisplayObject;
  4.     import flash.display.MovieClip;
  5.     import flash.events.MouseEvent;
  6.     import flash.geom.Rectangle;
  7.     /**
  8.      * ...
  9.      * @author Samuel Jacob Walker
  10.      */
  11.     public class EditableObject extends MovieClip
  12.     {
  13.         private var dragType:String;
  14.         private var prevMouseX:int;
  15.         private var prevMouseY:int;
  16.        
  17.         public function EditableObject()
  18.         {
  19.             //side right
  20.             rightside_mc.addEventListener(MouseEvent.MOUSE_DOWN, rightMouseDown);
  21.             rightside_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  22.             //side bottom
  23.             bottomside_mc.addEventListener(MouseEvent.MOUSE_DOWN, bottomMouseDown);
  24.             bottomside_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  25.            
  26.             //Manual scale code
  27.             //bottomright
  28.             bottomright_mc.addEventListener(MouseEvent.MOUSE_DOWN, bottomRightDown);
  29.             bottomright_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  30.            
  31.             //Basic drag
  32.             center_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
  33.         }
  34.        
  35.         public function addDisplay(displayObject:DisplayObject) //Parent uses addDisplay instead of addChild
  36.         {
  37.             //Proper size and positioning of all objects
  38.             leftside_mc.height = displayObject.height;
  39.             rightside_mc.height = displayObject.height;
  40.             rightside_mc.x = displayObject.width;
  41.             topside_mc.width = displayObject.width;
  42.             bottomside_mc.width = displayObject.width;
  43.             bottomside_mc.y = displayObject.height;
  44.             center_mc.bg_mc.width = displayObject.width;
  45.             center_mc.bg_mc.height = displayObject.height;
  46.            
  47.             bottomright_mc.x = displayObject.width - 15;
  48.             bottomright_mc.y = displayObject.height - 13;
  49.            
  50.             center_mc.addChild(displayObject);
  51.         }
  52.        
  53.         private function handleDrag(e:MouseEvent):void
  54.         {
  55.             this.startDrag();
  56.             addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
  57.         }
  58.        
  59.         private function stopDragHandler(e:MouseEvent):void
  60.         {
  61.             this.stopDrag();
  62.             removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
  63.         }
  64.        
  65.         private function bottomRightDown(e:MouseEvent):void
  66.         {
  67.             dragType = "bottomright";
  68.             prevMouseX = e.stageX;
  69.             prevMouseY = e.stageY;
  70.             this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  71.             this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  72.            
  73.             trace("Bottom right mouse down");
  74.         }
  75.        
  76.         private function bottomMouseDown(e:MouseEvent):void
  77.         {
  78.             dragType = "bottom";
  79.             prevMouseX = e.stageX;
  80.             prevMouseY = e.stageY;
  81.             this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  82.             this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  83.         }
  84.        
  85.         private function rightMouseDown(e:MouseEvent):void
  86.         {
  87.             dragType = "right";
  88.             prevMouseX = e.stageX;
  89.             prevMouseY = e.stageY;
  90.             this.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  91.             this.parent.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  92.  
  93.             trace("Right mouse down.");
  94.         }
  95.        
  96.         private function mouseUp(e:MouseEvent):void
  97.         {
  98.             this.parent.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  99.             this.parent.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
  100.  
  101.             trace("Mouse up. Removing event listener..");
  102.         }
  103.        
  104.         private function handleMouseMove(e:MouseEvent):void
  105.         {
  106.             switch(dragType)
  107.             {
  108.                 case "right":
  109.                     trace("Right triggered.");
  110.                     this.width = e.stageX - this.x;
  111.                     break;
  112.                 case "bottom":
  113.                     trace("Bottom triggered");
  114.                     this.height = e.stageY - this.y;
  115.                     break;
  116.                 case "bottomright":
  117.                     resizeMe(this, e.stageX - this.x, this.height);
  118.                     break;
  119.             }
  120.            
  121.            
  122.             prevMouseX = e.stageX;
  123.             prevMouseY = e.stageY;
  124.         }
  125.        
  126.         //Author/Tutorial of resizeMe at https://circlecube.com/says/2009/01/how-to-as3-resize-a-movieclip-and-constrain-proportions-actionscript-tutorial/
  127.         private function resizeMe(mc:MovieClip, maxW:Number, maxH:Number = 0, constrainProportions:Boolean = true):void
  128.         {
  129.             maxH = maxH == 0 ? maxW : maxH;
  130.             mc.width = maxW;
  131.             mc.height = maxH;
  132.             if (constrainProportions)
  133.             {
  134.                 mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
  135.             }
  136.         }
  137.        
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement