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