//
//  button.cpp
//  Wablet
//
//  Created by Robert Tubb on 19/03/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
//  button on screen either side of mesh
#include <iostream>
#include "button.h"
#include "globalUI.h"
#include "globalForces.h"
extern GlobalUI globalUI;
extern GlobalForces globalForces;

ControlButton::ControlButton(int abuttonId, int awidth,int aheight,int axpos,int aypos, string aname, Mesh *atheMeshPtr, ofColor acolor){
    width = awidth;
    height = aheight;
    xpos = axpos;
    ypos = aypos;
    name = aname;
    bcolor = acolor;
    bcolor.setSaturation(150);
    bcolor.setBrightness(150);
    hicolor = acolor;
    hicolor.setSaturation(230);
    bcolor.setBrightness(230);
    buttonId = abuttonId;
    pressed = 0;
    theMesh = atheMeshPtr;
    
    //callingApp = atestApp;
    
}

void ControlButton::draw(){
    //cout << "button draw " << hicolor.r << " \n";
    if(pressed){
        ofSetColor(hicolor);
    }else{
        ofSetColor(bcolor);
    }
    
    ofRect(xpos,ypos,width,height);
    
    ofSetLineWidth(2);
    ofSetColor(hicolor); 
    ofLine(xpos,ypos,xpos+width,ypos);
    ofLine(xpos,ypos,xpos,ypos+height);
    ofLine(xpos+width,ypos,xpos+width,ypos+height);
    ofLine(xpos,ypos+height,xpos+width,ypos+height);
    
    ofSetColor(0, 0, 0);
    ofDrawBitmapString(name, xpos+10, ypos+20);
    
}

bool ControlButton::checkTouchArea(int axpos,int aypos){
    if ( (axpos > xpos) && (axpos < xpos+width) && (aypos > ypos) && (aypos < ypos+height)){
        return true;
    }else{
        return false;
    }
    
}

void ControlButton::press(){
    cout << name << " button has been pressed\n";

	if (name == "still"){
		globalForces.homingAmt = 0.15;
	}

	if (name == "drift"){
		theMesh->toggleSpringForces(false);
	}	
	if (name == "tighter"){
		theMesh->increasePropagationSpeed();
	}		
	if (name == "slacker"){
		theMesh->decreasePropagationSpeed();
	}	

	if (name == "reset"){
		theMesh->resetAll();
	}	
	if (name=="stickedge"){
		theMesh->constrain(.0,.0,Mesh::CONSTRAIN_EDGES);
	}

	if (name == "free"){
		theMesh->unconstrain();
	}

    if (name == "drawscan"){
        theMesh->resetPositions();
        theMesh->resetVelocities();
        globalUI.touchMode = globalUI.INSCRIBE_PATH;
        theMesh->clearScanPath();
        theMesh->update();
	}
    if (name == "deleteMesh"){
		// cant
	}
    
	if (name == "force"){
		globalUI.touchMode = globalUI.FORCE_FIELD;
	}	
	if (name == "stick"){
		globalUI.touchMode = globalUI.CONSTRAIN;
	}		
    if (name == "unstick"){
		globalUI.touchMode = globalUI.UNCONSTRAIN;
	}	
	if (name == "free"){
        // nothing
		//globalUI.touchMode = globalUI.UNCONSTRAIN;
	}	
	if (name == "grab"){
		globalUI.touchMode = globalUI.GRAB;
	}	
	if (name == "smooth"){
		globalForces.avFilterAmt = 0.11;
	}
	if (name == "harmonic"){
        globalUI.touchMode = globalUI.SPATIAL_HARMONIC;
    }
    if (name == "gravity"){
        globalForces.gravityAmt = 4.0;
    }
    if(name == "scanmode"){
        theMesh->scanPath->scanMode = theMesh->scanPath->SPEED;
    }
        pressed = true;
}

void ControlButton::release(){
    
    if (name == "homing"){
		globalForces.homingAmt = 0.0;
	}
    if (name == "drift"){
		theMesh->toggleSpringForces(true);
	}	
	if (name == "smooth"){
		globalForces.avFilterAmt = 0.02;
	}	
    if (name == "gravity"){
        globalForces.gravityAmt = 0.0;
    }
    if(name == "scanmode"){
        theMesh->scanPath->scanMode = theMesh->scanPath->DISPLACEMENT;
    }
    if (name == "freeAll"){
		theMesh->constrain(.0,.0,Mesh::CONSTRAIN_EDGES);
	}
    pressed = false;
    
    
}