#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){
	ofBackground(127,127,127);

	vision.loadVideo("video/motion2.mov");
    //vision.setupCamera(0, 320, 240);

}

//--------------------------------------------------------------
void testApp::update(){
    vision.update();
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(0xffffff);

    vision.draw(0,0);

    //get the average vector back and scale
    //we scale by a lot because the number of changed pixels
    //is quite small in comparison to the total number
    //if you wanted to get motion but didn't care about magnitude
    //you could normalize this result.
    ofxVec2f overall = vision.getOverallMotionFromField();
    overall *= 30000.0;

    //our center point - just for drawing a line from the center of the screen
    ofxPoint2f center(ofGetWidth()/2, ofGetHeight()/2);

    //draw a line representing the average vector
    ofSetColor(0xFF00FF);
    ofLine(center.x, center.y, center.x + overall.x, center.y + overall.y);

    //draw just the horizontal component
    ofSetColor(0x0000FF);
    ofLine(center.x, center.y, center.x + overall.x, center.y);

    ofSetColor(0xFFFF00);
    ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate())+" thresh: "+ofToString(vision.threshAmnt)+" fade: "+ofToString(vision.fadeAmnt), 20, 30);
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){
    if(key == ' '){
        vision.togglePlayback();
    }

    //if you need to adjust the camera properties
    if(key == 's'){
        vision.camera.videoSettings();
    }
}

//--------------------------------------------------------------
void testApp::keyReleased  (int key){
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

    //adjust fade amount and threshold with the mouse
    float thresh = ( (float)mouseX/ofGetWidth() ) * 255.0;
    float fade   = 0.6 + ( (float)mouseY/ofGetHeight() ) * 0.4;

    vision.setThreshold(thresh);
    vision.setFadeAmnt(fade);
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(){

}
