root / trunk / tbeta / Windows / addons / ofxTBeta / TBetaBase.cpp @ 66

View | Annotate | Download (23.5 KB)

1
/*
2
 *  TBetaBase.cpp
3
 *  tbeta
4
 *
5
 *  Created by Artem Titoulenko on 2/1/09.
6
 *  Copyright 2009 NUI Inc.. All rights reserved.
7
 *
8
 */
9
10
#include "TBetaBase.h"
11
#include "gui.h"
12
13
/******************************************************************************
14
 * The setup function is run once to perform initializations in the application
15
 *****************************************************************************/
16
void TBetaBase::setup()
17
{
18
    //set the title to 'tbeta'
19
    ofSetWindowTitle("tbeta");
20
21
    //removes the 'x' button on windows which causes a crash due to a GLUT bug
22
#ifdef TARGET_WIN32
23
        HWND hwndConsole = FindWindowA(NULL, "tbeta");
24
        HMENU hMnu = ::GetSystemMenu(hwndConsole, FALSE);
25
        RemoveMenu(hMnu, SC_CLOSE, MF_BYCOMMAND);
26
#endif
27
28
        ofSetBackgroundAuto(false);
29
30
    //create filter
31
    if( filter == NULL ) {
32
        filter = new ProcessFilters();
33
    }
34
35
        //Load Settings from config.xml file
36
        loadXMLSettings();
37
38
        //Setup Window Properties
39
        ofSetWindowShape(winWidth,winHeight);
40
        ofSetFrameRate(camRate * 1.4);                        //This will be based on camera fps in the future
41
        ofSetVerticalSync(false);                    //Set vertical sync to false for better performance?
42
43
        //Pick the Source - camera or video
44
    if(bcamera)
45
    {
46
        activeInput = true;
47
        vidGrabber.listDevices();
48
        vidGrabber.setDeviceID(deviceID);
49
        vidGrabber.setVerbose(true);
50
        vidGrabber.initGrabber(camWidth,camHeight);
51
        int grabW = vidGrabber.width;
52
        int grabH = vidGrabber.height;
53
        printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, grabW, grabH);
54
    }
55
    else
56
    {
57
        activeInput = true;
58
        vidPlayer.loadMovie("test_videos/" + videoFileName);
59
        vidPlayer.play();
60
        printf("Video Mode\n");
61
        camHeight = vidPlayer.height;
62
        camWidth = vidPlayer.width;
63
    }
64
65
        /*****************************************************************************************************
66
         * Allocate images (needed for drawing/processing images) ----Most of These won't be needed in the end
67
         ******************************************************************************************************/
68
        processedImg.allocate(camWidth, camHeight); //main Image that'll be processed.
69
        processedImg.setUseTexture(false);
70
        sourceImg.allocate(camWidth, camHeight);    //Source Image
71
        sourceImg.setUseTexture(false);                                //We don't need to draw this so don't create a texture
72
        /******************************************************************************************************/
73
74
        //Fonts - Is there a way to dynamically change font size?
75
        verdana.loadFont("verdana.ttf", 8, true, true);           //Font used for small images
76
        sidebarTXT.loadFont("verdana.ttf", 8, true, true);
77
        bigvideo.loadFont("verdana.ttf", 13, true, true);  //Font used for big images.
78
79
        //Static Images
80
        background.loadImage("images/background.jpg"); //Main (Temp?) Background
81
82
        gui = ofxGui::Instance(this);
83
        setupGUI();
84
85
    calib.setup(camWidth, camHeight, &tracker);
86
87
    filter->allocate( camWidth, camHeight );
88
89
        printf("Tbeta is setup!\n\n");
90
}
91
92
/****************************************************************
93
 *        Load/Save config.xml file Settings
94
 ****************************************************************/
95
void TBetaBase::loadXMLSettings(){
96
97
        // TODO: a seperate XML to map keyboard commands to action
98
        message = "Loading config.xml...";
99
        // Can this load via http?
100
        if( XML.loadFile("config.xml")){
101
                message = "Settings Loaded!";
102
        }else{
103
                //FAIL!
104
                message = "No Settings Found...";
105
        }
106
        //--------------------------------------------------------------
107
        //  START BINDING XML TO VARS
108
        //--------------------------------------------------------------
109
        //frameRate                        = XML.getValue("CONFIG:APPLICATION:FRAMERATE",0);
110
111
        winWidth                        = XML.getValue("CONFIG:WINDOW:WIDTH",0);
112
        winHeight                        = XML.getValue("CONFIG:WINDOW:HEIGHT",0);
113
        minWidth                        = XML.getValue("CONFIG:WINDOW:MINX",0);
114
        minHeight                        = XML.getValue("CONFIG:WINDOW:MINY",0);
115
116
        bcamera                                = XML.getValue("CONFIG:CAMERA_0:USECAMERA",0);
117
        deviceID                        = XML.getValue("CONFIG:CAMERA_0:DEVICE",0);
118
        camWidth                        = XML.getValue("CONFIG:CAMERA_0:WIDTH",0);
119
        camHeight                        = XML.getValue("CONFIG:CAMERA_0:HEIGHT",0);
120
        camRate                                = XML.getValue("CONFIG:CAMERA_0:FRAMERATE",0);
121
122
        videoFileName                = XML.getValue("CONFIG:VIDEO:FILENAME", "RearDI.m4v");
123
124
        maxBlobs                        = XML.getValue("CONFIG:BLOBS:MAXNUMBER", 20);
125
126
        bShowLabels                        = XML.getValue("CONFIG:BOOLEAN:LABELS",0);
127
        bFastMode                        = XML.getValue("CONFIG:BOOLEAN:FAST",0);
128
        bDrawOutlines                = XML.getValue("CONFIG:BOOLEAN:OUTLINES",0);
129
130
        filter->bLearnBakground                = XML.getValue("CONFIG:BOOLEAN:LEARNBG",0);
131
        filter->bVerticalMirror                = XML.getValue("CONFIG:BOOLEAN:VMIRROR",0);
132
        filter->bHorizontalMirror        = XML.getValue("CONFIG:BOOLEAN:HMIRROR",0);
133
134
        //Filters
135
        filter->bHighpass                        = XML.getValue("CONFIG:BOOLEAN:HIGHPASS",1);
136
        filter->bAmplify                        = XML.getValue("CONFIG:BOOLEAN:AMPLIFY", 1);
137
        filter->bSmooth                                = XML.getValue("CONFIG:BOOLEAN:SMOOTH", 1);
138
        filter->bDynamicBG                        = XML.getValue("CONFIG:BOOLEAN:DYNAMICBG", 1);
139
140
        //GPU
141
        bGPUMode                        = XML.getValue("CONFIG:BOOLEAN:GPU", 0);
142
143
        //Filter Settings
144
        filter->threshold                        = XML.getValue("CONFIG:INT:THRESHOLD",0);
145
        filter->highpassBlur                = XML.getValue("CONFIG:INT:HIGHPASSBLUR",0);
146
        filter->highpassNoise                = XML.getValue("CONFIG:INT:HIGHPASSNOISE",0);
147
        filter->highpassAmp                        = XML.getValue("CONFIG:INT:HIGHPASSAMP",0);
148
        filter->smooth                                = XML.getValue("CONFIG:INT:SMOOTH",0);
149
150
        //--------------------------------------------------- TODO XML NETWORK SETTINGS
151
        bTUIOMode                        = XML.getValue("CONFIG:BOOLEAN:TUIO",0);
152
        tmpLocalHost                = XML.getValue("CONFIG:NETWORK:LOCALHOST", "localhost");
153
        tmpPort                                = XML.getValue("CONFIG:NETWORK:TUIOPORT_OUT", 3333);
154
        myTUIO.setup(tmpLocalHost.c_str(), tmpPort); //have to convert tmpLocalHost to a const char*
155
        //--------------------------------------------------------------
156
        //  END XML SETUP
157
}
158
159
void TBetaBase::saveConfiguration()
160
{
161
        XML.setValue("CONFIG:CAMERA_0:USECAMERA", bcamera);
162
        XML.setValue("CONFIG:CAMERA_0:DEVICE", deviceID);
163
        XML.setValue("CONFIG:CAMERA_0:WIDTH", camWidth);
164
        XML.setValue("CONFIG:CAMERA_0:HEIGHT", camHeight);
165
        XML.setValue("CONFIG:CAMERA_0:FRAMERATE", camRate);
166
167
        XML.setValue("CONFIG:BOOLEAN:PRESSURE",bShowPressure);
168
        XML.setValue("CONFIG:BOOLEAN:LABELS",bShowLabels);
169
        XML.setValue("CONFIG:BOOLEAN:OUTLINES",bDrawOutlines);
170
        XML.setValue("CONFIG:BOOLEAN:LEARNBG", filter->bLearnBakground);
171
        XML.setValue("CONFIG:BOOLEAN:TUIO",bTUIOMode);
172
        XML.setValue("CONFIG:BOOLEAN:VMIRROR", filter->bVerticalMirror);
173
        XML.setValue("CONFIG:BOOLEAN:HMIRROR", filter->bHorizontalMirror);
174
175
        XML.setValue("CONFIG:BOOLEAN:HIGHPASS", filter->bHighpass);
176
        XML.setValue("CONFIG:BOOLEAN:AMPLIFY", filter->bAmplify);
177
        XML.setValue("CONFIG:BOOLEAN:SMOOTH", filter->bSmooth);
178
        XML.setValue("CONFIG:BOOLEAN:DYNAMICBG", filter->bDynamicBG);
179
180
        XML.setValue("CONFIG:BOOLEAN:GPU", bGPUMode);
181
182
        XML.setValue("CONFIG:INT:THRESHOLD", filter->threshold);
183
        XML.setValue("CONFIG:INT:HIGHPASSBLUR", filter->highpassBlur);
184
        XML.setValue("CONFIG:INT:HIGHPASSNOISE", filter->highpassNoise);
185
        XML.setValue("CONFIG:INT:HIGHPASSAMP", filter->highpassAmp);
186
        XML.setValue("CONFIG:INT:SMOOTH", filter->smooth);
187
188
        //        XML.setValue("CONFIG:NETWORK:LOCALHOST", *myTUIO.localHost);
189
        //        XML.setValue("CONFIG:NETWORK:TUIO_PORT_OUT",myTUIO.TUIOPort);
190
191
        XML.saveFile("config.xml");
192
}
193
194
/******************************************************************************
195
 * The update function runs continuously. Use it to update states and variables
196
 *****************************************************************************/
197
void TBetaBase::update()
198
{
199
    bNewFrame = false;
200
201
        if(activeInput){
202
203
                if(bcamera){
204
                        vidGrabber.grabFrame();
205
                        bNewFrame = vidGrabber.isFrameNew();
206
                }
207
                else{
208
                        vidPlayer.idleMovie();
209
                        bNewFrame = vidPlayer.isFrameNew();
210
                }
211
212
                if (bNewFrame)
213
                {
214
                        ofBackground(0, 0, 0);
215
216
                        //Calculate FPS of Camera
217
                        frames++;
218
                        float time = ofGetElapsedTimeMillis();
219
                        if(time > (lastFPSlog + 1000)){
220
                                fps = frames;
221
                                frames = 0;
222
                                lastFPSlog = time;
223
                        }//End calculation
224
225
                        float beforeTime = ofGetElapsedTimeMillis();
226
227
                        if(bGPUMode){
228
                                grabFrameToGPU(filter->gpuSourceTex);
229
                                filter->applyGPUFilters();
230
                                contourFinder.findContours(filter->gpuReadBackImageGS, 1, (camWidth*camHeight)/25, maxBlobs, false);
231
                        }
232
                        else{
233
                                grabFrameToCPU();
234
                filter->applyCPUFilters( processedImg );
235
                                contourFinder.findContours(processedImg, 1, (camWidth*camHeight)/25, maxBlobs, false);
236
                        }
237
238
                        //Track found contours/blobss
239
                        tracker.track(&contourFinder);
240
                        //get DSP time
241
                        differenceTime = ofGetElapsedTimeMillis() - beforeTime;
242
243
            //Dynamic Background subtraction LearRate
244
                        if(filter->bDynamicBG){
245
                                filter->fLearnRate = 0.01f; //If there are no blobs, add the background faster.
246
                                if(contourFinder.nBlobs > 0){ //If there ARE blobs, add the background slower.
247
                                        filter->fLearnRate = 0.0003f;
248
                                }
249
                        }//End Background Learning rate
250
251
                        if(bTUIOMode){
252
                                //We're not using frameseq right now with OSC
253
                                //myTUIO.update();
254
255
                                //Start sending OSC
256
                                myTUIO.sendOSC();
257
                        }
258
                }
259
        }
260
}
261
262
/************************************************
263
 *                                Frame Grab
264
 ************************************************/
265
266
//Grab frame from CPU
267
void TBetaBase::grabFrameToCPU(){
268
        //Set sourceImg as new camera/video frame
269
        if(bcamera)
270
                sourceImg.setFromPixels(vidGrabber.getPixels(), camWidth, camHeight);
271
        else
272
                sourceImg.setFromPixels(vidPlayer.getPixels(),         camWidth, camHeight);
273
274
    //convert to grayscale
275
    processedImg = sourceImg;
276
}
277
278
//Grab frame from GPU
279
void TBetaBase::grabFrameToGPU(GLuint target){
280
        //grab the frame to a raw openGL texture
281
        if(bcamera)
282
        {
283
                glEnable(GL_TEXTURE_2D);
284
                //glPixelStorei(1);
285
                glBindTexture(GL_TEXTURE_2D, target);
286
                //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, camWidth, camHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, vidGrabber.getPixels());
287
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidGrabber.getPixels());
288
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290
                glBindTexture(GL_TEXTURE_2D,0);
291
        }
292
        else{
293
                glEnable(GL_TEXTURE_2D);
294
                glBindTexture(GL_TEXTURE_2D, target);
295
                //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, camWidth, camHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, vidPlayer.getPixels());
296
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidPlayer.getPixels());
297
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
298
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
299
                glBindTexture(GL_TEXTURE_2D,0);
300
        }
301
}
302
303
304
/******************************************************************************
305
 * The draw function paints the textures onto the screen. It runs after update.
306
 *****************************************************************************/
307
void TBetaBase::draw(){
308
309
    if(showConfiguration) {
310
311
        //temporary. only auto draw if in fullscreen
312
        if(bNewFrame && bFullscreen == false){drawToScreen();}
313
        else{drawToScreen();}
314
315
        if(!bCalibration)
316
        gui->draw();
317
318
                //draw link to tbeta website
319
                ofSetColor(180, 220, 180, 180);
320
                ofFill();
321
                ofRect(ofGetWidth() - 230,ofGetHeight() - 14, 230, 14);
322
                ofSetColor(0x000000);
323
                ofDrawBitmapString("|  ~  |tbeta.nuigroup.com", ofGetWidth() - 230, ofGetHeight() - 2);
324
    }
325
}
326
327
void TBetaBase::drawToScreen(){
328
329
    /*********************************
330
         * IF CALIBRATING
331
         *********************************/
332
    if(bCalibration)
333
    {
334
        //Don't draw main interface
335
        bShowInterface = false;
336
                calib.passInContourFinder(contourFinder.nBlobs, contourFinder.blobs);
337
        calib.doCalibration();
338
    }
339
340
    /********************************
341
         * IF SHOWING MAIN INTERFACE STUFF
342
         ********************************/
343
    if(bDrawVideo && bShowInterface && !bFastMode)
344
    {
345
        ofSetColor(0xFFFFFF);
346
        //Draw Everything
347
        background.draw(0, 0);
348
349
        //Draw arrows
350
        ofSetColor(187, 200, 203);
351
        ofFill();
352
        ofTriangle(680, 420, 680, 460, 700, 440);
353
        ofTriangle(70, 420, 70, 460, 50, 440);
354
        ofSetColor(255, 255, 0);
355
                //                ofNoFill();
356
                //                ofTriangle(70, 420, 70, 460, 50, 440);
357
358
        ofSetColor(0xFFFFFF);
359
360
        if(bGPUMode) filter->drawGPU();
361
        else filter->draw();
362
363
        ofSetColor(0x000000);
364
        if(bShowPressure){bigvideo.drawString("Pressure Map", 140, 20);}
365
        else             {bigvideo.drawString("Source Image", 140, 20);}
366
                bigvideo.drawString("Tracked Image", 475, 20);
367
    }
368
369
    if(bFastMode)
370
    {
371
        //Display Application information in bottom right
372
        string str = "DSP Milliseconds: ";
373
        str+= ofToString(differenceTime, 0)+"\n\n";
374
375
        if(bcamera)
376
        {
377
            string str2 = "Camera Res:     ";
378
            str2+= ofToString(vidGrabber.width, 0) + " x " + ofToString(vidGrabber.height, 0)  + "\n";
379
            string str4 = "Camera FPS:     ";
380
            str4+= ofToString(fps, 0)+"\n\n";
381
            string str5 = "Blob Count:       ";
382
            str5+= ofToString(contourFinder.nBlobs, 0)+"\n";
383
            ofSetColor(0xFFFFFF);
384
            sidebarTXT.drawString(str + str2 + str4 + str5, 10, 30);
385
        }
386
        else
387
        {
388
            string str2 = "Video Res:       ";
389
            str2+= ofToString(vidPlayer.width, 0) + " x " + ofToString(vidPlayer.height, 0)  + "\n";
390
            string str4 = "Video FPS:       ";
391
            str4+= ofToString(fps, 0)+"\n\n";
392
            string str5 = "Blob Count:       ";
393
            str5+= ofToString(contourFinder.nBlobs, 0)+"\n";
394
            ofSetColor(0xFFFFFF);
395
            sidebarTXT.drawString(str + str2 + str4 + str5, 10, 30);
396
        }
397
398
        if(bTUIOMode)
399
        {        //Draw Port and IP to screen
400
            ofSetColor(0xffffff);
401
            char buf[256];
402
            sprintf(buf, "Sending TUIO messages to:\nHost: %s\nPort: %i", myTUIO.localHost, myTUIO.TUIOPort);
403
            sidebarTXT.drawString(buf, 10, 105);
404
405
            //Draw GREEN CIRCLE 'ON' LIGHT
406
            ofSetColor(0x00FF00);
407
            ofFill();
408
            ofCircle(20, 10, 5);
409
            ofNoFill();
410
        }
411
412
        ofSetColor(0xFF0000);
413
        sidebarTXT.drawString("Press spacebar for full mode", 10, 145);
414
    }
415
416
    /*********************************
417
         * IF NOT CALIBRATING
418
         *********************************/
419
    if(!bCalibration && !bFastMode)
420
    {
421
        //Draw main interface
422
        bShowInterface = true;
423
424
        //Display Application information in bottom right
425
        string str = "DSP Milliseconds: ";
426
        str+= ofToString(differenceTime, 0)+"\n\n";
427
428
        if(bcamera)
429
        {
430
            string str2 = "Camera Res:     ";
431
            str2+= ofToString(vidGrabber.width, 0) + " x " + ofToString(vidGrabber.height, 0)  + "\n";
432
            string str4 = "Camera FPS:     ";
433
            str4+= ofToString(fps, 0)+"\n";
434
            ofSetColor(0xFFFFFF);
435
            sidebarTXT.drawString(str + str2 + str4, 740, 410);
436
        }
437
        else
438
        {
439
            string str2 = "Video Res:       ";
440
            str2+= ofToString(vidPlayer.width, 0) + " x " + ofToString(vidPlayer.height, 0)  + "\n";
441
            string str4 = "Video FPS:       ";
442
            str4+= ofToString(fps, 0)+"\n";
443
            ofSetColor(0xFFFFFF);
444
            sidebarTXT.drawString(str + str2 + str4, 740, 410);
445
        }
446
447
        if(bTUIOMode)
448
        {        //Draw Port and IP to screen
449
            ofSetColor(0xffffff);
450
            char buf[256];
451
            sprintf(buf, "Sending TUIO messages to:\nHost: %s\nPort: %i", myTUIO.localHost, myTUIO.TUIOPort);
452
            sidebarTXT.drawString(buf, 740, 480);
453
454
            //Draw GREEN CIRCLE 'ON' LIGHT
455
            ofSetColor(0x00FF00);
456
            ofFill();
457
            ofCircle(35, 10, 5);
458
            ofNoFill();
459
        }
460
461
        ofSetColor(0xFF0000);
462
        sidebarTXT.drawString("Press spacebar for mini mode", 740, 580);
463
464
        //Draw PINK CIRCLE 'ON' LIGHT
465
        ofSetColor(255, 0, 255);
466
        ofFill();
467
        ofCircle(20, 10, 5);
468
        ofNoFill();
469
470
                //gui->draw();
471
472
    }
473
474
    if(bShowInterface && !bFastMode) //IF DRAWING BLOB OUTLINES
475
    {
476
        drawFingerOutlines();
477
    }
478
}
479
480
void TBetaBase::drawFingerOutlines(){
481
482
    //Find the blobs
483
    for(int i=0; i<contourFinder.nBlobs; i++)
484
    {
485
        //temp blob to rescale and draw on screen
486
        ofxTBetaCvBlob drawBlob;
487
        drawBlob = contourFinder.blobs[i];
488
489
        if(bDrawOutlines)
490
        {
491
            //Get the contour (points) so they can be drawn
492
            for( int j=0; j<contourFinder.blobs[i].nPts; j++ )
493
            {
494
                drawBlob.pts[j].x = (MAIN_WINDOW_WIDTH/camWidth)  * (drawBlob.pts[j].x);
495
                drawBlob.pts[j].y = (MAIN_WINDOW_HEIGHT/camHeight) * (drawBlob.pts[j].y);
496
            }
497
498
            //This adjusts the blob drawing for different cameras
499
            drawBlob.boundingRect.width  *= (MAIN_WINDOW_WIDTH/camWidth);
500
            drawBlob.boundingRect.height *= (MAIN_WINDOW_HEIGHT/camHeight);
501
            drawBlob.boundingRect.x                 *= (MAIN_WINDOW_WIDTH/camWidth);
502
            drawBlob.boundingRect.y                 *= (MAIN_WINDOW_HEIGHT/camHeight);
503
504
            //Draw contours (outlines) on the source image
505
            drawBlob.draw(40, 30);
506
507
//                        ofSetColor(0x0099FF); //Make Plus Sign
508
//                        ofRect((drawBlob.centroid.x * (MAIN_WINDOW_WIDTH/camWidth)) + 40, (drawBlob.centroid.y * (MAIN_WINDOW_HEIGHT/camHeight)) - drawBlob.boundingRect.height + 30, .5, drawBlob.boundingRect.height * 2); //Horizontal Plus
509
//                        ofRect((drawBlob.centroid.x * (MAIN_WINDOW_WIDTH/camWidth)) - drawBlob.boundingRect.width + 40, (drawBlob.centroid.y  * (MAIN_WINDOW_HEIGHT/camHeight)) + 30, drawBlob.boundingRect.width * 2, .5); //Vertical Plus
510
        }
511
512
        if(bShowLabels)  //Show ID label;
513
        {
514
            float xpos = drawBlob.centroid.x * (MAIN_WINDOW_WIDTH/camWidth);
515
            float ypos = drawBlob.centroid.y * (MAIN_WINDOW_HEIGHT/camHeight);
516
517
            ofSetColor(0xCCFFCC);
518
            char idStr[1024];
519
            sprintf(idStr, "id: %i,(%i, %i)",drawBlob.id, drawBlob.lastCentroid.x, drawBlob.lastCentroid.y);
520
            verdana.drawString(idStr, xpos + 365, ypos + drawBlob.boundingRect.height/2 + 45);
521
        }
522
    }
523
    ofSetColor(0xFFFFFF);
524
}
525
526
/*****************************************************************************
527
 * KEY EVENTS
528
 *****************************************************************************/
529
void TBetaBase::keyPressed(int key) {
530
        if(showConfiguration) {
531
        switch(key)
532
        {
533
                case 'b':
534
                        filter->bLearnBakground = true;
535
                        break;
536
                case 'o':
537
                        bDrawOutlines ? bDrawOutlines = false : bDrawOutlines = true;
538
                        gui->update(appPtr->trackedPanel_outlines, kofxGui_Set_Bool, &appPtr->bDrawOutlines, sizeof(bool));
539
                        break;
540
                case 'h':
541
                        bHorizontalMirror ? bHorizontalMirror = false : bHorizontalMirror = true;
542
                        gui->update(appPtr->propertiesPanel_flipH, kofxGui_Set_Bool, &appPtr->bHorizontalMirror, sizeof(bool));
543
                        break;
544
                case 'j':
545
                        bVerticalMirror ? bVerticalMirror = false : bVerticalMirror = true;
546
                        gui->update(appPtr->propertiesPanel_flipV, kofxGui_Set_Bool, &appPtr->bVerticalMirror, sizeof(bool));
547
                        break;
548
                case 't':
549
                        if(!bCalibration && bTUIOMode)
550
                        {
551
                                bTUIOMode = false;
552
                                myTUIO.blobs.clear();
553
                                gui->update(appPtr->optionPanel_tuio, kofxGui_Set_Bool, &appPtr->bTUIOMode, sizeof(bool));
554
                        }
555
                        else
556
                        {
557
                                bTUIOMode = true;
558
                                gui->update(appPtr->optionPanel_tuio, kofxGui_Set_Bool, &appPtr->bTUIOMode, sizeof(bool));
559
                        }
560
                        break;
561
                case 'g':
562
                        bGPUMode ? bGPUMode = false : bGPUMode = true;
563
                        gui->update(appPtr->gpuPanel_use, kofxGui_Set_Bool, &appPtr->bGPUMode, sizeof(bool));
564
                        filter->bLearnBakground = true;
565
                        break;
566
                case 'v':
567
                        if(bcamera)
568
                                vidGrabber.videoSettings();
569
                        break;
570
                case 'l':
571
                        bShowLabels ? bShowLabels = false : bShowLabels = true;
572
                        gui->update(appPtr->trackedPanel_ids, kofxGui_Set_Bool, &appPtr->bShowLabels, sizeof(bool));
573
                        break;
574
                case 'p':
575
                        bShowPressure ? bShowPressure = false : bShowPressure = true;
576
                        break;
577
                case ' ':
578
                        if(bFastMode)
579
                        {
580
                                bFastMode = false;
581
                                ofSetWindowShape(950,600); //default size
582
                                //ofSetWindowTitle("Configuration");
583
                        }
584
                        else
585
                        {
586
                                bFastMode = true;
587
                                ofSetWindowShape(190,155); //minimized size
588
                                //ofSetWindowTitle("Mini");
589
                        }
590
                        break;
591
                         /***********************
592
                         * Keys for Calibration
593
                         ***********************/
594
                case 'x': //Begin Calibrating
595
                        if(bCalibration){                                
596
                                bCalibration = false;
597
                                calib.calibrating = false;
598
                                tracker.isCalibrating = false;
599
                                if(bFullscreen == true) ofToggleFullscreen(); bFullscreen = false; ofSetBackgroundAuto(false);
600
                        }
601
                        break;
602
        }
603
    }
604
}
605
606
void TBetaBase::keyReleased(int key){
607
608
    if(showConfiguration)
609
    {
610
        if( key == 'c' && !bCalibration)
611
        { //Enter/Exit Calibration
612
            bCalibration = true;
613
            calib.calibrating = true;
614
                        tracker.isCalibrating = true;
615
            if(bFullscreen == false) ofToggleFullscreen(); bFullscreen = true; ofSetBackgroundAuto(true);
616
        }
617
    }
618
619
        if( key == '~' || key == '`') showConfiguration = !showConfiguration;
620
}
621
622
/*****************************************************************************
623
 *        Touch EVENTS
624
 *****************************************************************************/
625
void TBetaBase::TouchDown( ofxTBetaCvBlob b)
626
{
627
         if(bTUIOMode)//If sending TUIO, add the blob to the map list
628
         {
629
         //if blob is not otuside calibration mesh
630
         if(b.centroid.x != 0 && b.centroid.y != 0)
631
         myTUIO.blobs[b.id] = b;
632
         }
633
}
634
635
void TBetaBase::TouchUp( ofxTBetaCvBlob b)
636
{
637
         if(bTUIOMode)//If sending TUIO delete Blobs from map list
638
         {
639
         std::map<int, ofxTBetaCvBlob>::iterator iter;
640
         for(iter = myTUIO.blobs.begin(); iter != myTUIO.blobs.end(); iter++)
641
         {
642
             if(iter->second.id == b.id)
643
             {
644
                myTUIO.blobs.erase(iter);
645
                break;
646
             }
647
         }
648
         }
649
}
650
651
void TBetaBase::TouchMoved( ofxTBetaCvBlob b)
652
{
653
         if(bTUIOMode)//If sending TUIO, add the blob to the map list
654
         {
655
         //if blob is not otuside calibration mesh
656
         if(b.centroid.x != 0 && b.centroid.y != 0)
657
         myTUIO.blobs[b.id] = b;
658
         }
659
}
660
661
/*****************************************************************************
662
 *        MOUSE EVENTS
663
 *****************************************************************************/
664
void TBetaBase::mouseMoved(int x, int y)
665
{
666
} 
667
668
void TBetaBase::mouseDragged(int x, int y, int button)
669
{
670
    if(showConfiguration)
671
        gui->mouseDragged(x, y, button); //guilistener
672
}
673
674
void TBetaBase::mousePressed(int x, int y, int button)
675
{
676
        if(showConfiguration){
677
                gui->mousePressed(x, y, button); //guilistener
678
                if(x > ofGetWidth() - 230 && y > ofGetHeight() - 14) ofLaunchBrowser("http://www.wordpress.com");        
679
        }
680
}
681
682
void TBetaBase::mouseReleased()
683
{
684
    if(showConfiguration)
685
        gui->mouseReleased(mouseX, mouseY, 0); //guilistener
686
}
687
688
/*****************************************************************************
689
 * ON EXIT
690
 *****************************************************************************/
691
void TBetaBase::exit()
692
{
693
        // -------------------------------- SAVE STATE ON EXIT
694
        saveConfiguration();
695
696
        printf("tBeta module has exited!\n");
697
}