Advertisement
Guest User

Untitled

a guest
Nov 1st, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.view
  2. {
  3.     import com.controller.LineEvent;
  4.     import com.helpers.LineTools;
  5.     import com.model.vo.DataVO;
  6.    
  7.     import flash.display.Sprite;
  8.     import flash.events.Event;
  9.     import flash.events.MouseEvent;
  10.     import flash.geom.Matrix;
  11.     import flash.geom.Point;
  12.     import flash.geom.Rectangle;
  13.    
  14.     import org.robotlegs.mvcs.Mediator;
  15.  
  16.     public class FurnitureMediator extends Mediator
  17.     {
  18.         [Inject]
  19.         public var furniture:FurnitureView;
  20.  
  21.         private var item:Sprite;
  22.  
  23.         private var canMove:Boolean=false;
  24.  
  25.         private var pointsArray:Array;
  26.         private var itemRect:Rectangle;
  27.         private var rotation:Number=0;
  28.  
  29.         private var itemLastPositionX:Number;
  30.         private var itemLastPositionY:Number;
  31.  
  32.         private var collisionWallPoints:Array;
  33.         private var wallIntersectPoints:Array;
  34.  
  35.         private var outSideBounds:Array;
  36.  
  37.         override public function onRegister():void
  38.         {
  39.             item=furniture.item;
  40.  
  41.             pointsArray=DataVO.cornerPoints;
  42.  
  43.             itemRect=item.getBounds(furniture);
  44.  
  45.             //for rotating center
  46.             itemRect.x=item.x - 25;
  47.             itemRect.y=item.y - 50;
  48.  
  49.             item.x=100;
  50.             item.y=100;
  51.  
  52.             eventMap.mapListener(item.stage, MouseEvent.MOUSE_UP, mouseUpHandler);
  53.             eventMap.mapListener(item, MouseEvent.MOUSE_DOWN, mouseDownHandler);
  54.             eventMap.mapListener(item, Event.ENTER_FRAME, render);
  55.         }
  56.  
  57.  
  58.         protected function render(event:Event):void
  59.         {
  60.             if (canMove)
  61.             {
  62.                 item.x=DataVO.mouseX;
  63.                 item.y=DataVO.mouseY;
  64.  
  65.                 itemRect.x=item.x - 25;
  66.                 itemRect.y=item.y - 50;
  67.  
  68.                 if (getCollisionWallCornerPoints())
  69.                 {
  70.                     collisionWallPoints = getCollisionWallCornerPoints();
  71.                 }
  72.  
  73.                 if (getObjectWallIntersectPoints(rotation, item, itemRect))
  74.                 {
  75.                     wallIntersectPoints = getObjectWallIntersectPoints(rotation, item, itemRect);
  76.                 }
  77.  
  78.                 if (collisionWallPoints)
  79.                 {
  80.                     rotation=getSnapRotationOfTwoPoint(collisionWallPoints);
  81.  
  82.                     item.rotation=rotation;
  83.                 }
  84.  
  85.                 if (wallIntersectPoints)
  86.                 {
  87.                     if (wallIntersectPoints.length > 0)
  88.                     {
  89.                         outSideBounds = getOutsidePointOfPolygon(getRotatedBoundsOfObject(rotation, item, itemRect));
  90.  
  91.                         if (wallIntersectPoints.length > 0)
  92.                         {
  93.                             try
  94.                             {
  95.                                 var diffX:Number = outSideBounds[0].x - wallIntersectPoints[1][0].x;
  96.                                 var diffY:Number = outSideBounds[0].y - wallIntersectPoints[1][0].y;
  97.                                
  98.                                 if (Math.abs(diffX) < 50 && Math.abs(diffY) < 50)
  99.                                 {
  100.                                     item.x=DataVO.mouseX + (diffX * -1);
  101.                                     item.y=DataVO.mouseY + (diffY * -1);
  102.                                    
  103.                                     itemRect.x=item.x - 25;
  104.                                     itemRect.y=item.y - 50;
  105.                                 }
  106.                             }
  107.                             catch(error:Error)
  108.                             {
  109.                                 trace(error);
  110.                             }
  111.  
  112.                         }
  113.                     }
  114.                 }
  115.  
  116.             }
  117.         }
  118.  
  119.         private function getOutsidePointOfPolygon(bounds:Array):Array
  120.         {
  121.             var points:Array=new Array();
  122.  
  123.             for (var i:int=0; i < 4; i++)
  124.             {
  125.                 var array:Array=new Array();
  126.                 var check:Boolean=LineTools.isPointInsidePolygon(bounds[i], pointsArray, convertPointsToWall(pointsArray).length);
  127.                 array.push(check);
  128.                 array.push(bounds[i]);
  129.  
  130.                 points.push(array);
  131.             }
  132.  
  133.             var result:Array=new Array();
  134.  
  135.             for (var j:int=0; j < points.length; j++)
  136.             {
  137.                 if (!points[j][0])
  138.                 {
  139.                     result.push(points[j][1]);
  140.                 }
  141.             }
  142.  
  143.             if (result.length > 0)
  144.             {
  145.                 return result;
  146.             }
  147.  
  148.             return null;
  149.         }
  150.  
  151.         private function pointInsidePolygon(point:Point):Boolean
  152.         {
  153.             var check:Boolean=false;
  154.  
  155.             check=LineTools.isPointInsidePolygon(point, pointsArray, convertPointsToWall(pointsArray).length);
  156.             return check;
  157.         }
  158.  
  159.         private function getObjectWallIntersectPoints(rotation:Number, object:Sprite, itemRect:Rectangle):Array
  160.         {
  161.             var result:Array=new Array();
  162.             var wallCornerPoints:Array=convertPointsToWall(pointsArray);
  163.             var rotatedItemBounds:Array=getRotatedBoundsOfObject(rotation, item, itemRect);
  164.  
  165.             for (var i:int=0; i < wallCornerPoints.length; i++)
  166.             {
  167.                 var wallPoints:Array=new Array();
  168.                 var boxIntersect:Array=LineTools.boxIntersect(wallCornerPoints[i][0], wallCornerPoints[i][1], rotatedItemBounds[0], rotatedItemBounds[1], rotatedItemBounds[2], rotatedItemBounds[3]);
  169.  
  170.                 if (boxIntersect)
  171.                 {
  172.                     wallPoints.push(wallCornerPoints[i]);
  173.                     wallPoints.push(boxIntersect);
  174.                     result.push(wallPoints);
  175.                 }
  176.             }
  177.  
  178.             //if two or more wall intersect
  179.             if (result.length >= 2)
  180.             {
  181.                 return null;
  182.             }
  183.  
  184.             //if one wall intersect
  185.             if (result.length == 1)
  186.             {
  187.                 return result[0];
  188.             }
  189.  
  190.             return new Array();
  191.         }
  192.  
  193.         private function getRotatedBoundsOfObject(rotation:Number, object:Sprite, objectRectangle:Rectangle):Array
  194.         {
  195.             var rotationInRadians:Number=degreesToRadians(rotation);
  196.  
  197.             var itemCenterPoint:Point=new Point(object.x, object.y);
  198.  
  199.             var realBounds:Array=getBoundsOfObject(objectRectangle);
  200.  
  201.             var bounds:Array=new Array();
  202.  
  203.             var topLeft:Point=getRotatedRectPoint(rotationInRadians, realBounds[0], itemCenterPoint);
  204.             var topRight:Point=getRotatedRectPoint(rotationInRadians, realBounds[1], itemCenterPoint);
  205.             var bottomRight:Point=getRotatedRectPoint(rotationInRadians, realBounds[2], itemCenterPoint);
  206.             var bottomLeft:Point=getRotatedRectPoint(rotationInRadians, realBounds[3], itemCenterPoint);
  207.  
  208.             bounds.push(topLeft);
  209.             bounds.push(topRight);
  210.             bounds.push(bottomRight);
  211.             bounds.push(bottomLeft);
  212.  
  213.             return bounds;
  214.         }
  215.  
  216.         private function getBoundsOfObject($objectRectangle:Rectangle):Array
  217.         {
  218.             var bounds:Array=new Array();
  219.  
  220.             var topLeft:Point=$objectRectangle.topLeft;
  221.             var topRight:Point=new Point($objectRectangle.right, $objectRectangle.top);
  222.             var bottomRight:Point=$objectRectangle.bottomRight;
  223.             var botomLeft:Point=new Point($objectRectangle.x, $objectRectangle.bottom);
  224.  
  225.             bounds.push(topLeft);
  226.             bounds.push(topRight);
  227.             bounds.push(bottomRight);
  228.             bounds.push(botomLeft);
  229.  
  230.             return bounds;
  231.         }
  232.  
  233.         private function getRotatedRectPoint(angle:Number, point:Point, rotationPoint:Point=null):Point
  234.         {
  235.             var ix:Number=(rotationPoint) ? rotationPoint.x : 0;
  236.             var iy:Number=(rotationPoint) ? rotationPoint.y : 0;
  237.  
  238.             var m:Matrix=new Matrix(1, 0, 0, 1, point.x - ix, point.y - iy);
  239.             m.rotate(angle);
  240.             return new Point(m.tx + ix, m.ty + iy);
  241.         }
  242.  
  243.         private function radiansToDegrees(radians:Number):Number
  244.         {
  245.             var degrees:Number=radians * 180 / Math.PI;
  246.             return degrees;
  247.         }
  248.  
  249.         private function degreesToRadians(degrees:Number):Number
  250.         {
  251.             var radians:Number=degrees * Math.PI / 180
  252.             return radians;
  253.         }
  254.  
  255.         private function drawRuler():void
  256.         {
  257.             var wallCornerPoints:Array=getCollisionWallCornerPoints();
  258.             var wallIntersectPoints:Array=getObjectWallIntersectPoints(rotation, item, itemRect);
  259.             var rulerPoints:Array=new Array();
  260.  
  261.             if (!wallCornerPoints || !wallIntersectPoints)
  262.                 return;
  263.  
  264.             if (Point.distance(wallCornerPoints[0], wallIntersectPoints[0]) > Point.distance(wallCornerPoints[0], wallIntersectPoints[1]))
  265.             {
  266.                 rulerPoints.push(wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[0], wallIntersectPoints[1], wallIntersectPoints[0], wallCornerPoints[1]);
  267.                 dispatch(new LineEvent(LineEvent.PLACE_RULER, rulerPoints));
  268.             }
  269.             else
  270.             {
  271.                 rulerPoints.push(wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[0], wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[1]);
  272.                 dispatch(new LineEvent(LineEvent.PLACE_RULER, rulerPoints));
  273.             }
  274.         }
  275.  
  276.         private function getSnapRotationOfTwoPoint(array:Array):Number
  277.         {
  278.             var rotation:Number=radiansToDegrees(getAngleOfTwoPoint(array[0], array[1]));
  279.             return rotation - 90;
  280.         }
  281.  
  282.         private function isThePointBetweenTwoPoint(testPoint:Point, point1:Point, point2:Point):Boolean
  283.         {
  284.             var slope:int=(point1.y - point2.y) / (point1.x - point2.x);
  285.             var int1:Number=testPoint.y - point2.y;
  286.             var int2:Number=slope * (testPoint.x - point2.x);
  287.  
  288.             if (int1 == int2)
  289.             {
  290.                 return true;
  291.             }
  292.  
  293.             return false;
  294.         }
  295.  
  296.         private function getAngleOfTwoPoint(point1:Point, point2:Point):Number
  297.         {
  298.             var dx:Number=point2.x - point1.x;
  299.             var dy:Number=point2.y - point1.y;
  300.             return Math.atan2(dy, dx);
  301.         }
  302.  
  303.         private function getCollisionWallCornerPoints():Array
  304.         {
  305.             var wallCornerPoints:Array=convertPointsToWall(pointsArray);
  306.             var wallIntersectPoints:Array=getObjectWallIntersectPoints(item.rotation, item, itemRect);
  307.             var result:Array=new Array();
  308.  
  309.             if (!wallIntersectPoints || wallIntersectPoints.length == 0)
  310.             {
  311.                 return null;
  312.             }
  313.  
  314.             try
  315.             {
  316.                 for (var i:int=0; i < wallCornerPoints.length; i++)
  317.                 {
  318.                     var point:Point=LineTools.rayRayIntersect(wallIntersectPoints[1][0], wallIntersectPoints[1][1], wallCornerPoints[i][0], wallCornerPoints[i][1]);
  319.  
  320.                     if (point)
  321.                     {
  322.                         result.push(wallCornerPoints[i][0]);
  323.                         result.push(wallCornerPoints[i][1]);
  324.                         return result;
  325.                     }
  326.  
  327.                 }
  328.             }
  329.             catch (error:Error)
  330.             {
  331.                 trace(error);
  332.             }
  333.  
  334.  
  335.  
  336.             return null;
  337.         }
  338.  
  339.         private function getCenterPointOfTwoPoint(point1:Point, point2:Point):Point
  340.         {
  341.             var x:int=point1.x + point2.x;
  342.             var y:int=point1.y + point2.y;
  343.             var centerPoint:Point=new Point(x / 2, y / 2);
  344.             return centerPoint;
  345.         }
  346.  
  347.         private function convertPointsToWall(array:Array):Array
  348.         {
  349.             var result:Array=new Array();
  350.             var points:Array=array;
  351.  
  352.             for (var i:int=0; i < points.length; i++)
  353.             {
  354.                 var wall:Array=new Array();
  355.  
  356.                 if (i == points.length - 1)
  357.                 {
  358.                     wall.push(points[i]);
  359.                     wall.push(points[0]);
  360.                 }
  361.                 else
  362.                 {
  363.                     wall.push(points[i]);
  364.                     wall.push(points[i + 1]);
  365.                 }
  366.                 result.push(wall);
  367.             }
  368.  
  369.             return result;
  370.  
  371.         }
  372.  
  373.         protected function mouseUpHandler(event:MouseEvent):void
  374.         {
  375.             canMove=false;
  376.         }
  377.  
  378.         protected function mouseDownHandler(event:MouseEvent):void
  379.         {
  380.             canMove=true;
  381.         }
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement