-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPHostSnapping.m
116 lines (103 loc) · 2.72 KB
/
TPHostSnapping.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* TPHostSnapping.c
* teleport
*
* Created by JuL on 11/05/05.
* Copyright 2005 abyssoft. All rights reserved.
*
*/
#include "TPHostSnapping.h"
float TPSquaredDistance(NSRect rect1, NSRect rect2) {
NSPoint point1 = rect1.origin;
NSPoint point2 = rect2.origin;
float dx = point1.x - point2.x;
float dy = point1.y - point2.y;
return dx*dx+dy*dy;
}
float TPGluedRect(NSRect * outGluedRect, TPSide * outSide, NSRect fixedRect, NSRect mobileRect, TPSide excludedSides)
{
int s = 0;
NSRect gluedRect[4];
TPSide side[4];
/* Right */
side[s] = TPRightSide;
gluedRect[s] = mobileRect;
gluedRect[s].origin.x = NSMaxX(fixedRect);
gluedRect[s].origin.y = MIN(NSMaxY(fixedRect), MAX(NSMinY(fixedRect) - NSHeight(mobileRect), gluedRect[s].origin.y));
/* Left */
side[++s] = TPLeftSide;
gluedRect[s] = mobileRect;
gluedRect[s].origin.x = NSMinX(fixedRect) - NSWidth(mobileRect);
gluedRect[s].origin.y = gluedRect[s-1].origin.y;
/* Top */
side[++s] = TPTopSide;
gluedRect[s] = mobileRect;
gluedRect[s].origin.y = NSMaxY(fixedRect);
gluedRect[s].origin.x = MIN(NSMaxX(fixedRect), MAX(NSMinX(fixedRect) - NSWidth(mobileRect), gluedRect[s].origin.x));
/* Bottom */
side[++s] = TPBottomSide;
gluedRect[s] = mobileRect;
gluedRect[s].origin.y = NSMinY(fixedRect) - NSHeight(mobileRect);
gluedRect[s].origin.x = gluedRect[s-1].origin.x;
NSRect minGluedRect = NSZeroRect;
float minDist = INFINITY;
TPSide minSide = TPLeftSide;
for(s=0; s<4; s++) {
if((side[s] & excludedSides) == 0) {
float dist = TPSquaredDistance(mobileRect, gluedRect[s]);
if(dist < minDist) {
minDist = dist;
minSide = side[s];
minGluedRect = gluedRect[s];
}
}
}
if(outGluedRect != NULL)
*outGluedRect = minGluedRect;
if(outSide != NULL) {
switch(minSide) {
case TPRightSide:
case TPLeftSide:
if(NSMinY(mobileRect) >= NSMaxY(fixedRect))
minSide |= TPTopSide;
else if(NSMaxY(mobileRect) <= NSMinY(fixedRect))
minSide |= TPBottomSide;
break;
case TPTopSide:
case TPBottomSide:
if(NSMinX(mobileRect) >= NSMaxX(fixedRect))
minSide |= TPRightSide;
else if(NSMaxX(mobileRect) <= NSMinX(fixedRect))
minSide |= TPLeftSide;
break;
default:
break;
}
*outSide = minSide;
}
if(NSIntersectsRect(fixedRect, mobileRect))
return -minDist;
else
return minDist;
}
NSRect TPRectShiftedOnSide(NSRect rect, TPSide side, float amount)
{
NSRect shiftedRect = rect;
switch(side) {
case TPRightSide:
shiftedRect.origin.x += amount;
break;
case TPLeftSide:
shiftedRect.origin.x -= amount;
break;
case TPTopSide:
shiftedRect.origin.y += amount;
break;
case TPBottomSide:
shiftedRect.origin.y -= amount;
break;
default:
break;
}
return shiftedRect;
}