17 "title" :
"VuoGraphicsView",
28 @property(retain) NSImage *circleImage;
46 if (
self = [super init])
48 self.wantsLayer =
true;
50 #pragma clang diagnostic push
51 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
53 self.acceptsTouchEvents = YES;
54 #pragma clang diagnostic pop
56 self.wantsRestingTouches = YES;
57 _touchTriggers = [NSMutableSet new];
58 _zoomedTriggers = [NSMutableSet new];
59 _swipedLeftTriggers = [NSMutableSet new];
60 _swipedRightTriggers = [NSMutableSet new];
64 _circleRect = NSMakeRect(0,0,48,48);
65 _circleImage = [[NSImage alloc] initWithSize:_circleRect.size];
66 [_circleImage lockFocus];
68 [[NSColor colorWithDeviceWhite:1 alpha:0.75] setFill];
69 [[NSColor colorWithDeviceWhite:0 alpha:0.15] setStroke];
70 NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:NSInsetRect(_circleRect, 1, 1)];
74 [_circleImage unlockFocus];
86 - (void)viewDidMoveToWindow
91 _viewport =
self.frame;
100 - (void)viewDidChangeBackingProperties
111 - (void)resetCursorRects
115 NSCursor *nsCursor = nil;
117 if (cursor == VuoCursor_None)
119 NSImage *im = [[NSImage alloc] initWithSize:NSMakeSize(1,1)];
120 nsCursor = [[[NSCursor alloc] initWithImage:im hotSpot:NSMakePoint(0,0)] autorelease];
123 else if (cursor == VuoCursor_Pointer)
124 nsCursor = [NSCursor arrowCursor];
125 else if (cursor == VuoCursor_Crosshair)
126 nsCursor = [NSCursor crosshairCursor];
127 else if (cursor == VuoCursor_HandOpen)
128 nsCursor = [NSCursor openHandCursor];
129 else if (cursor == VuoCursor_HandClosed)
130 nsCursor = [NSCursor closedHandCursor];
131 else if (cursor == VuoCursor_IBeam)
132 nsCursor = [NSCursor IBeamCursor];
133 else if (cursor == VuoCursor_Circle)
134 nsCursor = [[[NSCursor alloc] initWithImage:_circleImage hotSpot:NSMakePoint(NSMidX(_circleRect),NSMidY(_circleRect))] autorelease];
137 [
self addCursorRect:[
self visibleRect] cursor:nsCursor];
146 - (BOOL)acceptsFirstResponder
162 - (BOOL)mouseDownCanMoveWindow
172 [_circleImage release];
176 static NSInteger VuoGraphicsView_touchComparator(NSTouch *a, NSTouch *b,
void *p)
178 float ax = a.normalizedPosition.x;
179 float bx = b.normalizedPosition.x;
188 - (void)fireTouches:(NSEvent *)e
190 NSSet *touchesSet = [e touchesMatchingPhase:NSTouchPhaseAny inView:nil];
191 NSArray *orderedTouches = [touchesSet.allObjects sortedArrayUsingFunction:VuoGraphicsView_touchComparator context:nil];
196 for (NSTouch *t in orderedTouches)
197 touchPoints[i++] = (VuoPoint2d){
198 t.normalizedPosition.x * 2 - 1,
199 (t.normalizedPosition.y * 2 - 1) * (t.deviceSize.height / t.deviceSize.width)
202 dispatch_async(dispatch_get_main_queue(), ^{
203 for (NSValue *v in
self.touchTriggers)
207 touchesMoved(touches);
211 - (void)touchesBeganWithEvent:(NSEvent *)event
213 [
self fireTouches:event];
215 - (void)touchesMovedWithEvent:(NSEvent *)event
217 [
self fireTouches:event];
219 - (void)touchesEndedWithEvent:(NSEvent *)event
221 [
self fireTouches:event];
223 - (void)touchesCancelledWithEvent:(NSEvent *)event
225 [
self fireTouches:event];
228 - (void)magnifyWithEvent:(NSEvent *)event
230 if (event.magnification != 0)
231 dispatch_async(dispatch_get_main_queue(), ^{
232 for (NSValue *v in
self.zoomedTriggers)
234 void (*zoomed)(
VuoReal) = v.pointerValue;
236 zoomed(event.magnification);
241 - (void)swipeWithEvent:(NSEvent *)event
243 if (event.deltaX > 0)
244 dispatch_async(dispatch_get_main_queue(), ^{
245 for (NSValue *v in
self.swipedLeftTriggers)
247 void (*swipedLeft)(void) = v.pointerValue;
252 else if (event.deltaX < 0)
253 dispatch_async(dispatch_get_main_queue(), ^{
254 for (NSValue *v in
self.swipedRightTriggers)
256 void (*swipedRight)(void) = v.pointerValue;
270 zoomed:(
void (*)(
VuoReal))zoomed
271 swipedLeft:(
void (*)(
void))swipedLeft
272 swipedRight:(
void (*)(
void))swipedRight
274 dispatch_async(dispatch_get_main_queue(), ^{
275 [
self.touchTriggers addObject:[NSValue valueWithPointer:touchesMoved]];
276 [
self.zoomedTriggers addObject:[NSValue valueWithPointer:zoomed]];
277 [
self.swipedLeftTriggers addObject:[NSValue valueWithPointer:swipedLeft]];
278 [
self.swipedRightTriggers addObject:[NSValue valueWithPointer:swipedRight]];
289 zoomed:(
void (*)(
VuoReal))zoomed
290 swipedLeft:(
void (*)(
void))swipedLeft
291 swipedRight:(
void (*)(
void))swipedRight
293 dispatch_async(dispatch_get_main_queue(), ^{
294 for (NSValue *v in
self.touchTriggers)
295 if (v.pointerValue == touchesMoved)
296 [
self.touchTriggers removeObject:v];
297 for (NSValue *v in
self.zoomedTriggers)
298 if (v.pointerValue == zoomed)
299 [
self.zoomedTriggers removeObject:v];
300 for (NSValue *v in
self.swipedLeftTriggers)
301 if (v.pointerValue == swipedLeft)
302 [
self.swipedLeftTriggers removeObject:v];
303 for (NSValue *v in
self.swipedRightTriggers)
304 if (v.pointerValue == swipedRight)
305 [
self.swipedRightTriggers removeObject:v];