Advertisement
Guest User

Untitled

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