#include "graffitiControlPanel.h"

graffitiControlPanel::graffitiControlPanel(vars *v) {
	myVars = v;
}

void graffitiControlPanel::startGUI() {
	files.listDir(".");//our GMLs should be in data by default anyway. Add user input later
	
	gui = new ofxControlPanel();
	gui->notify();
	gui->loadFont("MONACO.TTF", 8);		
	gui->setup("Graffiti Control", ofGetWidth() - myVars->controlWidth, 0, myVars->controlWidth, ofGetHeight());

	gui->addPanel("Control Panel", 2, false);	

	gui->setWhichPanel(0);
	gui->setWhichColumn(0);

	gui->addSlider("Thickness","THICKNESS",myVars->thickness,1,20,false);
	gui->addSlider("Bridge thickness","CONTOUR_THICKNESS", myVars->contourThickness, 1,20,false);
	gui->addSlider("Hole threshold","THRESHOLD",myVars->threshold,0,5000,false);
	gui->addSlider("Scale factor","SCALE_FACTOR", myVars->scaleFactor, 1, 1000,false);

	gui->addFileLister("GML files",&files,180,150);
	
	gui->setWhichColumn(1);

	gui->addToggle("Thicken","THICKEN?",false);
	gui->addToggle("Show holes","SHOW_HOLES?",false);
	gui->addToggle("Add bridges","CONTOURS?",false);
	gui->addToggle("Save me!","SAVE",false);

	gui->addToggle("Closest bridges","CBRIDGE",false);
	gui->addToggle("Highest bridges","HBRIDGE",false);
	gui->addToggle("Lowest bridges","BBRIDGE",false);
	gui->addToggle("Left bridges","LBRIDGE",false);
	gui->addToggle("Right bridges","RBRIDGE",false);
}

void graffitiControlPanel::updateGUI() {
	myVars->thickness = gui->getValueF("THICKNESS");
	myVars->contourThickness = gui->getValueF("CONTOUR_THICKNESS");
	myVars->threshold = gui->getValueF("THRESHOLD");
	myVars->scaleFactor = gui->getValueF("SCALE_FACTOR");
		
	myVars->thickenOn = gui->getValueB("THICKEN?");
	myVars->contoursOn = gui->getValueB("CONTOURS?");
	myVars->showHoles = gui->getValueB("SHOW_HOLES?");

	myVars->findClosestBridge = gui->getValueB("CBRIDGE");
	myVars->highestBridge = gui->getValueB("HBRIDGE");
	myVars->lowestBridge = gui->getValueB("BBRIDGE");
	myVars->leftMostBridge = gui->getValueB("LBRIDGE");
	myVars->rightMostBridge = gui->getValueB("RBRIDGE");

	myVars->save = gui->getValueB("SAVE");
	if(files.selectedHasChanged()) { 
		char *a = new char[files.getSelectedPath().size()+1];
		a[files.getSelectedPath().size()] = 0;
		memcpy(a,files.getSelectedPath().c_str(),files.getSelectedPath().size());
		myVars->fileName = a;
		myVars->fileChanged = true;

		//all the values get reset when you load a new image in order to make it faster to load
		gui->setValueB("THICKEN?",false);
		gui->setValueB("CONTOURS?",false);
		gui->setValueB("SHOW_HOLES?",false);

		gui->setValueB("CBRIDGE",false);
		gui->setValueB("HBRIDGE",false);
		gui->setValueB("BBRIDGE",false);
		gui->setValueB("LBRIDGE",false);
		gui->setValueB("RBRIDGE",false);
	}

	gui->setValueB("SAVE",false);
	gui->update();
	files.clearChangedFlag();
}

void graffitiControlPanel::drawGUI() {
	gui->draw();
}

void graffitiControlPanel::mouseDragged(int x, int y, int button) {
	gui->mouseDragged(x,y,button);
}

void graffitiControlPanel::mousePressed(int x, int y, int button) {
	gui->mousePressed(x,y,button);
}

void graffitiControlPanel::mouseReleased() {
	gui->mouseReleased();
}