-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuioPlugIn.m
295 lines (251 loc) · 11 KB
/
TuioPlugIn.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
/*
TUIO Quartz Composer Plugin - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2016 Martin Kaltenbrunner <martin@tuio.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is
requested to send the modifications to the original developer so that
they can be incorporated into the canonical version.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//#import <OpenGL/CGLMacro.h>
#import "TuioPlugIn.h"
#import "QuartzTuioClient.h"
#define kQCPlugIn_Name @"TuioClient"
#define kQCPlugIn_Description @"receives and decodes TUIO 1.1 messages"
@implementation TuioPlugIn
/* We need to declare the input / output properties as dynamic as Quartz Composer will handle their implementation */
@dynamic outputObjects, outputCursors, outputBlobs;
+ (NSDictionary*) attributes
{
/* Return the attributes of this plug-in */
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
/* Return the attributes for the plug-in property ports */
if([key isEqualToString:@"outputObjects"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"TuioObjects", QCPortAttributeNameKey, nil];
if([key isEqualToString:@"outputCursors"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"TuioCursors", QCPortAttributeNameKey, nil];
if([key isEqualToString:@"outputBlobs"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"TuioBlobs", QCPortAttributeNameKey, nil];
return nil;
}
+ (QCPlugInExecutionMode) executionMode
{
/* This plug-in is a provider (it provides data from an external source) */
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
/* This plug-in does not depend on the time (time parameter is completely ignored in the -execute:atTime:withArguments: method) */
return kQCPlugInTimeModeIdle;
}
+ (NSArray*) plugInKeys
{
/* Return the list of KVC keys corresponding to our internal settings */
return [NSArray arrayWithObject:@"udpPort"];
}
- (void)addTuioObject:(int)s_id :(int)c_id :(float)xpos :(float)ypos :(float)angle
{
@synchronized(lockObjectList) {
id POOL = [[NSAutoreleasePool alloc] init];
[objectList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: c_id], @"c_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: angle], @"angle",
[NSNumber numberWithDouble: 0.0f ], @"xspeed",
[NSNumber numberWithDouble: 0.0f], @"yspeed",
[NSNumber numberWithDouble: 0.0f], @"rspeed",
[NSNumber numberWithDouble: 0.0f], @"maccel",
[NSNumber numberWithDouble: 0.0f], @"raccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)updateTuioObject:(int)s_id :(int)c_id :(float)xpos :(float)ypos :(float)angle :(float)xspeed :(float)yspeed :(float)rspeed :(float)maccel :(float)raccel
{
@synchronized(lockObjectList) {
id POOL = [[NSAutoreleasePool alloc] init];
[objectList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: c_id], @"c_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: angle], @"angle",
[NSNumber numberWithDouble: xspeed], @"xspeed",
[NSNumber numberWithDouble: yspeed], @"yspeed",
[NSNumber numberWithDouble: rspeed], @"rspeed",
[NSNumber numberWithDouble: maccel], @"maccel",
[NSNumber numberWithDouble: raccel], @"raccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)removeTuioObject:(int)s_id :(int)c_id
{
@synchronized(lockObjectList) {
id POOL = [[NSAutoreleasePool alloc] init];
[objectList removeObjectForKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)addTuioCursor:(int)s_id :(int)c_id :(float)xpos :(float)ypos
{
@synchronized(lockCursorList) {
id POOL = [[NSAutoreleasePool alloc] init];
[cursorList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: c_id], @"c_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: 0.0f], @"xspeed",
[NSNumber numberWithDouble: 0.0f], @"yspeed",
[NSNumber numberWithDouble: 0.0f], @"maccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)updateTuioCursor:(int)s_id :(int)c_id :(float)xpos :(float)ypos :(float)xspeed :(float)yspeed :(float)maccel
{
@synchronized(lockCursorList) {
id POOL = [[NSAutoreleasePool alloc] init];
[cursorList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: c_id], @"c_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: xspeed], @"xspeed",
[NSNumber numberWithDouble: yspeed], @"yspeed",
[NSNumber numberWithDouble: maccel], @"maccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)removeTuioCursor:(int)s_id :(int)c_id
{
@synchronized(lockCursorList) {
id POOL = [[NSAutoreleasePool alloc] init];
[cursorList removeObjectForKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)addTuioBlob:(int)s_id :(int)b_id :(float)xpos :(float)ypos :(float)angle :(float)width :(float)height :(float)area
{
@synchronized(lockBlobList) {
id POOL = [[NSAutoreleasePool alloc] init];
[blobList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: b_id], @"b_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: angle], @"angle",
[NSNumber numberWithDouble: width], @"width",
[NSNumber numberWithDouble: height], @"height",
[NSNumber numberWithDouble: area], @"area",
[NSNumber numberWithDouble: 0.0f ], @"xspeed",
[NSNumber numberWithDouble: 0.0f], @"yspeed",
[NSNumber numberWithDouble: 0.0f], @"rspeed",
[NSNumber numberWithDouble: 0.0f], @"maccel",
[NSNumber numberWithDouble: 0.0f], @"raccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)updateTuioBlob:(int)s_id :(int)b_id :(float)xpos :(float)ypos :(float)angle :(float)width :(float)height :(float)area :(float)xspeed :(float)yspeed :(float)rspeed :(float)maccel :(float)raccel
{
@synchronized(lockBlobList) {
id POOL = [[NSAutoreleasePool alloc] init];
[blobList setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: s_id], @"s_id",
[NSNumber numberWithInt: b_id], @"b_id",
[NSNumber numberWithDouble: (xpos*2.0f-1.0f)], @"xpos",
[NSNumber numberWithDouble: ratio*((1.0f-ypos)*2.0f-1.0f)], @"ypos",
[NSNumber numberWithDouble: angle], @"angle",
[NSNumber numberWithDouble: width], @"width",
[NSNumber numberWithDouble: height], @"height",
[NSNumber numberWithDouble: area], @"area",
[NSNumber numberWithDouble: xspeed], @"xspeed",
[NSNumber numberWithDouble: yspeed], @"yspeed",
[NSNumber numberWithDouble: rspeed], @"rspeed",
[NSNumber numberWithDouble: maccel], @"maccel",
[NSNumber numberWithDouble: raccel], @"raccel", nil] forKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (void)removeTuioBlob:(int)s_id :(int)b_id
{
@synchronized(lockBlobList) {
id POOL = [[NSAutoreleasePool alloc] init];
[blobList removeObjectForKey:[NSNumber numberWithInt: s_id]];
[POOL release];
}
}
- (QCPlugInViewController*) createViewController
{
/* Create a QCPlugInViewController to handle the user-interface to edit the our internal settings */
return [[QCPlugInViewController alloc] initWithPlugIn:self viewNibName:@"TuioClientSettings"];
}
- (BOOL) validateUdpPort:(id*)ioValue error:(NSError**)outError
{
/* Make sure the "udpPort" new value is valid */
if([(NSNumber*)*ioValue integerValue] < 1024)
*ioValue = [NSNumber numberWithUnsignedInteger:3333];
return YES;
}
- (void) setUdpPort:(NSUInteger)newPort
{
_udpPort = newPort;
}
- (NSUInteger) udpPort
{
return _udpPort;
}
@end
@implementation TuioPlugIn (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
ratio = 1.0f;
objectList = [[NSMutableDictionary alloc] init];
cursorList = [[NSMutableDictionary alloc] init];
blobList = [[NSMutableDictionary alloc] init];
lockObjectList=[NSNumber numberWithInt: 1];
lockCursorList=[NSNumber numberWithInt: 1];
lockBlobList=[NSNumber numberWithInt: 1];
_client = [[QuartzTuioClient alloc] init:_udpPort];
[(QuartzTuioClient*)_client addTuioListener:self];
[(QuartzTuioClient*)_client start];
return YES;
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
/* This method is called by Quartz Composer whenever the plug-in needs to recompute its result: retrieve the input string and compute the output string */
NSRect dim = [context bounds];
ratio = (float)dim.size.height/(float)dim.size.width;
@synchronized(lockObjectList) {
self.outputObjects = [objectList allValues];
}
@synchronized(lockCursorList){
self.outputCursors = [cursorList allValues];
}
@synchronized(lockBlobList){
self.outputBlobs = [blobList allValues];
}
return YES;
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
[(QuartzTuioClient*)_client stop];
[(QuartzTuioClient*)_client removeTuioListener];
[(QuartzTuioClient*)_client dealloc];
[objectList dealloc];
[cursorList dealloc];
[blobList dealloc];
}
@end