-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRSEyeXYController.m
More file actions
344 lines (281 loc) · 11.3 KB
/
CRSEyeXYController.m
File metadata and controls
344 lines (281 loc) · 11.3 KB
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
//
// CRSEyeXYController.m
// Experiment
//
// XY Display of eye position.
//
// Copyright (c) 2006. All rights reserved.
//
#define NoGlobals
#import "CRSEyeXYController.h"
#define kCircleRadiusDeg 0.15
#define kCrossArmDeg 0.25
#define kLineWidthDeg 0.02
NSString *CRSEyeXYDoGridKey = @"CRSEyeXYDoGrid";
NSString *CRSEyeXYDoTicksKey = @"CRSEyeXYDoTicks";
NSString *CRSEyeXYSamplesSavedKey = @"CRSEyeXYSamplesSaved";
NSString *CRSEyeXYDotSizeDegKey = @"CRSEyeXYDotSizeDeg";
NSString *CRSEyeXYDrawCalKey = @"CRSEyeXYDrawCal";
//NSString *CRSEyeXYEyeColorKey = @"CRSEyeXYEyeColor";
NSString *CRSEyeXYLEyeColorKey = @"CRSEyeXYLEyeColor";
NSString *CRSEyeXYREyeColorKey = @"CRSEyeXYREyeColor";
NSString *CRSEyeXYFadeDotsKey = @"CRSEyeXYFadeDots";
NSString *CRSEyeXYGridDegKey = @"CRSEyeXYGridDeg";
NSString *CRSEyeXYHScrollKey = @"CRSEyeXYHScroll";
NSString *CRSEyeXYMagKey = @"CRSEyeXYMag";
NSString *CRSEyeXYOneInNKey = @"CRSEyeXYOneInN";
NSString *CRSEyeXYVScrollKey = @"CRSEyeXYVScroll";
NSString *CRSEyeXYTickDegKey = @"CRSEyeXYTickDeg";
NSString *CRSEyeXYWindowVisibleKey = @"CRSEyeXYWindowVisible";
NSString *CRSXYAutosaveKey = @"CRSXYAutosave";
@implementation CRSEyeXYController
- (IBAction)centerDisplay:(id)sender {
[eyePlot centerDisplay];
}
- (IBAction)changeZoom:(id)sender {
[self setScaleFactor:[sender floatValue]];
}
// Prepare to be destroyed. This odd method is needed because we increased our retainCount when we added
// ourselves to eyePlot (in windowDidLoad). Owing to that increment, the object that created us will never
// get us to a retainCount of zero when it releases us. For that reason, we need this method as a route
// for our creating object to get us to get us released from eyePlot and prepared to be fully released.
- (void)deactivate;
{
[eyePlot removeDrawable:self]; // Remove ourselves, lowering our retainCount;
[self close]; // clean up
}
- (void) dealloc;
{
long eyeIndex;
NSRect r;
r = [eyePlot visibleRect];
[[task defaults] setFloat:r.origin.x forKey:CRSEyeXYHScrollKey];
[[task defaults] setFloat:r.origin.y forKey:CRSEyeXYVScrollKey];
[fixWindowColor release];
[respWindowColor release];
[calColor release];
for (eyeIndex = kLeftEye; eyeIndex < kEyes; eyeIndex++) {
[unitsToDeg[eyeIndex] release];
[degToUnits[eyeIndex] release];
[calBezierPath[eyeIndex] release];
[eyeXSamples[eyeIndex] release];
[eyeYSamples[eyeIndex] release];
}
[sampleLock release];
[super dealloc];
}
- (IBAction) doOptions:(id)sender {
[NSApp beginSheet:optionsSheet modalForWindow:[self window] modalDelegate:self
didEndSelector:nil contextInfo:nil];
}
// Because we have added ourself as an LLDrawable to the eyePlot, this draw method
// will be called every time eyePlot redraws. This allows us to put in any specific
// windows, etc that we want to display.
- (void)draw
{
float defaultLineWidth = [NSBezierPath defaultLineWidth];
// Draw the fixation window
if (NSPointInRect(currentEyeDeg[kLeftEye], eyeWindowRectDeg)) {
[[fixWindowColor highlightWithLevel:0.90] set];
[NSBezierPath fillRect:eyeWindowRectDeg];
}
[fixWindowColor set];
[NSBezierPath setDefaultLineWidth:defaultLineWidth * 4.0];
[NSBezierPath strokeRect:eyeWindowRectDeg];
// Draw the response windows
if (NSPointInRect(currentEyeDeg[kLeftEye], respWindowRectDeg) || NSPointInRect(currentEyeDeg[kRightEye], respWindowRectDeg)) {
[[respWindowColor highlightWithLevel:0.80] set];
[NSBezierPath fillRect:respWindowRectDeg];
}
[respWindowColor set];
[NSBezierPath setDefaultLineWidth:(defaultLineWidth * ((inTrial) ? 4.0 : 1.0))];
[NSBezierPath strokeRect:respWindowRectDeg];
[NSBezierPath setDefaultLineWidth:defaultLineWidth];
// Draw the calibration for the fixation window
if ([[task defaults] integerForKey:CRSEyeXYDrawCalKey]) {
//[calColor set];
[[eyePlot eyeLColor] set];
[calBezierPath[kLeftEye] stroke];
[[eyePlot eyeRColor] set];
[calBezierPath[kRightEye] stroke];
}
}
- (IBAction) endOptionSheet:(id)sender {
[self setEyePlotValues];
[optionsSheet orderOut:sender];
[NSApp endSheet:optionsSheet returnCode:1];
}
- (id)init {
if ((self = [super initWithWindowNibName:@"CRSEyeXYController"]) != nil) {
[[task defaults] registerDefaults:
[NSDictionary dictionaryWithObject:
[NSArchiver archivedDataWithRootObject:[NSColor blueColor]]
forKey:CRSEyeXYLEyeColorKey]];
[[task defaults] registerDefaults:
[NSDictionary dictionaryWithObject:
[NSArchiver archivedDataWithRootObject:[NSColor blueColor]]
forKey:CRSEyeXYREyeColorKey]];
//eyeXSamples = [[NSMutableData alloc] init];
//eyeYSamples = [[NSMutableData alloc] init];
eyeXSamples[kLeftEye] = [[NSMutableData alloc] init];
eyeYSamples[kLeftEye] = [[NSMutableData alloc] init];
eyeXSamples[kRightEye] = [[NSMutableData alloc] init];
eyeYSamples[kRightEye] = [[NSMutableData alloc] init];
sampleLock = [[NSLock alloc] init];
[self setShouldCascadeWindows:NO];
[self setWindowFrameAutosaveName:CRSXYAutosaveKey];
[self window]; // Force the window to load now
}
return self;
}
- (void)processEyeSamplePairs:(long)eyeIndex;
{
NSEnumerator *enumerator;
NSArray *pairs;
NSValue *value;
[sampleLock lock];
pairs = [LLDataUtil pairXSamples:eyeXSamples[eyeIndex] withYSamples:eyeYSamples[eyeIndex]];
[sampleLock unlock];
if (pairs != nil) {
enumerator = [pairs objectEnumerator];
while ((value = [enumerator nextObject])) {
currentEyeDeg[eyeIndex] = [unitsToDeg[eyeIndex] transformPoint:[value pointValue]];
[eyePlot addSample:currentEyeDeg[eyeIndex] forEye:eyeIndex];
}
}
}
- (void)setEyePlotValues {
[eyePlot setDotSizeDeg:[[task defaults] floatForKey:CRSEyeXYDotSizeDegKey]];
[eyePlot setDotFade:[[task defaults] boolForKey:CRSEyeXYFadeDotsKey]];
[eyePlot setEyeColor:[NSUnarchiver
unarchiveObjectWithData:[[task defaults]
objectForKey:CRSEyeXYLEyeColorKey]] forEye:kLeftEye];
[eyePlot setGrid:[[task defaults] boolForKey:CRSEyeXYDoGridKey]];
[eyePlot setGridDeg:[[task defaults] floatForKey:CRSEyeXYGridDegKey]];
[eyePlot setOneInN:[[task defaults] integerForKey:CRSEyeXYOneInNKey]];
[eyePlot setTicks:[[task defaults] boolForKey:CRSEyeXYDoTicksKey]];
[eyePlot setTickDeg:[[task defaults] floatForKey:CRSEyeXYTickDegKey]];
[eyePlot setSamplesToSave:[[task defaults] integerForKey:CRSEyeXYSamplesSavedKey]];
}
// Change the scaling factor for the view
// Because scaleUnitSquareToSize acts on the current scaling, not the original scaling,
// we have to work out the current scaling using the relative scaling of the eyePlot and
// its superview
- (void) setScaleFactor:(double)factor;
{
float currentFactor, applyFactor;
currentFactor = [eyePlot bounds].size.width / [[eyePlot superview] bounds].size.width;
applyFactor = factor / currentFactor;
[[scrollView contentView] scaleUnitSquareToSize:NSMakeSize(applyFactor, applyFactor)];
[self centerDisplay:self];
}
- (void)updateEyeCalibration:(long)eyeIndex eventData:(NSData *)eventData;
{
LLEyeCalibrationData cal;
[eventData getBytes:&cal];
[unitsToDeg[eyeIndex] setTransformStruct:cal.calibration];
[degToUnits[eyeIndex] setTransformStruct:cal.calibration];
[degToUnits[eyeIndex] invert];
[calBezierPath[eyeIndex] autorelease];
calBezierPath[eyeIndex] = [LLEyeCalibrator bezierPathForCalibration:cal];
[calBezierPath[eyeIndex] retain];
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification;
{
[[task defaults] setObject:[NSNumber numberWithBool:YES] forKey:CRSEyeXYWindowVisibleKey];
}
// Initialization is handled through the following delegate method for our window
- (void) windowDidLoad;
{
calColor = [[NSColor colorWithDeviceRed:0.60 green:0.45 blue:0.15 alpha:1.0] retain];
fixWindowColor = [[NSColor colorWithDeviceRed:0.00 green:0.00 blue:1.00 alpha:1.0] retain];
respWindowColor = [[NSColor colorWithDeviceRed:0.95 green:0.55 blue:0.50 alpha:1.0] retain];
unitsToDeg[kLeftEye] = [[NSAffineTransform alloc] initWithTransform:[NSAffineTransform transform]];
unitsToDeg[kRightEye] = [[NSAffineTransform alloc] initWithTransform:[NSAffineTransform transform]];
degToUnits[kLeftEye] = [[NSAffineTransform alloc] initWithTransform:[NSAffineTransform transform]];
degToUnits[kRightEye] = [[NSAffineTransform alloc] initWithTransform:[NSAffineTransform transform]];
[self setScaleFactor:[[task defaults] floatForKey:CRSEyeXYMagKey]];
[self setEyePlotValues];
[eyePlot addDrawable:self];
[self changeZoom:slider];
[eyePlot scrollPoint:NSMakePoint(
[[task defaults] floatForKey:CRSEyeXYHScrollKey],
[[task defaults] floatForKey:CRSEyeXYVScrollKey])];
[[self window] setFrameUsingName:CRSXYAutosaveKey]; // Needed when opened a second time
if ([[task defaults] boolForKey:CRSEyeXYWindowVisibleKey]) {
[[self window] makeKeyAndOrderFront:self];
}
else {
[NSApp addWindowsItem:[self window] title:[[self window] title] filename:NO];
}
[scrollView setPostsBoundsChangedNotifications:YES];
[super windowDidLoad];
}
- (BOOL) windowShouldClose:(NSNotification *)aNotification;
{
[[self window] orderOut:self];
[[task defaults] setObject:[NSNumber numberWithBool:NO] forKey:CRSEyeXYWindowVisibleKey];
[NSApp addWindowsItem:[self window] title:[[self window] title] filename:NO];
return NO;
}
// Methods related to data events follow:
// Update the display of the calibration in the xy window. We get the calibration structure
// and use it to construct crossing lines that mark the current calibration.
- (void)eyeLeftCalibration:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[self updateEyeCalibration:kLeftEye eventData:eventData];
}
- (void)eyeRightCalibration:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[self updateEyeCalibration:kRightEye eventData:eventData];
}
- (void)eyeWindow:(NSData *)eventData eventTime:(NSNumber *)eventTime {
FixWindowData fixWindowData;
[eventData getBytes:&fixWindowData];
eyeWindowRectDeg = fixWindowData.windowDeg;
[eyePlot setNeedsDisplay:YES];
}
// Just save the x eye data until we get the corresponding y eye data
- (void)eyeLXData:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[sampleLock lock];
[eyeXSamples[kLeftEye] appendData:eventData];
[sampleLock unlock];
[self processEyeSamplePairs:kLeftEye];
}
- (void)eyeLYData:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[sampleLock lock];
[eyeYSamples[kLeftEye] appendData:eventData];
[sampleLock unlock];
}
- (void)eyeRXData:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[sampleLock lock];
[eyeXSamples[kRightEye] appendData:eventData];
[sampleLock unlock];
}
- (void)eyeRYData:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[sampleLock lock];
[eyeYSamples[kRightEye] appendData:eventData];
[sampleLock unlock];
[self processEyeSamplePairs:kRightEye];
}
- (void)responseWindow:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
FixWindowData respWindowData;
[eventData getBytes:&respWindowData];
respWindowRectDeg = respWindowData.windowDeg;
}
- (void)trial:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[eventData getBytes:&trial];
inTrial = YES;
respWindowIndex = kAttend0;
}
- (void)trialEnd:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
inTrial = NO;
}
@end