-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYEDEdgeView.j
More file actions
395 lines (319 loc) · 11.1 KB
/
Copy pathYEDEdgeView.j
File metadata and controls
395 lines (319 loc) · 11.1 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
@import <AppKit/CPBezierPath.j>
@import <AppKit/CPView.j>
@import <Foundation/CPNotificationCenter.j>
@import "YEDNodeView.j"
var intersectLineLine = function(a1, a2, b1, b2)
{
var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
var u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if ( u_b != 0 ) {
var ua = ua_t / u_b;
var ub = ub_t / u_b;
if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
result = {type:"intersection", points:[]};
result.points.push(
{
x: a1.x + ua * (a2.x - a1.x),
y: a1.y + ua * (a2.y - a1.y)
}
);
} else {
result = {type:"none", points:[]};
}
} else {
if ( ua_t == 0 || ub_t == 0 ) {
result = {type:"coincident", points:[]};
} else {
result = {type:"parallel", points:[]};
}
}
return result;
};
var intersectLineRect = function(a1, a2, rect) {
var min = rect.origin;
var max = {x:rect.origin.x+rect.size.width, y:rect.origin.y+rect.size.height};
var topRight = {x:max.x, y:min.y };
var bottomLeft = {x:min.x, y:max.y };
var inter1 = intersectLineLine(min, topRight, a1, a2);
var inter2 = intersectLineLine(topRight, max, a1, a2);
var inter3 = intersectLineLine(max, bottomLeft, a1, a2);
var inter4 = intersectLineLine(bottomLeft, min, a1, a2);
var result = {type:"none", points:[]};
result.points = result.points.concat(inter1.points);
result.points = result.points.concat(inter2.points);
result.points = result.points.concat(inter3.points);
result.points = result.points.concat(inter4.points);
if ( result.points.length > 0 )
result.type = "intersection";
return result;
};
var Padding = 20;
@implementation YEDEdgeView : CPView
{
YEDNodeView startNodeView @accessors;
YEDNodeView endNodeView @accessors;
CPColor strokeColor @accessors;
BOOL isSelected @accessors;
}
+ (id)edgeFromView:(YEDNodeView)start toView:(YEDNodeView)end
{
return [[self alloc] initEdgeFromView:start toView:end];
}
- (id)initEdgeFromView:(YEDNodeView)start toView:(YEDNodeView)end
{
self = [self init];
if(self)
{
[self setStartNodeView:start];
[self setEndNodeView:end];
[self setStrokeColor:[CPColor blackColor]];
}
return self;
}
- (BOOL)isEqual:(id)other
{
if(other === self)
return YES;
if(!other || ![other isKindOfClass:[self class]])
return NO
return [self isEqualToEdgeView:other];
}
- (BOOL)isEqualToEdgeView:(YEDEdgeView)otherView
{
if(otherView === self)
return YES;
if(![[self startNodeView] isEqual:[otherView startNodeView]])
return NO;
if(![[self endNodeView] isEqual:[otherView endNodeView]])
return NO;
return YES;
}
- (void)setStartNodeView:(YEDNodeView)start
{
if(startNodeView === start)
return;
var center = [CPNotificationCenter defaultCenter];
if(startNodeView)
{
[center removeObserver:self
name:CPViewFrameDidChangeNotification
object:startNodeView];
}
[self willChangeValueForKey:@"startNodeView"];
startNodeView = start;
[self didChangeValueForKey:@"startNodeView"];
if(startNodeView)
{
[center addObserver:self
selector:@selector(nodeViewFrameChanged:)
name:CPViewFrameDidChangeNotification
object:startNodeView];
[self nodeViewFrameChanged:nil];
// [[startNodeView superview] addSubview:self];
// [[startNodeView superview] addSubview:startNodeView];
// if(endNodeView)
// [[endNodeView superview] addSubview:endNodeView];
}
else
{
// [self removeFromSuperview];
}
}
- (void)setEndNodeView:(YEDNodeView)endView
{
if(endNodeView === endView)
return;
var center = [CPNotificationCenter defaultCenter];
if(endNodeView)
{
[center removeObserver:self
name:CPViewFrameDidChangeNotification
object:endNodeView];
}
[self willChangeValueForKey:@"endNodeView"];
endNodeView = endView
[self didChangeValueForKey:@"endNodeView"];
if(endNodeView)
{
[center addObserver:self
selector:@selector(nodeViewFrameChanged:)
name:CPViewFrameDidChangeNotification
object:endNodeView];
[self nodeViewFrameChanged:nil];
// [[endNodeView superview] addSubview:self];
// [[endNodeView superview] addSubview:endNodeView];
// if(startNodeView)
// [[startNodeView superview] addSubview:startNodeView];
}
else
{
// [self removeFromSuperview];
}
}
- (void)mouseDown:(CPEvent)event
{
var location = [self convertPoint:[event locationInWindow] fromView:nil];
var onEdge = [self containsPoint:location];
if(onEdge)
{
CPLog.trace("Selecting Edge");
[[CPNotificationCenter defaultCenter]
postNotificationName:"YEDSelectedItemNotification"
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
"mouseDown":event
}]];
}
else
[super mouseDown:event];
}
- (id)hitTest:(CGPoint)point
{
if([self containsPoint:[self convertPoint:point fromView:[self superview]]])
return self;
else
return nil;
}
- (BOOL)containsPoint:(CGPoint)point
{
var A = [self convertPoint:[startNodeView center] fromView:[self superview]],
B = [self convertPoint:[endNodeView center] fromView:[self superview]];
var slope = (B.y - A.y)/(B.x - A.x);
var x = point.x,
y = point.y;
var Y = slope*(x - A.x) + A.y;
var X = (y - A.y)/slope + A.x
return ((X <= MAX(A.x,B.x)) && (X >= MIN(A.x,B.x)) && (Y <= MAX(A.y, B.y)) && (Y >= MIN(A.y, B.y))) &&
(((Y-10 <= y) && (y <= Y+10)) || ((X-10 <= x) && (x <= X+10)));
}
- (void)nodeViewFrameChanged:(CPNotification)notification
{
if(!startNodeView || !endNodeView)
return;
// CPLog.trace("YEDEdgeView: startNodeViewFrameChanged:");
var startCenter = [startNodeView center],
endOrigin = [endNodeView center],
delta = CGPointMake(endOrigin.x - startCenter.x, endOrigin.y - startCenter.y),
frame = CGRectMakeZero();
var minXPoint = startCenter.x <= endOrigin.x ? startCenter.x : endOrigin.x,
minYPoint = startCenter.y <= endOrigin.y ? startCenter.y : endOrigin.y,
maxXPoint = startCenter.x >= endOrigin.x ? startCenter.x : endOrigin.x,
maxYPoint = startCenter.y >= endOrigin.y ? startCenter.y : endOrigin.y;
var width = Math.abs(maxXPoint-minXPoint),
height = Math.abs(maxYPoint-minYPoint);
// width = width < 20 ? 20 : width;
// height = height < 20 ? 20 : height;
// CPLog.trace("w = %s, h = %s", width, height);
frame = CGRectMake(minXPoint,
minYPoint,
width,
height);
[self setFrame:CGRectInset(frame, -Padding, -Padding)];
[self setNeedsDisplay:YES];
}
- (void)setIsSelected:(BOOL)selected
{
if(isSelected === selected)
return;
[self willChangeValueForKey:@"isSelected"];
isSelected = selected;
[self didChangeValueForKey:@"isSelected"];
if(isSelected)
{
[self setStrokeColor:[CPColor redColor]];
[self setNeedsDisplay:YES];
}
else
{
[self setStrokeColor:[CPColor blackColor]];
[self setNeedsDisplay:YES];
}
}
- (void)drawRect:(CGRect)rect
{
// CPLog.trace("YEDEdgeView drawRect:");
if(!startNodeView || !endNodeView)
return;
var rect = CPRectInset(rect, Padding, Padding);
// CPLog.trace("YEDEdgeView: drawing edge");
var startPoint = [self convertPoint:[startNodeView center] fromView:[self superview]],
endPoint = [self convertPoint:[endNodeView center] fromView:[self superview]],
context = [[CPGraphicsContext currentContext] graphicsPort];
var intersection = intersectLineRect([startNodeView center], [endNodeView center], [endNodeView frame]);
if(intersection.type === "intersection")
{
var pt = intersection.points[0];
var point = [self convertPoint:CGPointMake(pt.x,pt.y) fromView:[self superview]];
var arrowLength = 10;
CGContextSetStrokeColor(context, [self strokeColor]);
CGContextSetFillColor(context, [self strokeColor]);
CGContextSetLineWidth(context, 3.0);
var path = [CPBezierPath bezierPath];
[path setLineWidth:2.0];
[path moveToPoint:startPoint];
[path lineToPoint:point];
[path stroke];
// Draw arrow end point
var aTan2FromOrigin = function(x, y, oX, oY)
{
oX = oX || 0;
oY = oY || 0;
var dx = x - oX,
dy = y - oY;
var angle = 0;
if(dx > 0)
{
angle = Math.atan(dy/dx);
}
else if(dy >= 0 && dx < 0)
{
angle = Math.PI + Math.atan(dy/dx);
}
else if(dy < 0 && dx < 0)
{
angle = -Math.PI + Math.atan(dy/dx);
}
else if(dy > 0 && dx == 0)
{
angle = Math.PI/2;
}
else if(dy < 0 && dx == 0)
{
angle = -Math.PI/2;
}
else if(dy == 0 && dx ==0)
{
angle = 0;
}
return angle;
}
with(Math)
{
var x1 = startPoint.x,
y1 = startPoint.y,
x2 = point.x,
y2 = point.y;
var lineAngle = aTan2FromOrigin(x2,y2,x1,y1),
arrowAngle1 = lineAngle + 45*PI/180,
arrowAngle2 = lineAngle - 45*PI/180;
var x3 = x2 - arrowLength * cos(arrowAngle1),
y3 = y2 - arrowLength * sin(arrowAngle1),
x4 = x2 - arrowLength * cos(arrowAngle2),
y4 = y2 - arrowLength * sin(arrowAngle2);
var arrowPath1 = [CPBezierPath bezierPath];
[arrowPath1 setLineWidth:2.0];
[arrowPath1 moveToPoint:CGPointMake(x2,y2)];
[arrowPath1 lineToPoint:CGPointMake(x3,y3)];
[arrowPath1 lineToPoint:CGPointMake(x4,y4)];
[arrowPath1 closePath];
[arrowPath1 fill];
// var arrowPath2 = [CPBezierPath bezierPath];
// [arrowPath2 setLineWidth:2.0];
// [arrowPath2 moveToPoint:CGPointMake(x2,y2)];
// [arrowPath2 lineToPoint:CGPointMake(x4,y4)];
// [arrowPath2 fill];
}
}
}
@end