#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	ofBackground(255,255,255);	
	
	message = "loading mySettings.xml";
	
	//currently ofXMLSettings doesn't default to the data folder
	//so we have to use ofToDataPath to load the file from data/ 
	if( XML.loadFile(ofToDataPath("mySettings.xml")) ){
		message = "mySettings.xml loaded!";
	}else{
		message = "unable to load mySettings.xml check data/ folder";
	}
	
	//read the colors from xml
	//if the file doesn't yet exist it assigns default values 
	
	red		= XML.getValue("BACKGROUND:COLOR:RED", 170);
	green	= XML.getValue("BACKGROUND:COLOR:GREEN", 190);
	blue	= XML.getValue("BACKGROUND:COLOR:BLUE", 240);
	
	/*
		"BACKGROUND:COLOR:RED" creates a structure like this:
		
		<BACKGROUND>
			<COLOR>
				<RED>101.103516</RED>
			</COLOR>
		</BACKGROUND>
		
		then "BACKGROUND:COLOR:GREEN" makes the xml become:

		<BACKGROUND>
			<COLOR>
				<RED>101.103516</RED>
				<GREEN>190.103516</GREEN>
			</COLOR>
		</BACKGROUND>		
	*/
}

//--------------------------------------------------------------
void testApp::update(){
	//we change the background color here
	ofBackground(red,green,blue);	
}

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

	ofSetColor(0xDDDDDD);
	ofRect(0, 0, ofGetWidth(), 20);
	ofRect(0, ofGetHeight()-20, ofGetWidth(), 20);

	ofSetColor(210, 90, 100);
	ofDrawBitmapString(message, 10, 14);
	
	ofDrawBitmapString("drag mouse to change color - 's' key saves to xml", 10, ofGetHeight() - 7);
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
		
		if(key == 's'){
			XML.saveFile(ofToDataPath("mySettings.xml"));
		}
}

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

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

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
	//calculate r g and b for the two mouse coords coming in
	float xpct = (float)x / ofGetWidth();
	float ypct = (float)y / ofGetHeight();
	
	red = xpct * 255.0;
	green = ypct * 255.0;
	blue = (int)(red - green) % 255;
		
}

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

}

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

	//update the colors to the xml structure when the mouse is released
	XML.setValue("BACKGROUND:COLOR:RED", red);
	XML.setValue("BACKGROUND:COLOR:GREEN", green);
	XML.setValue("BACKGROUND:COLOR:BLUE", blue);
	
}
