Tuesday, March 23, 2010

Dragging a rotated skewed scaled object inside a rectangle boundary

Create a movieclip named outer_mc. Inside that create an another movieclip named inner_mc
Goto to root's first frame, type this code

Here is the source http://rapidshare.com/files/367143837/DragRestriction.zip.html

//////////////////////////FRAME CODE///////////////////////////////
outer_mc.inner_mc.addEventListener(MouseEvent.MOUSE_DOWN,initDrag);

function initDrag(e:MouseEvent){
DragManager.target=outer_mc.inner_mc;
DragManager.dragArea=new Rectangle(0,0,560,400);
}
///////////////////////////////////////////////////////////////////////

////////////////////////AS CODE//////////////////////////////////////
package {
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.geom.Point;
public class DragManager {
/////////PRIVATE VARIABLES DECLARATION////////////////////////////////
private static var mxx:Number;
private static var myy:Number;
private static var delX:Number;
private static var delY:Number;
private static var __target:DisplayObject;
private static var targetWidth:Number;
private static var targetHeight:Number;
private static var topLeftRegPoint:Point=new Point();
private static var __dragArea:Rectangle;
//////////PUBLIC VARIABLES DECLARATION////////////////////////////////
public static var mouseMoveFunction:Function;
public static var mouseUpFunction:Function;

//////////SETTING TARGET///////////////////////////////////////////////
public static function set target($target:DisplayObject) {
if ($target&&$target.root) {
///////////////////////////////////////////////////////////////
__target=$target;
targetWidth=target.getRect(target).width;
targetHeight=target.getRect(target).height;
topLeftRegPoint.x=target.getRect(target).x;
topLeftRegPoint.y=target.getRect(target).y;
delX=__target.parent.mouseX-__target.x;
delY=__target.parent.mouseY-__target.y;
__target.stage.addEventListener(MouseEvent.MOUSE_MOVE,dragTheTarget);
__target.stage.addEventListener(MouseEvent.MOUSE_UP,dropTheTarget);
}
}
public static function get target():DisplayObject {
return __target;
}
public static function set dragArea($dragArea:Rectangle) {
__dragArea=$dragArea;
}
public static function get dragArea():Rectangle {
return __dragArea;
}
//////////DRAGGING/////////////////////////////////////////////////////
private static function dragTheTarget(e:MouseEvent) {
mxx=target.parent.mouseX;
myy=target.parent.mouseY;

mxx=mxx<0?0:mxx;
mxx=mxx>(dragArea.x+dragArea.width)?(dragArea.x+dragArea.width):mxx;
myy=myy<0?0:myy;
myy=myy>(dragArea.y+dragArea.height)?(dragArea.y+dragArea.height):myy;

target.x=mxx-delX;
target.y=myy-delY;
target.visible=false
if (dragArea!=null) {
checkBoundary();
}
if (mouseMoveFunction!=null) {
mouseMoveFunction();
}
target.visible=true;
e.updateAfterEvent();
}

//////////DROPPING/////////////////////////////////////////////////////
private static function dropTheTarget(e:MouseEvent) {
if (mouseUpFunction!=null) {
mouseUpFunction();
}
target.stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragTheTarget);
target.stage.removeEventListener(MouseEvent.MOUSE_UP,dropTheTarget);
target=null;
}

/////////CHECKING BOUNDARY/////////////////////////////////////////////
private static function checkBoundary() {
var pt_1:Point=getPointWRTTargetParent(new Point(topLeftRegPoint.x,topLeftRegPoint.y));
var pt_2:Point=getPointWRTTargetParent(new Point(topLeftRegPoint.x+targetWidth,topLeftRegPoint.y));
var pt_3:Point=getPointWRTTargetParent(new Point(topLeftRegPoint.x+targetWidth,topLeftRegPoint.y+targetHeight));
var pt_4:Point=getPointWRTTargetParent(new Point(topLeftRegPoint.x,topLeftRegPoint.y+targetHeight));
debugger(pt_1,pt_2,pt_3,pt_4);
restrictPoint(dragArea,pt_1);
restrictPoint(dragArea,pt_2);
restrictPoint(dragArea,pt_3);
restrictPoint(dragArea,pt_4);
}
/////////DEBUGGER//////////////////////////////////////////////////////
private static function debugger(pt_1:Point,pt_2:Point,pt_3:Point,pt_4:Point) {
var par=target.parent;
par.graphics.clear();
par.graphics.lineStyle(5,0x00ff00,1);
par.graphics.moveTo(pt_1.x,pt_1.y);
par.graphics.lineTo(pt_2.x,pt_2.y);
par.graphics.lineTo(pt_3.x,pt_3.y);
par.graphics.lineTo(pt_4.x,pt_4.y);
par.graphics.lineTo(pt_1.x,pt_1.y);
}
/////////MAIN---->POINT RESTRICTION////////////////////////////////////
private static function restrictPoint($dragArea:Rectangle,pt:Point) {
var del:Number;
if (pt.x<$dragArea.x) {
del=$dragArea.x-pt.x;
target.x=target.x+del;
}
if (pt.x>$dragArea.x+$dragArea.width) {
del=pt.x-($dragArea.x+$dragArea.width);
target.x=target.x-del;
}
if (pt.y<$dragArea.y) {
del=$dragArea.y-pt.y;
target.y=target.y+del;
}
if (pt.y>$dragArea.y+$dragArea.height) {
del=pt.y-($dragArea.y+$dragArea.height);
target.y=target.y-del;
}
delX=mxx-__target.x;
delY=myy-__target.y;
}
/////////COORDINATE TRANSFER///////////////////////////////////////////
private static function getPointWRTTargetParent(pt:Point):Point {
var newPt:Point=new Point();
newPt=target.localToGlobal(pt);
newPt=target.parent.globalToLocal(newPt);
return newPt;
}
}
}



Monday, March 22, 2010

Hi friends

Hi friends,

Welcome to my come blog. I will share my knowledge(too small) through this blog. Please post your views.