-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYEDEditorView.j
221 lines (177 loc) · 6.42 KB
/
YEDEditorView.j
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
@import <AppKit/CPView.j>
@import <Foundation/CPNotificationCenter.j>
@import "CPView+OffsetCorners.j"
@import "YEDNodeView.j"
var SharedEditorView = nil,
HandleSize = 10.0,
HandleSlop = 20.0,
Margin = 15.0,
Padding = 5.0;
var topLeftHandle = CGRectMakeZero(),
topRightHandle = CGRectMakeZero(),
bottomRightHandle = CGRectMakeZero(),
bottomLeftHandle = CGRectMakeZero();
var sloppyHandle = function(rect)
{
return CGRectInset(rect, -HandleSlop, -HandleSlop);
};
@implementation YEDEditorView : CPView
{
YEDNodeView nodeView @accessors;
BOOL isResizing;
CGPoint resizeLocation;
}
+ (id)sharedEditor
{
if(!SharedEditorView)
SharedEditorView = [[YEDEditorView alloc] initWithFrame:CPRectMakeZero()];
return SharedEditorView;
}
- (id)initWithFrame:(CPRect)aFrame
{
self = [super initWithFrame:aFrame];
if(self)
{
}
return self;
}
- (void)setNodeView:(YEDNodeView)aNodeView
{
if(nodeView === aNodeView)
return;
CPLog.trace("YEDEditorView: setNodeView");
var center = [CPNotificationCenter defaultCenter];
if(nodeView)
{
[center removeObserver:self
name:CPViewFrameDidChangeNotification
object:nodeView];
}
[self willChangeValueForKey:@"nodeView"];
nodeView = aNodeView;
[self didChangeValueForKey:@"nodeView"];
if(nodeView)
{
[center addObserver:self
selector:@selector(nodeViewFrameChanged:)
name:CPViewFrameDidChangeNotification
object:nodeView];
[self nodeViewFrameChanged:nil];
[[nodeView superview] addSubview:self];
[[nodeView superview] addSubview:nodeView];
}
else
{
[self removeFromSuperview];
}
}
- (void)nodeViewFrameChanged:(CPNotification)notification
{
var bounds = [nodeView frame];
// [self setFrame:CGRectMake(bounds.origin.x-FrameOutset, bounds.origin.y-FrameOutset, bounds.size.width+(FrameOutset*2), bounds.size.height+(FrameOutset*2))];
[self setFrame:CGRectInset(bounds, -(Margin+Padding), -(Margin+Padding))];
}
- (void)mouseDown:(CPEvent)event
{
var location = [self convertPoint:[event locationInWindow] fromView:nil];
// CPLog.trace("YEDEditorView: mouseDown");
if(CGRectContainsPoint(sloppyHandle(topLeftHandle), location))
{
// CPLog.trace("YEDEditorView: topLeftHandle");
isResizing = YES;
resizeLocation = [event locationInWindow];
}
else if(CGRectContainsPoint(sloppyHandle(topRightHandle), location))
{
// CPLog.trace("YEDEditorView: topRightHandle");
isResizing = YES;
resizeLocation = [event locationInWindow];
}
else if(CGRectContainsPoint(sloppyHandle(bottomRightHandle), location))
{
// CPLog.trace("YEDEditorView: bottomRightHandle");
isResizing = YES;
resizeLocation = [event locationInWindow];
}
else if(CGRectContainsPoint(sloppyHandle(bottomLeftHandle), location))
{
// CPLog.trace("YEDEditorView: bottomLeftHandle");
isResizing = YES;
resizeLocation = [event locationInWindow];
}
else
{
[super mouseDown:event];
}
}
- (void)mouseDragged:(CPEvent)event
{
if(!isResizing)
return;
var handleLocation = [self convertPoint:[event locationInWindow] fromView:nil]
var location = [event locationInWindow],
dX = location.x - resizeLocation.x,
dY = location.y - resizeLocation.y;
// CPLog.trace("YEDEditorView: mouseDragged");
// CPLog.trace("dX = " + dX);
// CPLog.trace("dY = " + dY);
if(CGRectContainsPoint(sloppyHandle(topLeftHandle), handleLocation))
{
[nodeView offsetFrameTopLeft:CGPointMake(dX,dY)];
}
else if(CGRectContainsPoint(sloppyHandle(topRightHandle), handleLocation))
{
[nodeView offsetFrameTopRight:CGPointMake(dX,dY)];
}
else if(CGRectContainsPoint(sloppyHandle(bottomRightHandle), handleLocation))
{
[nodeView offsetFrameBottomRight:CGPointMake(dX,dY)];
}
else if(CGRectContainsPoint(sloppyHandle(bottomLeftHandle), handleLocation))
{
[nodeView offsetFrameBottomLeft:CGPointMake(dX,dY)];
}
[self setNeedsDisplay:YES];
resizeLocation = location;
}
- (void)mouseUp:(CPEvent)event
{
if(!isResizing)
return;
isResizing = NO;
}
- (void)drawRect:(CGRect)rect
{
var bounds = CGRectInset([self bounds], Margin, Margin),
context = [[CPGraphicsContext currentContext] graphicsPort];
CGContextSetStrokeColor(context, [CPColor grayColor]);
CGContextSetLineWidth(context, 1.0);
CGContextStrokeRect(context, bounds);
[self drawHandles:CGRectInset(bounds,-(HandleSize/2),-(HandleSize/2))];
}
- (void)drawHandles:(CGRect)rect
{
var topLeftPoint = CGPointMake(rect.origin.x, rect.origin.y),
topRigthPoint = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y),
bottomRightPoint = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height),
bottomLeftPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
var totalHandleSize = HandleSize + (HandleSlop*2);
topLeftHandle = CGRectMake(topLeftPoint.x, topLeftPoint.y, HandleSize, HandleSize);
topRightHandle = CGRectMake(topRigthPoint.x - HandleSize, topRigthPoint.y, HandleSize, HandleSize);
bottomRightHandle = CGRectMake(bottomRightPoint.x - HandleSize, bottomRightPoint.y - HandleSize, HandleSize, HandleSize);
bottomLeftHandle = CGRectMake(bottomLeftPoint.x, bottomLeftPoint.y - HandleSize, HandleSize, HandleSize);
var context = [[CPGraphicsContext currentContext] graphicsPort];
function drawHandle(rect)
{
// var newRect = CGRectMake(rect.origin.x,rect.origin.y, rect.size.width - HandleSlop*2, rect.size.height - HandleSlop*2);
// CGContextFillEllipseInRect(context, rect);
CGContextFillRect(context, rect);
// CGContextFillEllipseInRect(context, rect);
}
CGContextSetFillColor(context, [CPColor blackColor]);
drawHandle(topLeftHandle);
drawHandle(topRightHandle);
drawHandle(bottomRightHandle);
drawHandle(bottomLeftHandle);
}
@end