-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSelect360.m
99 lines (70 loc) · 1.6 KB
/
Select360.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
//
// Select360.m
// FluidApp
//
#import "Select360.h"
#include <math.h>
@implementation Select360
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
m_value = (NSPoint){0,-1};
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
NSRect rect = [self bounds];
float cx = rect.size.width/2 + rect.origin.x;
float cy = rect.size.height/2 + rect.origin.y;
rect.size.width-=6;
rect.size.height-=6;
rect.origin.x+=3;
rect.origin.y+=3;
NSPoint c = {cx,cy};
[[NSColor blackColor] set];
NSBezierPath *p = [NSBezierPath bezierPathWithOvalInRect:rect];
[p stroke];
NSPoint d = { m_value.x * rect.size.width/2 + cx,
m_value.y * rect.size.height/2 + cy};
[NSBezierPath strokeLineFromPoint:c toPoint:d];
NSRect r2 = {cx-2,cy-2,4,4};
[[NSBezierPath bezierPathWithOvalInRect:r2] fill];
}
- (void)mouseDown:(NSEvent*)e
{
NSRect rect = [self bounds];
float cx = rect.size.width/2 + rect.origin.x;
float cy = rect.size.height/2 + rect.origin.y;
NSPoint p = {cx,cy};
NSPoint m = [self convertPoint:[e locationInWindow] fromView:nil];
//printf("(%f %f) - (%f %f)\n",p.x, p.y, m.x, m.y);
p.x = m.x - p.x;
p.y = m.y - p.y;
if (p.x*p.x + p.y*p.y > 3.0f)
{
float d = sqrtf(p.x*p.x + p.y*p.y);
m_value.x = p.x/d;
m_value.y = p.y/d;
}
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent*)e
{
[self mouseDown:e];
}
- (NSPoint)vector
{
return m_value;
}
- (void)setVectorX:(float) x Y:(float)y
{
float d = sqrtf(x*x + y*y);
if (d > 0.01f)
{
m_value = (NSPoint){x/d, y/d};
[self setNeedsDisplay:YES];
}
}
@end