17 "title" :
"VuoGraphicsView",
28 @property(retain) NSImage *circleImage;
46 if (
self = [super init])
48 self.wantsLayer =
true;
50 self.acceptsTouchEvents = YES;
51 self.wantsRestingTouches = YES;
52 _touchTriggers = [NSMutableSet new];
53 _zoomedTriggers = [NSMutableSet new];
54 _swipedLeftTriggers = [NSMutableSet new];
55 _swipedRightTriggers = [NSMutableSet new];
59 _circleRect = NSMakeRect(0,0,48,48);
60 _circleImage = [[NSImage alloc] initWithSize:_circleRect.size];
61 [_circleImage lockFocus];
63 [[NSColor colorWithDeviceWhite:1 alpha:0.75] setFill];
64 [[NSColor colorWithDeviceWhite:0 alpha:0.15] setStroke];
65 NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:NSInsetRect(_circleRect, 1, 1)];
69 [_circleImage unlockFocus];
81 - (void)viewDidMoveToWindow
86 _viewport =
self.frame;
95 - (void)viewDidChangeBackingProperties
106 - (void)resetCursorRects
110 NSCursor *nsCursor = nil;
112 if (cursor == VuoCursor_None)
114 NSImage *im = [[NSImage alloc] initWithSize:NSMakeSize(1,1)];
115 nsCursor = [[[NSCursor alloc] initWithImage:im hotSpot:NSMakePoint(0,0)] autorelease];
118 else if (cursor == VuoCursor_Pointer)
119 nsCursor = [NSCursor arrowCursor];
120 else if (cursor == VuoCursor_Crosshair)
121 nsCursor = [NSCursor crosshairCursor];
122 else if (cursor == VuoCursor_HandOpen)
123 nsCursor = [NSCursor openHandCursor];
124 else if (cursor == VuoCursor_HandClosed)
125 nsCursor = [NSCursor closedHandCursor];
126 else if (cursor == VuoCursor_IBeam)
127 nsCursor = [NSCursor IBeamCursor];
128 else if (cursor == VuoCursor_Circle)
129 nsCursor = [[[NSCursor alloc] initWithImage:_circleImage hotSpot:NSMakePoint(NSMidX(_circleRect),NSMidY(_circleRect))] autorelease];
132 [
self addCursorRect:[
self visibleRect] cursor:nsCursor];
141 - (BOOL)acceptsFirstResponder
157 - (BOOL)mouseDownCanMoveWindow
167 [_circleImage release];
171 static NSInteger VuoGraphicsView_touchComparator(NSTouch *a, NSTouch *b,
void *p)
173 float ax = a.normalizedPosition.x;
174 float bx = b.normalizedPosition.x;
183 - (void)fireTouches:(NSEvent *)e
185 NSSet *touchesSet = [e touchesMatchingPhase:NSTouchPhaseAny inView:nil];
186 NSArray *orderedTouches = [touchesSet.allObjects sortedArrayUsingFunction:VuoGraphicsView_touchComparator context:nil];
191 for (NSTouch *t in orderedTouches)
192 touchPoints[i++] = (VuoPoint2d){
193 t.normalizedPosition.x * 2 - 1,
194 (t.normalizedPosition.y * 2 - 1) * (t.deviceSize.height / t.deviceSize.width)
197 dispatch_async(dispatch_get_main_queue(), ^{
198 for (NSValue *v in
self.touchTriggers)
202 touchesMoved(touches);
206 - (void)touchesBeganWithEvent:(NSEvent *)event
208 [
self fireTouches:event];
210 - (void)touchesMovedWithEvent:(NSEvent *)event
212 [
self fireTouches:event];
214 - (void)touchesEndedWithEvent:(NSEvent *)event
216 [
self fireTouches:event];
218 - (void)touchesCancelledWithEvent:(NSEvent *)event
220 [
self fireTouches:event];
223 - (void)magnifyWithEvent:(NSEvent *)event
225 if (event.magnification != 0)
226 dispatch_async(dispatch_get_main_queue(), ^{
227 for (NSValue *v in
self.zoomedTriggers)
229 void (*zoomed)(
VuoReal) = v.pointerValue;
231 zoomed(event.magnification);
236 - (void)swipeWithEvent:(NSEvent *)event
238 if (event.deltaX > 0)
239 dispatch_async(dispatch_get_main_queue(), ^{
240 for (NSValue *v in
self.swipedLeftTriggers)
242 void (*swipedLeft)(void) = v.pointerValue;
247 else if (event.deltaX < 0)
248 dispatch_async(dispatch_get_main_queue(), ^{
249 for (NSValue *v in
self.swipedRightTriggers)
251 void (*swipedRight)(void) = v.pointerValue;
265 zoomed:(
void (*)(
VuoReal))zoomed
266 swipedLeft:(
void (*)(
void))swipedLeft
267 swipedRight:(
void (*)(
void))swipedRight
269 dispatch_async(dispatch_get_main_queue(), ^{
270 [
self.touchTriggers addObject:[NSValue valueWithPointer:touchesMoved]];
271 [
self.zoomedTriggers addObject:[NSValue valueWithPointer:zoomed]];
272 [
self.swipedLeftTriggers addObject:[NSValue valueWithPointer:swipedLeft]];
273 [
self.swipedRightTriggers addObject:[NSValue valueWithPointer:swipedRight]];
284 zoomed:(
void (*)(
VuoReal))zoomed
285 swipedLeft:(
void (*)(
void))swipedLeft
286 swipedRight:(
void (*)(
void))swipedRight
288 dispatch_async(dispatch_get_main_queue(), ^{
289 for (NSValue *v in
self.touchTriggers)
290 if (v.pointerValue == touchesMoved)
291 [
self.touchTriggers removeObject:v];
292 for (NSValue *v in
self.zoomedTriggers)
293 if (v.pointerValue == zoomed)
294 [
self.zoomedTriggers removeObject:v];
295 for (NSValue *v in
self.swipedLeftTriggers)
296 if (v.pointerValue == swipedLeft)
297 [
self.swipedLeftTriggers removeObject:v];
298 for (NSValue *v in
self.swipedRightTriggers)
299 if (v.pointerValue == swipedRight)
300 [
self.swipedRightTriggers removeObject:v];