/
editModeTouchCallbacks.m
534 lines (410 loc) · 14.2 KB
/
editModeTouchCallbacks.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
//
// editModeTouchCallbacks.m
// Vertices
//
// Created by James O'Neill on 11/10/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "editModeTouchCallbacks.h"
@implementation editModeTouchCallbacks
@synthesize owner;
@synthesize view;
@synthesize touchTracker;
@synthesize pd;
@synthesize nodeToDrawObject;
@synthesize nodeFromUserTouches;
@synthesize nodeFromRecordedTouches;
@synthesize editableSOD;
@synthesize editing;
@synthesize playingNode;
- (id)initWithOwner:(EditViewController*)own {
self = [super init];
if (self) {
[self setOwner:own];
[self setView:(EditView*)[[self owner] view]];
[self setTouchTracker:[[self owner] touchTracker]];
[self setPd:[[self owner] pd]];
[self setNodeToDrawObject:[[self owner] nodeToDrawObject]];
//these two SOs will be used to determine the nodeToDrawObject for each node that is playing back
[self setNodeFromRecordedTouches:[[SoundObjectNodeToDraw alloc] init]];
[self setNodeFromUserTouches:[[SoundObjectNodeToDraw alloc] init]];
editing = NO;
}
return self;
}
-(void)startEditModeWithDecoder:(SoundObjectDecoder *)sod {
[self setEditableSOD:sod]; //copies the sod
editableSOD->voice = kPreviewVoice;
[editableSOD setIsPlaying:[NSNumber numberWithBool:FALSE]];
[self playEditable];
[NSThread detachNewThreadSelector:@selector(editableObjectsPlaybackTimer)
toTarget:self
withObject:nil];
}
-(void)endEditMode {
}
-(void) playEditable {
if (self.editableSOD != NULL) {
[editableSOD setIsPlaying:[NSNumber numberWithBool:TRUE]];
editableSOD->nodeIndex = 0;
[editableSOD setStartTime:[NSNumber numberWithDouble:[EditViewController timestamp]]];
}
}
-(void) stopEditable {
}
- (void)editableObjectsPlaybackTimer {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSMutableArray* playingObjects = [receivedViewController playingSoundObjectDecoders];
double time;
while (TRUE) {
[NSThread sleepForTimeInterval:0.006];
if(owner.scratchMode)
break;
else if([editableSOD isPlaying]) {
time = [EditViewController timestamp];
//MAKE NOISE
[editableSOD editModeCueForTimestamp:time - [[editableSOD startTime] doubleValue]];
/*
for (i = 0; i<[playingObjects count]; i+=1) {
//cue objects
[[playingObjects objectAtIndex:i] cueForTimestamp:(time - [[[playingObjects objectAtIndex:i] startTime] doubleValue])];
}
*/
}
}
[pool release];
}
- (void)updateNodeToDrawPoints:(CGPoint)pointToUpdate withID:(int)pointID andState:(BOOL)state
{
if (state == YES)
{
[[owner nodeToDrawObject] setPointIDValue:pointID atIndex:pointID];
[[owner nodeToDrawObject] setPointValues:pointToUpdate atIndex:pointID];
}
else
{
[[owner nodeToDrawObject] setPointIDValue:-1 atIndex:pointID];
}
}
- (void)updateNodeFromUserTouchesPoints:(CGPoint)pointToUpdate withID:(int)pointID andState:(BOOL)state
{
if (state == YES)
{
[[self nodeFromUserTouches] setPointIDValue:pointID atIndex:pointID];
[[self nodeFromUserTouches] setPointValues:pointToUpdate atIndex:pointID];
}
else
{
[[self nodeFromUserTouches] setPointIDValue:-1 atIndex:pointID];
}
}
- (void)editModeUpdateNodeToDraw:(SoundObjectNode *)son {
int length = [[son runningXCoords] count];
[[owner nodeToDrawObject] setNumTouches:length];
int tID;
int i;
CGPoint point;
//add touches
for (i = 0; i < length; i+=1) {
point.x = [[[son runningXCoords] objectAtIndex:i] floatValue];
point.y = [[[son runningYCoords] objectAtIndex:i] floatValue];
tID = [[[son runningTouchIDs] objectAtIndex:i] intValue];
[self updateNodeToDrawPoints:point withID:tID andState:YES];
}
//take away touches that may have been there - touchIDs will always be contiguous and increasing because of the way they are assigned
for (i = length; i<MAX_TOUCHES; i+=1) {
[self updateNodeToDrawPoints:point withID:i andState:NO];
}
[self performSelectorOnMainThread:@selector(setNeedsDisplayHook:) withObject:nil waitUntilDone:NO];
}
- (void)setNeedsDisplayHook:(NSNull*)n {
[owner.view setNeedsDisplay];
}
- (void)mergeNode:(SoundObjectNode *)son {
editing = YES;
int playingIDLink, playingIndex;
CGPoint userPoint;
for(int i = 0; i<MAX_TOUCHES; i+=1) {
playingIDLink = [self nodeFromUserTouches]->nodeToDraw.userNodeIDtoPlayingID[i];
if(playingIDLink != -1) {
playingIndex = -1;
for(int j = 0; j<[[son runningTouchIDs] count]; j+=1) {
if([[[son runningTouchIDs] objectAtIndex:j] intValue] == playingIDLink) {
playingIndex = j;
break;
}
}
if(playingIndex == -1) {
//the vertex has been removed in the recording - unlink
[self unlinkTouchForID:i];
}else {
userPoint = [[self nodeFromUserTouches] getPointAtIndex:i];
[[son runningXCoords] replaceObjectAtIndex:playingIndex withObject:[NSNumber numberWithFloat:userPoint.x]];
[[son runningYCoords] replaceObjectAtIndex:playingIndex withObject:[NSNumber numberWithFloat:userPoint.y]];
}
}
}
[self setPlayingNode:son];
}
- (void)linkTouchForID:(int)tID
{
int length = [[[self playingNode] runningXCoords] count];
CGPoint newPoint = [[self nodeFromUserTouches] getPointAtIndex:tID];
CGPoint point;
float diffx, diffy, dist;
int playingTID;
BOOL touchControlled;
//find the points that are not controlled and see if one is close enough
for (int i = 0; i < length; i+=1) {
playingTID = [[[[self playingNode] runningTouchIDs] objectAtIndex:i] intValue];
touchControlled = NO;
for(int j = 0; j<MAX_TOUCHES; j+=1) {
if([self nodeFromUserTouches]->nodeToDraw.userNodeIDtoPlayingID[j] == playingTID) {
touchControlled = YES;
break;
}
}
if(!touchControlled) {
point.x = [[[[self playingNode] runningXCoords] objectAtIndex:i] floatValue];
point.y = [[[[self playingNode] runningYCoords] objectAtIndex:i] floatValue];
diffx = newPoint.x - point.x;
diffy = newPoint.y - point.y;
dist = sqrt(diffx * diffx + diffy * diffy);
if (dist < 35.0) {
[self nodeFromUserTouches]->nodeToDraw.userNodeIDtoPlayingID[tID] = playingTID;
[[owner nodeToDrawObject] setEditStatusValue:1 atIndex:tID];
return;
//float oldValX = [[[[self playingNode] runningXCoords] objectAtIndex:i] floatValue];
//NSLog(@"Got em': %f", dist);
//[[[self playingNode] runningXCoords] replaceObjectAtIndex:i withObject:[NSNumber numberWithFloat:newPoint.x]];
//[[[self playingNode] runningYCoords] replaceObjectAtIndex:i withObject:[NSNumber numberWithFloat:newPoint.y]];
//float newValX = [[[[self playingNode] runningXCoords] objectAtIndex:i] floatValue];
//[self updateNodeToDrawPoints:newPoint withID:tID andState:YES];
}
}
}
}
- (void)unlinkTouchForID:(int)tID {
[self nodeFromUserTouches]->nodeToDraw.userNodeIDtoPlayingID[tID] = -1;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *beginTouches = [event allTouches];
for (UITouch *touch in beginTouches)
{
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
if(fingerID > 4) {
NSLog(@"shit");
}
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID == -1)
{
//add it to our list
fingerID = [touchTracker AddNewTouch:touch];
[self updateNodeFromUserTouchesPoints:point withID:fingerID andState:YES];
} else
{
[self updateNodeFromUserTouchesPoints:point withID:fingerID andState:YES];
continue;
}
[self linkTouchForID:fingerID];
}
//ADD TO nodeFromUserTouches
/*
NSSet *beginTouches = [event allTouches];
[owner setCurrentTouches:[event allTouches]];
//owner->numberOfCurrentTouches += [beginTouches count];
//owner->nodeToDraw.numTouches = [beginTouches count];
[[owner nodeToDrawObject] setNumTouches:[beginTouches count]];
//[pd numberOfTouches:(float)(owner->numberOfCurrentTouches) toVoiceWithId:kPreviewVoice];
float xCentroid = 0;
float yCentroid = 0;
float xSum = 0;
float ySum = 0;
// Compute centroid of each coordinate
for (UITouch *touch in beginTouches)
{
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
if(fingerID > 4) {
NSLog(@"shit");
}
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID == -1)
{
//add it to our list
fingerID = [touchTracker AddNewTouch:touch];
[self updateNodeToDrawPoints:point withID:fingerID andState:YES];
} else
{
//already on the list. Don't send this
int dsfdsfsdf = 1;//LogMsg("Ignoring touch %d", fingerID);
[self updateNodeToDrawPoints:point withID:fingerID andState:YES];
continue;
}
//touches will already be logged for the recorder to fetch
if(owner->record == TRUE)
[owner.recorder addTouches:touches withAction:kTouchesBegan atTime:(double)[event timestamp]];
xSum = xSum + point.x;
ySum = ySum + point.y;
}
[view setNeedsDisplay];
xCentroid = xSum / [[owner nodeToDrawObject] getNumTouches];
yCentroid = ySum / [[owner nodeToDrawObject] getNumTouches];
[pd sendXCentroid:xCentroid toVoiceWithId:kPreviewVoice];
[pd sendYCentroid:yCentroid toVoiceWithId:kPreviewVoice];
[pd numberOfTouches:(float)([[owner nodeToDrawObject] getNumTouches]) toVoiceWithId:kPreviewVoice];
if(owner->record == TRUE)
[owner.recorder addTouches:touches withAction:kTouchesBegan atTime:(double)[event timestamp]];
*/
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//ADD TO nodeFromUserTouches
NSSet *movingTouches = [event allTouches];
for (UITouch *touch in movingTouches)
{
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
if(fingerID > 4) {
NSLog(@"shit");
}
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID != -1)
{
[self updateNodeFromUserTouchesPoints:point withID:fingerID andState:YES];
} else
{
fingerID = [touchTracker AddNewTouch:touch];
[self updateNodeFromUserTouchesPoints:point withID:fingerID andState:YES];
continue;
}
if([self nodeFromUserTouches]->nodeToDraw.userNodeIDtoPlayingID[fingerID] == -1)
[self linkTouchForID:fingerID];
}
/*
if (editing)
{
[self linkTouchIDs:movingTouches];
editing = NO;
}
*/
/*
NSSet *movingTouches = [event allTouches];
[owner setCurrentTouches:movingTouches];
float xCentroid = 0;
float yCentroid = 0;
float xSum = 0;
float ySum = 0;
// Compute centroid of each coordinate
for (UITouch *touch in movingTouches)
{
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
if(fingerID > 4) {
NSLog(@"shit");
}
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID != -1)
{
[self updateNodeToDrawPoints:point withID:fingerID andState:YES];
} else
{
int sfsdf = 1;//wasn't on our list?!
fingerID = [touchTracker AddNewTouch:touch];
[self updateNodeToDrawPoints:point withID:fingerID andState:YES];
continue;
}
xSum = xSum + point.x;
ySum = ySum + point.y;
}
[view setNeedsDisplay];
xCentroid = xSum / [[owner nodeToDrawObject] getNumTouches];
yCentroid = ySum / [[owner nodeToDrawObject] getNumTouches];
[pd sendXCentroid:xCentroid toVoiceWithId:kPreviewVoice];
[pd sendYCentroid:yCentroid toVoiceWithId:kPreviewVoice];
if(owner->record == TRUE)
[owner.recorder addTouches:touches withAction:kTouchesMoved atTime:(double)[event timestamp]];
//}
*/
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID != -1)
{
touchTracker->g_touchTracker[fingerID].m_touchPointer = nil;
[self updateNodeFromUserTouchesPoints:point withID:fingerID andState:NO];
[self unlinkTouchForID:fingerID];
} else
{
//nothing
}
}
/*
if (editing)
{
NSSet *beginTouches = [event allTouches];
[self linkTouchIDs:beginTouches];
editing = NO;
}
//ADD TO nodeFromUserTouches
*/
/*
//NSSet *endTouches = [event allTouches];
//numberOfCurrentTouches -= [endTouches count];
//owner->numberOfCurrentTouches -= [touches count];
[owner setCurrentTouches:[event allTouches]];
[[owner currentTouches] minusSet:touches]; //doesn't work - using numberOfCurrentTouches to check if record pressed while fingers already down
[[owner nodeToDrawObject] setNumTouches:[[owner nodeToDrawObject] getNumTouches] - [touches count]];
[pd numberOfTouches:[[owner nodeToDrawObject] getNumTouches] toVoiceWithId:kPreviewVoice];
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID != -1)
{
touchTracker->g_touchTracker[fingerID].m_touchPointer = nil;
[self updateNodeToDrawPoints:point withID:fingerID andState:NO];
} else
{
//wasn't on our list
int sdfdsfdsfdsf = 0;
continue;
}
}
[view setNeedsDisplay];
if (owner->record == TRUE)
[owner.recorder addTouches:touches withAction:kTouchesEnded atTime:(double)[event timestamp]];
*/
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//this is probably what caused our crash in the beta demo
/*
//owner->nodeToDraw.numTouches -= [touches count];
//[pd numberOfTouches:(float)owner->nodeToDraw.numTouches toVoiceWithId:kPreviewVoice];
[[owner nodeToDraw] setNumTouches:[[owner nodeToDrawObject] getNumTouches] - [touches count]];
//[pd numberOfTouches:[[owner nodeToDrawObject] numTouches] toVoiceWithId:kPreviewVoice];
[pd numberOfTouches:[[owner nodeToDrawObject] getNumTouches] toVoiceWithId:kPreviewVoice];
// Enumerate through all the touch objects.
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = [touchTracker GetFingerTrackIDByTouch:touch];
CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
if (fingerID != -1)
{
touchTracker->g_touchTracker[fingerID].m_touchPointer = nil;
[self updateNodeToDrawPoints:point withID:fingerID andState:NO];
} else
{
//wasn't on our list
continue;
}
}
[view updateDrawing:YES];
*/
}
@end