root / ofxTouchAPI / src / ofxTouchAPI_IO.cpp @ 14

View | Annotate | Download (12.1 KB)

1
/***********************************************************************
2
 
3
 Copyright (c) 2009 Dimitri Diakopoulos, http://www.dimitridiakopoulos.com/
4
 === Google Summer of Code 2009 - NUI Group === 
5
6
 Portions Copyright (c) 2008, 2009 Memo Atkens, http://www.memo.tv/
7
 -> Based on ofxMSAInteractiveObject
8
9
        Redistribution and use in source and binary forms, with or without modification, 
10
        are permitted provided that the following conditions are met:
11
12
        1. Redistributions of source code must retain the above copyright notice, 
13
        this list of conditions and the following disclaimer.
14
15
        2. Redistributions in binary form must reproduce the above copyright notice, 
16
        this list of conditions and the following disclaimer in the documentation and/or 
17
        other materials provided with the distribution.
18
19
        3. The name of the author may not be used to endorse or promote products derived 
20
        from this software without specific prior written permission.
21
22
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
23
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
24
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
25
        ARE DISCLAIMED. IN NOEVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
26
        INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
27
        (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
28
        LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
29
        AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
31
        OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
32
33
*************************************************************************/ 
34
35
#include "ofMain.h"
36
#include "ofxTouchAPI_IO.h"
37
38
// ================================================================= CTor
39
ofxTouchAPI_IO::ofxTouchAPI_IO() {
40
41
        _mouseOver        = false;
42
        _mouseDown        = false;
43
44
        enabled                = true;
45
        verbose                = false;
46
        
47
        enableAllEvents(); 
48
}
49
50
// ================================================================= DTor
51
ofxTouchAPI_IO::~ofxTouchAPI_IO() {
52
        disableAllEvents();
53
}
54
55
void ofxTouchAPI_IO::killMe() {
56
        delete this;
57
}
58
59
60
// ================================================================= Events Enablers/Disablers
61
void ofxTouchAPI_IO::enableAllEvents() {
62
        enableMouseEvents();
63
        enableKeyEvents();
64
        enableAppEvents();
65
        enableTouchEvents(); 
66
}
67
68
void ofxTouchAPI_IO::disableAllEvents() {
69
        disableMouseEvents();
70
        disableKeyEvents();
71
        disableAppEvents();
72
        disableTouchEvents(); 
73
}
74
75
76
void ofxTouchAPI_IO::enableMouseEvents() {
77
        ofAddListener(ofEvents.mousePressed, this, &ofxTouchAPI_IO::_mousePressed);
78
        ofAddListener(ofEvents.mouseMoved, this, &ofxTouchAPI_IO::_mouseMoved);
79
        ofAddListener(ofEvents.mouseDragged, this, &ofxTouchAPI_IO::_mouseDragged);
80
        ofAddListener(ofEvents.mouseReleased, this, &ofxTouchAPI_IO::_mouseReleased);
81
}
82
83
void ofxTouchAPI_IO::disableMouseEvents() {
84
        ofRemoveListener(ofEvents.mousePressed, this, &ofxTouchAPI_IO::_mousePressed);
85
        ofRemoveListener(ofEvents.mouseMoved, this, &ofxTouchAPI_IO::_mouseMoved);
86
        ofRemoveListener(ofEvents.mouseDragged, this, &ofxTouchAPI_IO::_mouseDragged);
87
        ofRemoveListener(ofEvents.mouseReleased, this, &ofxTouchAPI_IO::_mouseReleased);
88
}
89
90
void ofxTouchAPI_IO::enableKeyEvents() {
91
        ofAddListener(ofEvents.keyPressed, this, &ofxTouchAPI_IO::_keyPressed);
92
        ofAddListener(ofEvents.keyReleased, this, &ofxTouchAPI_IO::_keyReleased);
93
}
94
95
void ofxTouchAPI_IO::disableKeyEvents() {
96
        ofRemoveListener(ofEvents.keyPressed, this, &ofxTouchAPI_IO::_keyPressed);
97
        ofRemoveListener(ofEvents.keyReleased, this, &ofxTouchAPI_IO::_keyReleased);
98
}
99
100
void ofxTouchAPI_IO::enableAppEvents() {
101
        ofAddListener(ofEvents.setup, this, &ofxTouchAPI_IO::_setup);
102
        ofAddListener(ofEvents.update, this, &ofxTouchAPI_IO::_update);
103
        ofAddListener(ofEvents.draw, this, &ofxTouchAPI_IO::_draw);
104
        ofAddListener(ofEvents.exit, this, &ofxTouchAPI_IO::_exit);
105
}
106
107
void ofxTouchAPI_IO::disableAppEvents() {
108
        ofRemoveListener(ofEvents.setup, this, &ofxTouchAPI_IO::_setup);
109
        ofRemoveListener(ofEvents.update, this, &ofxTouchAPI_IO::_update);
110
        ofRemoveListener(ofEvents.draw, this, &ofxTouchAPI_IO::_draw);
111
        ofRemoveListener(ofEvents.exit, this, &ofxTouchAPI_IO::_exit);
112
}
113
114
void ofxTouchAPI_IO::enableTouchEvents(){
115
        ofAddListener(tuio.cursorAdded,this,&ofxTouchAPI_IO::_tuioAdded);
116
        ofAddListener(tuio.cursorRemoved,this,&ofxTouchAPI_IO::_tuioRemoved);
117
        ofAddListener(tuio.cursorUpdated,this,&ofxTouchAPI_IO::_tuioUpdated);
118
}
119
120
void ofxTouchAPI_IO::disableTouchEvents(){
121
        ofRemoveListener(tuio.cursorAdded,this,&ofxTouchAPI_IO::_tuioAdded);
122
        ofRemoveListener(tuio.cursorRemoved,this,&ofxTouchAPI_IO::_tuioRemoved);
123
        ofRemoveListener(tuio.cursorUpdated,this,&ofxTouchAPI_IO::_tuioUpdated);
124
}
125
126
127
// ================================================================= IO Functions
128
void ofxTouchAPI_IO::setPos(float _x, float _y) {
129
        x = _x;
130
        y = _y;
131
}
132
133
void ofxTouchAPI_IO::setSize(float _w, float _h) {
134
        width = _w;
135
        height = _h;
136
}
137
138
void ofxTouchAPI_IO::setPosAndSize(float _x, float _y, float _w, float _h) {
139
        setPos(_x, _y);
140
        setSize(_w, _h);
141
}
142
143
bool ofxTouchAPI_IO::isMouseOver() {
144
        return _mouseOver;
145
}
146
147
bool ofxTouchAPI_IO::isMouseDown() {
148
        return _mouseDown;
149
}
150
151
int ofxTouchAPI_IO::getMouseX() {
152
        return _mouseX;
153
}
154
155
int ofxTouchAPI_IO::getMouseY() {
156
        return _mouseY;
157
}
158
159
int ofxTouchAPI_IO::getTouches(){
160
        return touchList.size(); 
161
}
162
163
bool ofxTouchAPI_IO::isBeingTouched(){
164
        if (touchList.size() >= 1)
165
                return true; 
166
}
167
168
169
int ofxTouchAPI_IO::getLastMouseButton() {
170
        return _mouseButton;
171
}
172
173
void ofxTouchAPI_IO::_setup(ofEventArgs &e) {
174
        if(!enabled) return;
175
176
        setup();
177
}
178
179
void ofxTouchAPI_IO::_update(ofEventArgs &e) {
180
        if(!enabled) return;
181
        
182
        // check to see if object has moved, and if so update mouse events
183
        if(oldRect.x != this->x || oldRect.y != this->y || oldRect.width != this->width ||oldRect.height != this->height) {
184
                ofMouseEventArgs e;
185
                e.button = _mouseButton;
186
                e.x = _mouseX;
187
                e.y = _mouseY;
188
                if(_mouseDown) _mouseDragged(e);        
189
                else _mouseMoved(e);
190
                
191
                oldRect =  (ofRectangle) (*this);
192
        }
193
194
        update();
195
}
196
197
void ofxTouchAPI_IO::_draw(ofEventArgs &e) {
198
        if(!enabled) return;
199
200
        draw();
201
}
202
203
void ofxTouchAPI_IO::_exit(ofEventArgs &e) {
204
        if(!enabled) return;
205
206
        exit();
207
}
208
209
// ================================================================= Events
210
void ofxTouchAPI_IO::_mouseMoved(ofMouseEventArgs &e) {
211
        int x = e.x;
212
        int y = e.y;
213
        int button = e.button;
214
        if(verbose) printf("ofxTouchAPI_IO::_mouseMoved(x: %i, y: %i)\n", x, y);
215
        if(!enabled) return;
216
        
217
        _mouseX = x;
218
        _mouseY = y;
219
        
220
        // If the mouse is over the object
221
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
222
                if (!_mouseOver) {                                // if wasn't over previous frame
223
                        onRollOver(x, y);                        // call onRollOver
224
                        _mouseOver = true;                        // update flag
225
                }
226
                onMouseMove(x, y);                                // and trigger onMouseMove
227
        } 
228
229
        // If the mouse is not over the object (but the flag is still true from previous frame)
230
        else if (_mouseOver) {                        
231
                onRollOut();                                        // call onRollOut
232
                _mouseOver = false;                                // update flag
233
        }
234
}
235
236
237
void ofxTouchAPI_IO::_mousePressed(ofMouseEventArgs &e) {
238
        int x = e.x;
239
        int y = e.y;
240
        int button = e.button;
241
        
242
        if(verbose) printf("ofxTouchAPI_IO::_mousePressed(x: %i, y: %i, button: %i)\n", x, y, button);
243
        if(!enabled) return;
244
245
        _mouseX = x;
246
        _mouseY = y;
247
        _mouseButton = button;
248
        
249
        // If the mouse is over the object
250
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
251
252
                if(!_mouseDown) {                                        // if wasn't down previous frame
253
                        onPress(x, y, button);                        // call onPress
254
                        _mouseDown = true;                                // update flag
255
                }
256
        } 
257
        
258
        // If the mouse is not over the object... do nothing. 
259
        else {}
260
}
261
262
void ofxTouchAPI_IO::_mouseDragged(ofMouseEventArgs &e) {
263
        int x = e.x;
264
        int y = e.y;
265
        int button = e.button;
266
        
267
        if(verbose) printf("ofxTouchAPI_IO::_mouseDragged(x: %i, y: %i, button: %i)\n", x, y, button);
268
        if(!enabled) return;
269
270
        _mouseX = x;
271
        _mouseY = y;
272
        _mouseButton = button;
273
274
        // If the mouse is over the object
275
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
276
                if(!_mouseOver) {                                                // if wasn't over previous frame
277
                        //onPress(x, y);                                        // call onPress - alternate behavior
278
                        _mouseOver = true;                                        // update flag
279
                }
280
                onDragOver(x, y, button);                                // and trigger onDragOver
281
        } 
282
        
283
        else {
284
                if(_mouseOver) {                                                // if mouse is not over the object, but the flag is true (From previous frame)
285
                        onRollOut();                                                // call onRollOut
286
                        _mouseOver = false;                                        // update flag
287
                }
288
                if(_mouseDown) {
289
                        onDragOutside(x, y, button);
290
                }
291
        }
292
}
293
294
void ofxTouchAPI_IO::_mouseReleased(ofMouseEventArgs &e) {
295
        int x = e.x;
296
        int y = e.y;
297
        int button = e.button;
298
        
299
        if(verbose) printf("ofxTouchAPI_IO::_mouseReleased(x: %i, y: %i, button: %i)\n", x, y, button);
300
        if(!enabled) return;
301
302
        _mouseX = x;
303
        _mouseY = y;
304
        _mouseButton = button;
305
        
306
        // If the mouse is over the object
307
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
308
                onRelease(x, y, button);
309
        } 
310
311
        else {
312
                if(_mouseDown) onReleaseOutside(x, y, button);
313
        }
314
315
        _mouseDown = false;
316
}
317
318
319
void ofxTouchAPI_IO::_keyPressed(ofKeyEventArgs &e) {
320
321
        int key = e.key;
322
323
        if(verbose) printf("ofxTouchAPI_IO::_keyPressed(key: %i)\n", key);
324
325
        if(!enabled) return;
326
        keyPressed(key);
327
}
328
329
void ofxTouchAPI_IO::_keyReleased(ofKeyEventArgs &e) {
330
331
        int key = e.key;        
332
333
        if(verbose) printf("ofxTouchAPI_IO::_keyReleased(key: %i)\n", key);
334
335
        if(!enabled) return;
336
        keyReleased(key);
337
}
338
339
void ofxTouchAPI_IO::_tuioAdded(ofxTuioCursor &tuioCursor){
340
341
        int x = tuioCursor.getX() * ofGetWidth();
342
        int y = tuioCursor.getY() * ofGetHeight(); 
343
        int ID = tuioCursor.getFingerId();
344
345
        if(verbose) printf("ofxTouchAPI_IO::_tuioAdded(x: %i, y: %i)\n", x, y);
346
347
        if(!enabled) return;
348
349
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
350
            onTouchDown(x, y, ID);        
351
                touchList.push_back(ID);
352
        }
353
354
}
355
356
void ofxTouchAPI_IO::_tuioRemoved(ofxTuioCursor &tuioCursor){
357
        
358
        int x = tuioCursor.getX() * ofGetWidth();
359
        int y = tuioCursor.getY() * ofGetHeight(); 
360
        int ID = tuioCursor.getFingerId();
361
362
        if(verbose) printf("ofxTouchAPI_IO::_tuioRemoved(x: %i, y: %i)\n", x, y);
363
364
        if(!enabled) return;
365
366
        // If touch is inside object when it's removed
367
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
368
                if(touchList.size() > 0){
369
                        onTouchUp(x, y, ID);
370
                }
371
        } 
372
        
373
        // If touch is outside the object when it's removed
374
        else {
375
376
                onTouchUpOutside(x, y, ID); 
377
378
                std::list<int>::iterator touchid;
379
                touchid = find(touchList.begin(), touchList.end(), ID);
380
                if(touchid != touchList.end()) touchList.remove(ID);
381
        }
382
383
}
384
385
void ofxTouchAPI_IO::_tuioUpdated(ofxTuioCursor &tuioCursor){
386
        
387
        int x = tuioCursor.getX() * ofGetWidth();
388
        int y = tuioCursor.getY() * ofGetHeight(); 
389
        int ID = tuioCursor.getFingerId();
390
391
        if(verbose) printf("Number of Touches: %i \n", getTouches());  
392
        if(verbose) printf("ofxTouchAPI_IO::_tuioUpdated(x: %i, y: %i, ID: %i)\n", x, y, ID);
393
394
        if(!enabled) return;
395
396
        // If touch is moving inside an object
397
        if(HitTest::rectangle(x, y, this->width, this->height, this->x, this->y)) {
398
399
                if(touchList.size()){
400
                        std::list<int>::iterator touchid;
401
                        touchid = find(touchList.begin(), touchList.end(), ID);
402
                        if(touchid==touchList.end()) touchList.push_back(ID);
403
                        
404
                        onTouchMove(x, y, ID);                
405
                } else {
406
                        onTouchMoveOver(x, y, ID);
407
                        touchList.push_back(ID);
408
                }
409
        } 
410
        
411
        // If the touch moves outside an object
412
        else {
413
                if(touchList.size()){
414
                        if(touchList.size() <= 1){
415
                                std::list<int>::iterator touchid;
416
                                touchid = find(touchList.begin(), touchList.end(), ID);
417
                                if(touchid!=touchList.end()){
418
                                        onTouchMoveOutside(x, y, ID);
419
                                        touchList.remove(ID);
420
                                }
421
                        } else {
422
                                std::list<int>::iterator touchid;
423
                                touchid = find(touchList.begin(), touchList.end(), ID);
424
                                if(touchid!=touchList.end()) touchList.remove(ID);
425
                        }
426
                }
427
        }
428
}