This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Ring.m
815 lines (669 loc) · 23.3 KB
/
Ring.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//
// Ring.m
//
// Copyright 2009 SwiftRing. All rights reserved.
//
#import "Ring.h"
#import "RingFactory.h"
#import "Debug.h"
#define RING_INNER_RADIUS 50
#define RING_OUTER_RADIUS 60
#define RING_LABEL_RADIUS 70
#define SEGMENT_GAP 3
#define RING_VELOCITY_RADIUS 2
#define LABEL_HEIGHT_CORRECTION 0.90
#define LABEL_BOX_BORDER_X 6
#define LABEL_BOX_BORDER_Y 2
#define LABEL_BOX_ROUND 7
#define INVALID_KEY -1
typedef enum _PARSE_STATES
{
PARSE_RING = 0,
PARSE_SCROLLUP,
PARSE_SCROLLDOWN,
PARSE_DONE,
} PARSE_STATES;
#define RADIANS(degrees) ((degrees) * M_PI / 180)
#define DEGREES(radians) ((radians) * 180 / M_PI)
@implementation Ring
// This function can recurse, and is used internally only
- (void) initRing: (NSDictionary *) pRingInfo: (int *) index: (float) startAngle: (NSArray *) pAttribKeys:
(NSArray *) pAttribObjects: (bool) isSubring;
{
int parseState = PARSE_RING;
int ring = *index;
int segment = 0;
int ringSegments = 0;
int totalSegments = 0;
int key = 0;
float angle;
float spanAngle;
NSDictionary *pSegmentInfo;
NSDictionary *pSubringInfo;
NSString *pKeyCode;
// Normalize the start angle
startAngle = (startAngle > 0) ? startAngle : 360 + startAngle;
angle = startAngle;
//DebugLog(@"initRing: index = %i", *index);
//DebugLog(@"initRing: startAngle = %f", startAngle);
totalSegments = [[pRingInfo objectForKey: @"TotalSegments"] intValue];
ringSegments = [[pRingInfo objectForKey: @"RingSegments"] intValue];
spanAngle = 360 / ([[pRingInfo objectForKey: @"RingSegments"] intValue] + 0);
ringContainer[ring].numSegments = totalSegments;
ringContainer[ring].pLabelAttributes = [[NSDictionary alloc] initWithObjects: pAttribObjects forKeys: pAttribKeys];
ringContainer[ring].isSubring = isSubring;
// Set up all the label and action info for each segment (MAX_SEGMENTS check is a fail-safe)
while (TRUE && (segment < MAX_SEGMENTS))
{
// Parser state machine
if (PARSE_RING == parseState)
{
pSegmentInfo = [pRingInfo objectForKey: [NSString stringWithFormat:@"%i", segment /*- (isSubring ? 1: 0)*/]];
if ((segment >= ringSegments) || (nil == pSegmentInfo))
{
// Move to the next state immediately, skip processing
parseState++;
//DebugLog(@"Moved to PARSE_SCROLLUP %i", parseState);
continue;
}
}
else if (PARSE_SCROLLUP == parseState)
{
pSegmentInfo = [pRingInfo objectForKey: @"ScrollUp"];
// Move to the next state after processing
parseState++;
//DebugLog(@"Moved to PARSE_SCROLLDOWN %i", parseState);
if (nil == pSegmentInfo)
{
// Move to the next state immediately, skip processing
ringContainer[ring].scrollUpSegment = -1;
continue;
}
ringContainer[ring].scrollUpSegment = segment;
}
else if (PARSE_SCROLLDOWN == parseState)
{
pSegmentInfo = [pRingInfo objectForKey: @"ScrollDown"];
// Move to the next state after processing
parseState++;
//DebugLog(@"Moved to PARSE_DONE %i", parseState);
if (nil == pSegmentInfo)
{
// Move to the next state immediately, skip processing
ringContainer[ring].scrollDownSegment = -1;
continue;
}
ringContainer[ring].scrollDownSegment = segment;
}
else
{
break;
}
//DebugLog(@"Ring initializing segment: %i", segment);
ringContainer[ring].label[segment].pLabel = [[NSString alloc] initWithString:[pSegmentInfo objectForKey:@"Label"]];
//
// Construct the keyboard actions. Commas seperate keys that should be held down at the same time, spaces
// seperate the ordering of keys pressed at the same time.
//
key = 0;
// Keycodes are seperated by spaces
//DebugLog(@"Keys: '%@'", [pSegmentInfo objectForKey: @"Keys"]);
NSEnumerator *pKeyCodeEnum = [[[pSegmentInfo objectForKey: @"Keys"] componentsSeparatedByString:@" "] objectEnumerator];
// Loop through all the keycodes
while ((pKeyCode = [pKeyCodeEnum nextObject]) && (NSOrderedSame != [pKeyCode compare: @""]))
{
// Keycodes and up/down indication are seperated by underscores
NSArray *pKeyUpDownArray = [pKeyCode componentsSeparatedByString:@"_"];
ringContainer[ring].action[segment].keyCode[key] = [[pKeyUpDownArray objectAtIndex: 0] intValue];
ringContainer[ring].action[segment].keyIsDown[key] = [[pKeyUpDownArray objectAtIndex: 1] boolValue];
//DebugLog(@"Code: %i Down: %i key: %i",
// ringContainer[ring].action[segment].keyCode[key], ringContainer[ring].action[segment].keyIsDown[key], key);
key++;
}
// Mark the key watermark
if (key < MAX_KEYSTROKES)
{
ringContainer[ring].action[segment].keyCode[key] = INVALID_KEY;
}
pSubringInfo = [pSegmentInfo objectForKey:@"Submenu"];
// If there is a sub-ring, call this function recursively w/ a new index
if (nil != pSubringInfo)
{
*index = *index + 1;
if (*index < MAX_SEGMENTS)
{
ringContainer[ring].action[segment].pNextRing = &ringContainer[*index];
[self initRing: pSubringInfo: index: 90/*angle*/: pAttribKeys: pAttribObjects: YES];
}
else
{
DebugLog(@"initRing: Out of segments!");
}
}
segment++;
// Keep advancing the angle counter clockwise - needed to track the entry angle for the subring
if (PARSE_RING == parseState)
{
angle -= spanAngle;
}
}
// Generate the ring graphics and labels
[self generatePaths: &ringContainer[ring] :ringContainer[ring].numSegments: startAngle: spanAngle: ringCenterX: ringCenterY: isSubring];
}
- (id) init: (NSDictionary *) pRingInfo: (float) centerX: (float) centerY
{
DebugLog(@"init start");
int index;
NSFont *pFont = [NSFont systemFontOfSize:15];
//NSFont *pFont = [NSFont fontWithName:@"Verdana" size:15];
NSColor *pFgColor = [NSColor colorWithDeviceRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0];
NSArray *pAttribKeys = [NSArray arrayWithObjects:NSFontAttributeName,
NSForegroundColorAttributeName,
nil];
NSArray *pAttribObjects = [NSArray arrayWithObjects:pFont,
pFgColor,
nil];
if (nil == pRingInfo)
{
return self;
}
ringCenterX = centerX;
ringCenterY = centerY;
index = 0;
// Generate all ring text and paths
[self initRing: pRingInfo: &index: 90: pAttribKeys: pAttribObjects: NO];
// Set the current ring to the main ring for now
pCurrentRing = &ringContainer[0];
DebugLog(@"init done");
return self;
}
- (void)dealloc
{
int ringIndex, segIndex;
//
// Release all the items we allocated
// (only methods that have 'alloc', 'new', or 'copy' must be freed)
//
for (ringIndex = 0; ringIndex < MAX_RINGS; ringIndex++)
{
[ringContainer[ringIndex].pLabelAttributes release];
ringContainer[ringIndex].pLabelAttributes = NULL;
for (segIndex = 0; segIndex < ringContainer[ringIndex].numSegments; segIndex++)
{
[ringContainer[ringIndex].path[segIndex].pPath release];
ringContainer[ringIndex].path[segIndex].pPath = NULL;
[ringContainer[ringIndex].label[segIndex].pLabel release];
ringContainer[ringIndex].label[segIndex].pLabel = NULL;
[ringContainer[ringIndex].label[segIndex].pLabelBox release];
ringContainer[ringIndex].label[segIndex].pLabelBox = NULL;
}
}
[super dealloc];
}
- (int) getNumSegments
{
return pCurrentRing->numSegments;
}
- (NSDictionary *) getLabelAttributes
{
return pCurrentRing->pLabelAttributes;
}
- (NSBezierPath *) getSegmentPath: (int) segment
{
return pCurrentRing->path[segment].pPath;
}
- (LabelElements) getSegmentLabel: (int) segment
{
return pCurrentRing->label[segment];
}
- (int) detectHit:(NSPoint) location: (float) centerX: (float) centerY: (bool) scrollUp: (bool) scrollDown: (unsigned int) arrowBitfield;
{
int i;
int status = -1;
location.x -= centerX;
location.y -= centerY;
if (arrowBitfield)
{
status = [Ring arrowSegmentHitTest: arrowBitfield: pCurrentRing->numSegments];
}
else if (scrollUp && (pCurrentRing->scrollUpSegment != 0))
{
status = pCurrentRing->scrollUpSegment;
}
else if (scrollDown && (pCurrentRing->scrollDownSegment != 0))
{
status = pCurrentRing->scrollDownSegment;
}
/*
else if (pCurrentRing->isSubring && (sqrt((location.x * location.x) + (location.y * location.y)) > RING_VELOCITY_RADIUS))
{
//
// Subring velocity buffer
//
if ([Ring isPointInAngle: location: pCurrentRing->path[0]])
{
status = 0;
}
}
*/
// See if the point is within the ring's radius
else if (sqrt((location.x * location.x) + (location.y * location.y)) < RING_OUTER_RADIUS)
{
// -1 represents no legal segment
status = -1;
}
else
{
// Ok, now see which segment it passed through
for (i = 0; (i < pCurrentRing->numSegments) && (pCurrentRing->path[i].pPath != NULL); i++)
{
if ([Ring isPointInAngle: location: pCurrentRing->path[i]])
{
status = i;
break;
}
}
}
return status;
}
- (RingActionStatus) executeAction: (int) segment: (int *) pIgnoreKeyEvents
{
int i;
unsigned int flags;
CGEventRef event;
// Flags initialized to 0 will clear away the flag key used to bring up the ring,
// essentially masking it from the defined user key sequence which is what we want!
flags = 0;
// Velocity buffer just causes a refresh, no action taken
/*
if (pCurrentRing->action[segment].isVelocityBuffer)
{
return RING_ACTION_REFRESH;
}
*/
// Execute all the key codes
for (i = 0; i < MAX_KEYSTROKES; i++)
{
// Break if there is no valid keycode in the array
if (INVALID_KEY == pCurrentRing->action[segment].keyCode[i])
{
DebugLog(@"Invalid key hit!");
break;
}
else
{
DebugLog(@"Sending key: %i", pCurrentRing->action[segment].keyCode[i]);
event = CGEventCreateKeyboardEvent(0,
pCurrentRing->action[segment].keyCode[i],
pCurrentRing->action[segment].keyIsDown[i]);
if (pCurrentRing->action[segment].keyIsDown[i])
{
switch (pCurrentRing->action[segment].keyCode[i])
{
case 54:
case 55:
flags |= kCGEventFlagMaskCommand;
break;
case 56:
case 60:
flags |= kCGEventFlagMaskShift;
break;
case 57:
flags |= kCGEventFlagMaskAlphaShift;
break;
case 58:
case 61:
flags |= kCGEventFlagMaskAlternate;
break;
case 59:
case 62:
flags |= kCGEventFlagMaskControl;
break;
case 63:
break;
}
}
else
{
switch (pCurrentRing->action[segment].keyCode[i])
{
case 54:
case 55:
flags &= ~kCGEventFlagMaskCommand;
break;
case 56:
case 60:
flags &= ~kCGEventFlagMaskShift;
break;
case 57:
flags &= ~kCGEventFlagMaskAlphaShift;
break;
case 58:
case 61:
flags &= ~kCGEventFlagMaskAlternate;
break;
case 59:
case 62:
flags &= ~kCGEventFlagMaskControl;
break;
case 63:
break;
}
}
CGEventSetFlags(event, flags);
CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
/*
CGPostKeyboardEvent(0,
pCurrentRing->action[segment].keyCode[i],
pCurrentRing->action[segment].keyIsDown[i]);
*/
}
}
*pIgnoreKeyEvents = i;
// Scroll events keep the same ring
if (segment == pCurrentRing->scrollUpSegment ||
segment == pCurrentRing->scrollDownSegment)
{
return RING_ACTION_STAY;
}
// Normal ring actions w/ no subring just dismiss the ring
else if (NULL == pCurrentRing->action[segment].pNextRing)
{
return RING_ACTION_DONE;
}
// Actions w/ a subring move to the next ring
else
{
pCurrentRing = pCurrentRing->action[segment].pNextRing;
}
return RING_ACTION_REFRESH;
}
- (BOOL) containsNextRing: (int) segment
{
if (pCurrentRing->action[segment].pNextRing != NULL)
{
return YES;
}
return NO;
}
/*
- (BOOL) isVelocityBuffer: (int) segment
{
return pCurrentRing->action[segment].isVelocityBuffer;
}
*/
- (BOOL) isSubring
{
return pCurrentRing->isSubring;
}
- (void) generatePaths:(RingContainer*) pRingContainer: (int) segments: (float) startAngle: (float) spanAngle:
(float) centerX: (float) centerY: (bool) isSubring
{
int i;
int nonRingSegments = 0;
float angleCenter;
float angleLeftEdge;
float angleRightEdge;
float labelX;
float labelY;
NSSize labelSize;
// Adjust for non-ring segments
if (pRingContainer->scrollUpSegment >= 0)
{
nonRingSegments++;
}
if (pRingContainer->scrollDownSegment >= 0)
{
nonRingSegments++;
}
angleCenter = startAngle;
angleLeftEdge = angleCenter + (spanAngle / 2);
// Generate the segments
for (i = 0; i < segments; i++)
{
//
// Generate the bezier paths used for hit detection, clockwise (only for non-scroll wheel segments)
//
if ((i != pRingContainer->scrollUpSegment) && (i != pRingContainer->scrollDownSegment))
{
angleRightEdge = angleLeftEdge - spanAngle;
pRingContainer->path[i].pPath = [[NSBezierPath alloc] init];
[pRingContainer->path[i].pPath setLineWidth:2.0];
[pRingContainer->path[i].pPath appendBezierPathWithArcWithCenter:NSMakePoint(centerX, centerY)
radius:RING_INNER_RADIUS
startAngle:angleLeftEdge - SEGMENT_GAP
endAngle:angleRightEdge + SEGMENT_GAP
clockwise:YES];
[pRingContainer->path[i].pPath appendBezierPathWithArcWithCenter:NSMakePoint(centerX, centerY)
radius:RING_OUTER_RADIUS
startAngle:angleRightEdge + SEGMENT_GAP
endAngle:angleLeftEdge - SEGMENT_GAP
clockwise:NO];
[pRingContainer->path[i].pPath closePath];
// Store away the angles (always in positive form) for later hit detection
pRingContainer->path[i].angleEnd = (angleLeftEdge > 0) ? angleLeftEdge : 360 + angleLeftEdge;
pRingContainer->path[i].angleStart = (angleRightEdge > 0) ? angleRightEdge : 360 + angleRightEdge;
//DebugLog(@"Angles [%i] : %i %i", i, pRingContainer->path[i].angleStart, pRingContainer->path[i].angleEnd);
angleLeftEdge -= spanAngle;
}
//
// Generate the label positions, unless this is the special segment for the subring
//
/*
if ((0 == i) && isSubring)
{
pRingContainer->action[i].isVelocityBuffer = YES;
}
else
{
*/
labelSize = [pRingContainer->label[i].pLabel sizeWithAttributes:pRingContainer->pLabelAttributes];
// Scroll down segment
if (i == pRingContainer->scrollDownSegment)
{
// Start by setting the basic points for the current segment based on the angle
labelX = 0;
labelY = (RING_LABEL_RADIUS * sin(RADIANS(270))) - (labelSize.height * LABEL_HEIGHT_CORRECTION) - (LABEL_BOX_BORDER_Y * 4);
}
// Scroll up segment
else if (i == pRingContainer->scrollUpSegment)
{
// Start by setting the basic points for the current segment based on the angle
labelX = 0;
labelY = (RING_LABEL_RADIUS * sin(RADIANS(90))) + (labelSize.height * LABEL_HEIGHT_CORRECTION) + (LABEL_BOX_BORDER_Y * 4);
}
// Regular Segments
else
{
// Start by setting the basic points for the current segment based on the angle
labelX = RING_LABEL_RADIUS * cos(RADIANS(angleCenter));
labelY = RING_LABEL_RADIUS * sin(RADIANS(angleCenter));
}
//DebugLog(@"labelX %f | labelY %f | labelSize %f %f", labelX, labelY, labelSize.height, labelSize.width);
// If labelX = 0, the text must appear centered
if (labelX > -1.0 && labelX < 1.0)
{
labelX -= (labelSize.width / 2);
}
// If labelX > 0, account only for the label border
else if (labelX >= 1.0)
{
labelX += LABEL_BOX_BORDER_X;
}
// If labelX < 0, account for label border and right justify
else
{
labelX -= labelSize.width + LABEL_BOX_BORDER_X;
}
// If labelY < 0, account for the label height
if (labelY > -1.0 && labelY < 1.0)
{
labelY -= (labelSize.height * LABEL_HEIGHT_CORRECTION) / 2;
}
else if (labelY >= 1.0)
{
labelY += LABEL_BOX_BORDER_Y;
}
else
{
labelY -= (labelSize.height * LABEL_HEIGHT_CORRECTION) + LABEL_BOX_BORDER_Y;
}
//DebugLog(@"labelX %f | labelY %f | labelSize %f %f", labelX, labelY, labelSize.height, labelSize.width);
// Finally, place the location relative to the center of the ring.
pRingContainer->label[i].location.x = centerX + labelX;
pRingContainer->label[i].location.y = centerY + labelY;
// Create the label box
pRingContainer->label[i].pLabelBox = [[NSBezierPath alloc] init];
[pRingContainer->label[i].pLabelBox appendBezierPathWithRoundedRect:
NSMakeRect(pRingContainer->label[i].location.x - LABEL_BOX_BORDER_X,
pRingContainer->label[i].location.y - LABEL_BOX_BORDER_Y,
labelSize.width + (LABEL_BOX_BORDER_X * 2),
(labelSize.height * LABEL_HEIGHT_CORRECTION) + (LABEL_BOX_BORDER_Y * 2))
xRadius: LABEL_BOX_ROUND
yRadius: LABEL_BOX_ROUND];
//}
angleCenter -= spanAngle;
}
}
+ (int) arrowSegmentHitTest: (unsigned int) arrowBitfield: (int) segments;
{
// Arrow bitfield definition:
#define ARROW_LEFT (1 << 0)
#define ARROW_RIGHT (1 << 1)
#define ARROW_DOWN (1 << 2)
#define ARROW_UP (1 << 3)
unsigned int segment = -1;
// Subtract off the scroll segments, right now those cannot be arrow activated
segments -= 2;
DebugLog(@"arrowBitfield = %i, segments = %i", arrowBitfield, segments);
switch (segments)
{
case 1:
segment = 0;
break;
case 2:
if (ARROW_UP == arrowBitfield)
{
segment = 0;
}
else if (ARROW_DOWN == arrowBitfield)
{
segment = 1;
}
break;
case 3:
if (ARROW_UP == arrowBitfield)
{
segment = 0;
}
else if (ARROW_RIGHT == arrowBitfield)
{
segment = 1;
}
else if (ARROW_LEFT == arrowBitfield)
{
segment = 2;
}
break;
case 4:
if (ARROW_UP == arrowBitfield)
{
segment = 0;
}
else if (ARROW_RIGHT == arrowBitfield)
{
segment = 1;
}
else if (ARROW_DOWN == arrowBitfield)
{
segment = 2;
}
else if (ARROW_LEFT == arrowBitfield)
{
segment = 3;
}
break;
case 5:
if (ARROW_UP == arrowBitfield)
{
segment = 0;
}
else if ((ARROW_UP | ARROW_RIGHT) == arrowBitfield)
{
segment = 1;
}
else if ((ARROW_DOWN | ARROW_RIGHT) == arrowBitfield)
{
segment = 2;
}
else if ((ARROW_DOWN | ARROW_LEFT) == arrowBitfield)
{
segment = 3;
}
else if ((ARROW_UP | ARROW_LEFT) == arrowBitfield)
{
segment = 4;
}
break;
case 6:
if (ARROW_UP == arrowBitfield)
{
segment = 0;
}
else if ((ARROW_UP | ARROW_RIGHT) == arrowBitfield)
{
segment = 1;
}
else if ((ARROW_DOWN | ARROW_RIGHT) == arrowBitfield)
{
segment = 2;
}
else if (ARROW_DOWN == arrowBitfield)
{
segment = 3;
}
else if ((ARROW_DOWN | ARROW_LEFT) == arrowBitfield)
{
segment = 4;
}
else if ((ARROW_UP | ARROW_LEFT) == arrowBitfield)
{
segment = 5;
}
break;
}
return segment;
}
+ (bool) isPointInAngle: (NSPoint) location: (PathElements) path
{
float pointAngle;
// Calculate the arctan, and normalize to a positive angle
pointAngle = DEGREES(atan2(location.y, location.x));
if (pointAngle < 0)
{
pointAngle = 360 + pointAngle;
}
// Normal case, start angle less than end angle
if (path.angleStart < path.angleEnd)
{
DebugLog(@"hitDetect - normal case! point angle: %f segstart: %i segend: %i",
pointAngle, path.angleStart, path.angleEnd);
if (pointAngle >= path.angleStart && pointAngle < path.angleEnd)
{
return YES;
}
}
// Wrap around case
else
{
DebugLog(@"hitDetect - wrap case! point angle: %f segstart: %i segend: %i",
pointAngle, path.angleStart, path.angleEnd);
if (pointAngle >= path.angleStart || pointAngle < path.angleEnd)
{
return YES;
}
}
return NO;
}
@end