-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFLRadialPainter.m
273 lines (217 loc) · 6.26 KB
/
FLRadialPainter.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
/* Copyright (C) 1996 Dave Vasilevsky
* This file is licensed under the GNU General Public License,
* see the file Copying.txt for details. */
#import "FLRadialPainter.h"
#import "FLPolar.h"
#import "NSBezierPath+Segment.h"
#import "FLRadialItem.h"
@implementation NSView (FLRadialPainter)
- (NSPoint) center
{
NSRect bounds = [self bounds];
return NSMakePoint(NSMidX(bounds), NSMidY(bounds));
}
- (float) maxRadius
{
NSRect bounds = [self bounds];
NSSize size = bounds.size;
float minDim = size.width < size.height ? size.width : size.height;
return minDim / 2.0;
}
@end
@implementation FLRadialPainter
- (id) initWithView: (NSView <FLHasDataSource> *)view;
{
if (self = [super init]) {
// Default values
m_maxLevels = 5;
m_minRadiusFraction = 0.1;
m_maxRadiusFraction = 0.9;
m_minPaintAngle = 1.0;
m_view = view; // No retain, view should own us
m_colorer = nil;
}
return self;
}
- (void) dealloc
{
if (m_colorer) [m_colorer release];
[super dealloc];
}
#pragma mark Accessors
- (int) maxLevels
{
return m_maxLevels;
}
- (void) setMaxLevels: (int)levels
{
NSAssert(levels > 0, @"maxLevels must be positive!");
m_maxLevels = levels;
}
- (float) minRadiusFraction
{
return m_minRadiusFraction;
}
- (void) setMinRadiusFraction: (float)fraction
{
NSAssert(fraction >= 0.0 && fraction <= 1.0,
@"fraction must be between zero and one!");
NSAssert(fraction < [self maxRadiusFraction],
@"minRadius must be less than maxRadius!");
m_minRadiusFraction = fraction;
}
- (float) maxRadiusFraction
{
return m_maxRadiusFraction;
}
- (void) setMaxRadiusFraction: (float)fraction
{
NSAssert(fraction >= 0.0 && fraction <= 1.0,
@"fraction must be between zero and one!");
NSAssert(fraction > [self minRadiusFraction],
@"minRadius must be less than maxRadius!");
m_maxRadiusFraction = fraction;
}
- (float) minPaintAngle
{
return m_minPaintAngle;
}
- (void) setMinPaintAngle: (float)angle
{
m_minPaintAngle = angle;
}
- (id) colorer
{
return m_colorer;
}
- (void) setColorer: (id) c
{
[c retain];
if (m_colorer) [m_colorer release];
m_colorer = c;
}
- (NSView <FLHasDataSource> *) view
{
return m_view;
}
- (void) setView: (NSView <FLHasDataSource> *)view
{
m_view = view; // No retain, view should own us
}
#pragma mark Misc
- (FLRadialItem *) root
{
return [FLRadialItem rootItemWithDataSource: [[self view] dataSource]];
}
- (BOOL) wantItem: (FLRadialItem *) ritem
{
return [ritem level] < [self maxLevels]
&& [ritem angleSpan] >= [self minPaintAngle];
}
- (float) radiusFractionPerLevel
{
float availFraction = [self maxRadiusFraction] - [self minRadiusFraction];
return availFraction / [self maxLevels];
}
#pragma mark Painting
- (float) innerRadiusFractionForLevel: (int)level
{
// TODO: Deal with concept of "visible levels" <= maxLevels
NSAssert(level <= [self maxLevels], @"Level too high!");
return [self minRadiusFraction] + ([self radiusFractionPerLevel] * level);
}
// Default coloring scheme
- (NSColor *) colorForItem: (id) item
angleFrac: (float) angle
levelFrac: (float) level
{
return [NSColor colorWithCalibratedHue: angle
saturation: 0.6 - (level / 4)
brightness: 1.0
alpha: 1.0];
}
- (NSColor *) colorForItem: (FLRadialItem *)ritem
{
float levelFrac = (float)[ritem level] / ([self maxLevels] - 1);
float midAngle = [ritem midAngle];
float angleFrac = midAngle / 360.0;
angleFrac -= floorf(angleFrac);
NSAssert(angleFrac >= 0 && angleFrac <= 1.0,
@"Angle fraction must be between zero and one");
id c = m_colorer ? m_colorer : self;
return [c colorForItem: [ritem item]
angleFrac: angleFrac
levelFrac: levelFrac];
}
- (void) drawItem: (FLRadialItem *)ritem
{
int level = [ritem level];
float inner = [self innerRadiusFractionForLevel: level];
float outer = [self innerRadiusFractionForLevel: level + 1];
NSColor *fill = [self colorForItem: ritem];
NSBezierPath *bp = [NSBezierPath
circleSegmentWithCenter: [[self view] center]
startAngle: [ritem startAngle]
endAngle: [ritem endAngle]
smallRadius: inner * [[self view] maxRadius]
bigRadius: outer * [[self view] maxRadius]];
[fill set];
[bp fill];
[[fill shadowWithLevel: 0.4] set];
[bp stroke];
}
- (void) drawTreeForItem: (FLRadialItem *)ritem
{
if (![self wantItem: ritem]) {
return;
}
if ([ritem level] >= 0 && [ritem weight] > 0) {
[self drawItem: ritem];
}
// Draw the children
NSEnumerator *e = [ritem childEnumerator];
FLRadialItem *child;
while (child = [e nextObject]) {
[self drawTreeForItem: child];
}
}
- (void)drawRect: (NSRect)rect
{
// TODO: Choose root item(s) from rect
[self drawTreeForItem: [self root]];
}
#pragma mark Hit testing
- (id) findChildOf: (FLRadialItem *)ritem
depth: (int)depth
angle: (float)th
{
NSAssert(depth >= 0, @"Depth must be at least zero");
NSAssert(th >= [ritem startAngle], @"Not searching the correct tree");
if (![self wantItem: ritem]) {
return nil;
}
if (depth == 0) {
return [ritem item];
}
NSEnumerator *e = [ritem childEnumerator];
FLRadialItem *child;
while (child = [e nextObject]) {
if ([child endAngle] >= th) {
return [self findChildOf: child depth: depth - 1 angle: th];
}
}
return nil;
}
- (id) itemAt: (NSPoint)point
{
float r, th;
[FLPolar coordsForPoint: point center: [[self view] center] intoRadius: &r angle: &th];
float rfrac = r / [[self view] maxRadius];
if (rfrac < [self minRadiusFraction] || rfrac >= [self maxRadiusFraction]) {
return nil;
}
float usedFracs = rfrac - [self minRadiusFraction];
int depth = floorf(usedFracs / [self radiusFractionPerLevel]) + 1;
return [self findChildOf: [self root] depth: depth angle: th];
}
@end