Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.view
- {
- import com.controller.LineEvent;
- import com.helpers.LineTools;
- import com.model.vo.DataVO;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flashx.textLayout.operations.RedoOperation;
- import org.robotlegs.mvcs.Mediator;
- public class FurnitureMediator extends Mediator
- {
- [Inject]
- public var furniture:FurnitureView;
- private var item:Sprite;
- private var canMove:Boolean = false;
- private var pointsArray:Array;
- private var itemRect:Rectangle;
- private var rotation:Number = 0;
- private var collisionWallPoints:Array;
- private var wallIntersectPoints:Array;
- private var outSideBounds:Array;
- override public function onRegister():void
- {
- item = furniture.item;
- pointsArray = DataVO.cornerPoints;
- itemRect = item.getBounds(furniture);
- item.x = 100;
- item.y = 100;
- //for rotating center
- itemRect.x = item.x - 25;
- itemRect.y = item.y - 50;
- eventMap.mapListener(item.stage, MouseEvent.MOUSE_UP, mouseUpHandler);
- eventMap.mapListener(item, MouseEvent.MOUSE_DOWN, mouseDownHandler);
- eventMap.mapListener(item, Event.ENTER_FRAME, render);
- }
- private function render(event:Event):void
- {
- if (canMove)
- {
- item.x = DataVO.mouseX;
- item.y = DataVO.mouseY;
- itemRect.x = item.x - 25;
- itemRect.y = item.y - 50;
- if (getCollisionWallCornerPoints())
- {
- rotation = getSnapRotationOfTwoPoint(getCollisionWallCornerPoints());
- item.rotation = rotation;
- }
- }
- }
- private function getOutsidePointOfPolygon(bounds:Array):Array
- {
- var points:Array = new Array();
- for (var i:int = 0; i < 4; i++)
- {
- var array:Array = new Array();
- var check:Boolean = LineTools.isPointInsidePolygon(bounds[i], pointsArray, convertPointsToWall(pointsArray).length);
- array.push(check);
- array.push(bounds[i]);
- points.push(array);
- }
- var result:Array = new Array();
- for (var j:int = 0; j < points.length; j++)
- {
- if (!points[j][0])
- {
- result.push(points[j][1]);
- }
- }
- if (result.length > 0)
- {
- return result;
- }
- return null;
- }
- private function pointInsidePolygon(point:Point):Boolean
- {
- var check:Boolean = false;
- check = LineTools.isPointInsidePolygon(point, pointsArray, convertPointsToWall(pointsArray).length);
- return check;
- }
- private function getObjectWallIntersectPoints(rotation:Number, object:Sprite, itemRect:Rectangle):Array
- {
- var result:Array = new Array();
- var wallCornerPoints:Array = convertPointsToWall(pointsArray);
- var rotatedItemBounds:Array = getRotatedBoundsOfObject(rotation, item, itemRect);
- for (var i:int = 0; i < wallCornerPoints.length; i++)
- {
- var wallPoints:Array = new Array();
- var boxIntersect:Array = LineTools.boxIntersect(wallCornerPoints[i][0], wallCornerPoints[i][1], rotatedItemBounds[0], rotatedItemBounds[1], rotatedItemBounds[2], rotatedItemBounds[3]);
- if(boxIntersect)
- {
- wallPoints.push(wallCornerPoints[i]);
- wallPoints.push(boxIntersect);
- result.push(wallPoints);
- }
- }
- //if two or more wall intersect
- if (result.length >= 2)
- {
- return null;
- }
- //if one wall intersect
- if (result.length == 1)
- {
- return result[0];
- }
- return new Array();
- }
- private function getRotatedBoundsOfObject(rotation:Number, object:Sprite, objectRectangle:Rectangle):Array
- {
- var rotationInRadians:Number = degreesToRadians(rotation);
- var itemCenterPoint:Point = new Point(object.x, object.y);
- var realBounds:Array = getBoundsOfObject(objectRectangle);
- var bounds:Array = new Array();
- var topLeft:Point = getRotatedRectPoint(rotationInRadians, realBounds[0], itemCenterPoint);
- var topRight:Point = getRotatedRectPoint(rotationInRadians, realBounds[1], itemCenterPoint);
- var bottomRight:Point = getRotatedRectPoint(rotationInRadians, realBounds[2], itemCenterPoint);
- var bottomLeft:Point = getRotatedRectPoint(rotationInRadians, realBounds[3], itemCenterPoint);
- bounds.push(topLeft);
- bounds.push(topRight);
- bounds.push(bottomRight);
- bounds.push(bottomLeft);
- return bounds;
- }
- private function getBoundsOfObject($objectRectangle:Rectangle):Array
- {
- var bounds:Array = new Array();
- var topLeft:Point = $objectRectangle.topLeft;
- var topRight:Point = new Point($objectRectangle.right, $objectRectangle.top);
- var bottomRight:Point = $objectRectangle.bottomRight;
- var botomLeft:Point = new Point($objectRectangle.x, $objectRectangle.bottom);
- bounds.push(topLeft);
- bounds.push(topRight);
- bounds.push(bottomRight);
- bounds.push(botomLeft);
- return bounds;
- }
- private function getRotatedRectPoint(angle:Number, point:Point, rotationPoint:Point = null):Point
- {
- var ix:Number = (rotationPoint) ? rotationPoint.x : 0;
- var iy:Number = (rotationPoint) ? rotationPoint.y : 0;
- var m:Matrix = new Matrix( 1,0,0,1, point.x - ix, point.y - iy);
- m.rotate(angle);
- return new Point( m.tx + ix, m.ty + iy);
- }
- private function radiansToDegrees(radians:Number):Number
- {
- var degrees:Number = radians * 180 / Math.PI;
- return degrees;
- }
- private function degreesToRadians(degrees:Number):Number
- {
- var radians:Number = degrees * Math.PI / 180
- return radians;
- }
- private function drawRuler():void
- {
- var wallCornerPoints:Array = getCollisionWallCornerPoints();
- var wallIntersectPoints:Array = getObjectWallIntersectPoints(rotation, item, itemRect);
- var rulerPoints:Array = new Array();
- if(!wallCornerPoints || !wallIntersectPoints) return;
- if(Point.distance(wallCornerPoints[0], wallIntersectPoints[0]) > Point.distance(wallCornerPoints[0], wallIntersectPoints[1]))
- {
- rulerPoints.push(wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[0], wallIntersectPoints[1], wallIntersectPoints[0], wallCornerPoints[1]);
- dispatch(new LineEvent(LineEvent.PLACE_RULER, rulerPoints));
- }
- else
- {
- rulerPoints.push(wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[0], wallIntersectPoints[0], wallIntersectPoints[1], wallCornerPoints[1]);
- dispatch(new LineEvent(LineEvent.PLACE_RULER, rulerPoints));
- }
- }
- private function getSnapRotationOfTwoPoint(array:Array):Number
- {
- var rotation:Number = radiansToDegrees(getAngleOfTwoPoint(array[0], array[1]));
- return rotation - 90;
- }
- private function isThePointBetweenTwoPoint(testPoint:Point, point1:Point, point2:Point):Boolean
- {
- var slope:int = (point1.y - point2.y) / (point1.x - point2.x);
- var int1:Number = testPoint.y - point2.y;
- var int2:Number = slope * (testPoint.x - point2.x);
- if (int1 == int2)
- {
- return true;
- }
- return false;
- }
- private function getAngleOfTwoPoint(point1:Point, point2:Point):Number
- {
- var dx:Number = point2.x - point1.x;
- var dy:Number = point2.y - point1.y;
- return Math.atan2(dy,dx);
- }
- private function getCollisionWallCornerPoints():Array
- {
- var wallCornerPoints:Array = convertPointsToWall(pointsArray);
- var wallIntersectPoints:Array = getObjectWallIntersectPoints(item.rotation, item, itemRect);
- var result:Array = new Array();
- if (!wallIntersectPoints || wallIntersectPoints.length == 0)
- {
- return null;
- }
- try
- {
- for (var i:int = 0; i < wallCornerPoints.length; i++)
- {
- var point:Point = LineTools.rayRayIntersect(wallIntersectPoints[1][0], wallIntersectPoints[1][1], wallCornerPoints[i][0], wallCornerPoints[i][1]);
- if (point)
- {
- result.push(wallCornerPoints[i][0]);
- result.push(wallCornerPoints[i][1]);
- return result;
- }
- }
- }
- catch(error:Error)
- {
- trace(error);
- }
- return null;
- }
- private function getCenterPointOfTwoPoint(point1:Point, point2:Point):Point
- {
- var x:int = point1.x + point2.x;
- var y:int = point1.y + point2.y;
- var centerPoint:Point = new Point(x/2, y/2);
- return centerPoint;
- }
- private function convertPointsToWall(array:Array):Array
- {
- var result:Array = new Array();
- var points:Array = array;
- for (var i:int = 0; i < points.length; i++)
- {
- var wall:Array = new Array();
- if (i == points.length - 1)
- {
- wall.push(points[i]);
- wall.push(points[0]);
- }
- else
- {
- wall.push(points[i]);
- wall.push(points[i+1]);
- }
- result.push(wall);
- }
- return result;
- }
- protected function mouseUpHandler(event:MouseEvent):void
- {
- canMove = false;
- }
- protected function mouseDownHandler(event:MouseEvent):void
- {
- canMove = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement