#include "testApp.h"

void testApp::setup() {
	width = 1024;
	height = 786;
	
	mode = 1;
	construction = false;
	playAnimation = false;
	drag = false;
	prisoner = NULL;
	
	Verlet.setGravity(ofxVec2f(0,0.5)); //0g
	
	ofSetFrameRate(60);

	ofEnableAlphaBlending();
}


void testApp::update(){
	ofBackground( 0,0,0 );
	
	if(playAnimation) Verlet.step();
	
}


void testApp::draw() {
	
	ofBackground(0,0,0);
	Verlet.draw();
	
	fullscreen = false;
	
	char tempStr[255];
	if(drag) {
		sprintf(tempStr, "%i | mode_%i | -%i- | living: %f | total: %i", (int)ofGetFrameRate(), mode, drag, Verlet.resting, Verlet.active.size()); 
	} else {
		sprintf(tempStr,  "%i | mode_%i |     | living: %f | total: %i", (int)ofGetFrameRate(), mode, Verlet.resting, Verlet.active.size()); 
	}
	ofDrawBitmapString(tempStr, 10,15);
	
	if(playAnimation) {
		sprintf(tempStr, "|> playing", (int)ofGetFrameRate()); 
	} else {
		sprintf(tempStr, "[] stopped", (int)ofGetFrameRate()); 
	}
	ofDrawBitmapString(tempStr, 10,30);
	
}

void testApp::keyPressed( int key ) {
	if(key == 'c') {
		Verlet.active.clear();
		Verlet.sticks.clear();
		Verlet.joints.clear();
	}
	
	if( key == 6) {
		ofSetFullscreen(!fullscreen);
		fullscreen = !fullscreen;
	}
	
	
	if(key == '1') mode = 1;
	if(key == '2') mode = 2;
	if(key == '3') mode = 3;
	if(key == '4') mode = 4;
	
	if(key == ' ') playAnimation = !playAnimation;
	if(key == 'b') construction = !construction;
	if(key == 'd') drag = !drag;
}
void testApp::keyReleased( int key ) {}

void testApp::mouseMoved( int x, int y ) {}	
void testApp::mouseDragged( int x, int y, int button ) {
	if(drag && prisoner != Verlet.active.size() + 1) Verlet.active[prisoner]->setPosition(x,y);
}
void testApp::mousePressed( int x, int y, int button ) {
	int currIndex = Verlet.active.size() - 1;
	ofxVec2f mouse;
	mouse.set(x,y);
	bool taken = false;
	
	if(drag) {
		for(int i = 0; i < Verlet.active.size(); i++) {
			if(ofxVec2fDistance(mouse, Verlet.active[i]->currPos) < 25) {
				prisoner = i;
			}
		}
	}
	
	
	if(!drag) {
		switch(mode) {
			case(1):
				if(button == 0) {
					Verlet.addPoint(x, y, 0, 0 , 1);
				} else {
					Verlet.addFixedPoint(x, y, 0, 0, 1);
				}
				break;
			case(2):
				if(currIndex + 1 > 0) {
					for(int i = 0; i < currIndex + 1; i++) {
						if(ofxVec2fDistance(mouse, Verlet.active[i]->currPos) < 25 && (currIndex+ 1 > 0)) {
							if(currIndex != i) Verlet.addStick(Verlet.active[currIndex], Verlet.active[i]);
							taken = true;
						}
					}
				}
				
				if(!taken && currIndex + 1 > 0) {
					Verlet.addPoint(x, y, 0, 0, 1);
					Verlet.addStick(Verlet.active[currIndex + 1], Verlet.active[currIndex]);
				} else if(!taken){
					Verlet.addPoint(x,y,0,0,1);
				}
				taken = false;
				break;
			case(3):
				if(currIndex + 1 > 0) {
					for(int i = 0; i < currIndex + 1; i++) {
						if(ofxVec2fDistance(mouse, Verlet.active[i]->currPos) < 25 && (currIndex+ 1 > 0)) {
							if(currIndex != i) Verlet.addJoint(Verlet.active[currIndex], Verlet.active[i], 1, 1.2);
							taken = true;
						}
					}
				}
				
				if(!taken && currIndex + 1 > 0) {
					Verlet.addPoint(x, y, 0, 0, 1);
					Verlet.addJoint(Verlet.active[currIndex + 1], Verlet.active[currIndex], 1, 1.2);
				} else if(!taken) {
					Verlet.addPoint(x,y,0,0,1);
				}
				
				taken = false;
				break;
			case(4):
				Verlet.addBlob(x,y);
				break;
		}
	}
}
void testApp::mouseReleased() {
	prisoner = Verlet.active.size()+1;
}
    

