#include "testApp.h"

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

	franklinBook.loadFont("type/frabk.ttf",18);
	verdana.loadFont("type/verdana.ttf",11);

	tcpClient.setup("127.0.0.1", 11999);
	
	typed	  = false;
	pos		  = 0;

	msgTx	  = "";
	msgRx	  = "";
	
}



//--------------------------------------------------------------
void testApp::update(){
	ofBackground(20, 37, 51);	

	tcpClient.send(msgTx);
	msgRx = tcpClient.receive();
	
}

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

	ofSetColor(0x4A80BD);
	franklinBook.drawString("openFrameworks TCP Send Example", 15, 30);

	if(typed){
		verdana.drawString("sending:", 15, 55);		
		verdana.drawString(msgTx, 85, 55);
	}
	else{
		verdana.drawString("status: type something to send data to port 11999", 15, 55);
	}
	
	verdana.drawString("received from server: "+msgRx, 15, 370);
	
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 

	if(key == 13)key = '\n';
	if(key == 127){
		if( pos != 0 ){pos--;
			msgTx = msgTx.substr(0,pos);
		}else msgTx = "";
	}else{
		msgTx.append(1, (char) key);
		pos++;
	}
	typed = true;
}

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

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

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

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

}
