void testApp::openProjectFile( )
{

    cout << "open Dialgoue!" << endl ;
    ofFileDialogResult loadResult = ofSystemLoadDialog(  "Open Project XML" ) ;


    string path = loadResult.getPath() ; //
    cout << "end result XML path!" << endl ;

    projectXml.loadFile( path ) ;

    int numWordBlocks = projectXml.getNumTags( "wordBlock" ) ;

    quote.clearWordBlocks() ;
    quote.setup( ) ;

    for ( int i = 0 ; i < numWordBlocks ; i++ )
    {
        projectXml.pushTag( "wordBlock" , i ) ;
        ofPoint translate = ofPoint( projectXml.getValue( "translateX" , 0 ) , projectXml.getValue( "translateY" , 0 ) ) ;
        string text =   projectXml.getValue( "text" , "noText" ) ;
        string fontPath = "../" + projectXml.getValue( "fontPath" , "noFont" ) ;
        float fontSize = projectXml.getValue ( "fontSize" , 18 ) ;

        quote.addWordBlock( text , fontPath , translate , fontSize ) ;
        projectXml.popTag( ) ;
    }


    int numColorTags = projectXml.getNumTags( "color" ) ;
    cout << "numColorTags : " << numColorTags << endl ;
    colorPool.clear() ;
    inspector.colors.clear() ;
    for ( int c = 0 ; c < numColorTags ; c++ )
    {
        projectXml.pushTag( "color" , c ) ;

            ofColor color ;
            color.r = projectXml.getValue( "r" , 125 ) ;
            color.g = projectXml.getValue( "g" , 125 ) ;
            color.b = projectXml.getValue( "b" , 125 ) ;
            //cout << "r : " << color.r << " g : " << color.g << " b : " << color.b << endl ;
            colorPool.addColor( color ) ;
        projectXml.popTag( ) ;

        inspector.colors.push_back( color ) ;

    }
    a_maxSpeed = projectXml.getValue ( "MAX SPEED" , a_maxSpeed , a_maxSpeed ) ;
    a_rOffsetMaxSpeed = projectXml.getValue ( "MAX SPEED R OFFSET" , a_rOffsetMaxSpeed , a_rOffsetMaxSpeed ) ;
    a_maxForce = projectXml.getValue ( "MAX FORCE" , a_maxForce , a_maxForce ) ;
    a_rOffsetMaxTurn = projectXml.getValue ( "MAX FORCE R OFFSET" , a_rOffsetMaxTurn , a_rOffsetMaxTurn ) ;
    a_targetBuffer = projectXml.getValue ( "BUFFER DIST" , a_targetBuffer , a_targetBuffer ) ;
    a_pathSampling = projectXml.getValue ( "PATH SAMPLING" , a_pathSampling , a_pathSampling ) ;
    a_numAgents = projectXml.getValue ( "NUM AGENTS" , a_numAgents , a_numAgents ) ;

    resetAgents( ) ;
    quote.bReadyToStart = true ;
}
void AgentInjector::readAgents() {
	resetAgents();

	for (unsigned int i = 0; i < world.data_directories.size(); i++) {
		fs::path p = world.data_directories[i];
		if (engine.version == 2) p = p / "/Objects/";
		if (!fs::exists(p) || !fs::is_directory(p)) {
			continue;
		}

		fs::directory_iterator end_itr; // default constructor is the end
		for (fs::directory_iterator itr(p); itr != end_itr; itr++) {
			std::string cobext = fs::extension(itr->path());
			std::transform(cobext.begin(), cobext.end(), cobext.begin(), (int(*)(int))tolower); // downcase
			if (cobext != ".cob") continue;
			
			std::string cob = itr->path().string();
			
			if (engine.version == 1) {
				std::ifstream cobstream(cob.c_str(), std::ios::binary);
				if (!cobstream.fail()) {
					c1cobfile cobfile(cobstream);
					QListWidgetItem *newItem = new QListWidgetItem(cobfile.name.c_str(), ui.agentList);
					newItem->setToolTip(cob.c_str());
				}
			} else if (engine.version == 2) {
				cobFile *cobfile = new cobFile(itr->path());
				cobfiles.push_back(cobfile);

				for (std::vector<cobBlock *>::iterator i = cobfile->blocks.begin(); i != cobfile->blocks.end(); i++) {
					if ((*i)->type != "agnt") continue;
					cobAgentBlock *a = new cobAgentBlock(*i);
					QListWidgetItem *newItem = new QListWidgetItem(a->name.c_str(), ui.agentList);
					newItem->setToolTip(cob.c_str());
					newItem->setData(Qt::UserRole, QVariant::fromValue((void *)a));
				}
			}
		}
	}

	ui.agentList->sortItems();
}
void testApp::removeLastColorHandler( int &args )
{
    colorPool.setColors( inspector.colors ) ;
    resetAgents( ) ;
}
void testApp::newColorHandler( ofColor &color )
{
    cout << "newColor HANDLER! R : " << color.r << " G: " << color.g << " B: " << color.b << endl ;
    colorPool.setColors( inspector.colors ) ;
    resetAgents( ) ;
}
void testApp::keyPressed( int key )
{
    cout << "key : " << key << endl ;
 //   if ( canvasAlpha > 0.0f )
 //   {
        WordBlock * wb = quote.getEditableBlock( ) ;
        if ( wb == NULL )
        {
            switch ( key )
            {
                case 'p':
                case 'P':
                    exportPDF() ;
                    break ;

                case 'r':
                case 'R':
                    resetAgents() ;
                    break ;

                case 's':
                case 'S':
                    saveProjectFile( ) ;
                    break ;

                case 'o':
                case 'O':
                    openProjectFile( ) ;
                    break ;

                case ' ':
                    bRunAgents = !bRunAgents ;
                    break ;

                case 'd':
                case 'D':
                    bDebugDraw = !bDebugDraw ;
                    break ;

                case 'n':
                case 'N':
                    cout << "new WordBlock" << endl ;
                    createNewWordBlock() ;
                    break ;

                case 13 :
                    cout << "end typing wordBlock" << endl ;
                    break ;

                case 'e':
                case 'E':
                    quote.editWordBlockAt ( mouseX , mouseY ) ;
                    break ;

                case 'f':
                case 'F':
                    openFontDialogue() ;
                    break ;
                case 127 :
                case 8 :
                    quote.removeWordBlockAt ( mouseX , mouseY ) ;
                    break ;

            }
            cout << "no editable path!" << endl;
        }
        else
        {
            if ( key == 13 )
            {
                wb->bEditable = false ;
                return ;
            }

            //Backspace
            else if ( key == 127|| key == 8 )
            {
                string word = wb->word ;
                if ( word.size() > 0 )
                {
                    string word1 = word.substr( 0 , word.size()-1 ) ;
                    word = word1 ;
                }
                wb->word = word ;

                updateNewWordBlock( wb->word , newFontSize ) ;
                //updateNewWordBlock( wb->word , newFontSize ) ;
                return ;
            }

            wb->word += key ;
            cout << "updating word blcok with : " << wb->word << endl ;
            //void testApp::updateNewWordBlock ( string _word , float _fontSize )
           //   WordBlock * wb = quote.getEditableBlock( ) ;
            updateNewWordBlock( wb->word , newFontSize ) ;

        }
  //  }

}
void testApp::guiEvent(ofxUIEventArgs &e)
{
	string name = e.widget->getName();
	int kind = e.widget->getKind();

	if(name == "MAX SPEED")
		a_maxSpeed = ((ofxUISlider *) e.widget)->getScaledValue();

    if(name == "MAX FORCE")
		a_maxForce = ((ofxUISlider *) e.widget)->getScaledValue();

    if(name == "BUFFER DIST")
		a_targetBuffer = ((ofxUISlider *) e.widget)->getScaledValue();

    if(name ==  "PATH SAMPLING" )
		a_pathSampling =(int) ((ofxUISlider *) e.widget)->getScaledValue();

    if ( name == "NUM AGENTS" )
    {
        a_numAgents =(int) ((ofxUISlider *) e.widget)->getScaledValue();
        resetAgents( ) ;
    }

    if ( name == "MAX SPEED R OFFSET" )
        a_rOffsetMaxSpeed = ((ofxUISlider *) e.widget)->getScaledValue();

    if ( name == "MAX FORCE R OFFSET"  )
        a_rOffsetMaxTurn = ((ofxUISlider *) e.widget)->getScaledValue();

    if ( name == "TRAIL TYPES" )
    {
       // ((ofxUIRadio * ) e.widget)->ge
    }

    if ( name == "NEW FONT SIZE" )
    {
        newFontSize = ((ofxUISlider *) e.widget)->getScaledValue() ;

         WordBlock * wb = quote.getEditableBlock( ) ;
         if ( wb != NULL )
         {
             updateNewWordBlock( wb->word , newFontSize ) ;
             resetAgents() ;
         }
    }
    //newFontSize
    /*
     vector<string> hnames; hnames.push_back("LINES"); hnames.push_back("CIRCLES"); hnames.push_back("RECTANGLES");
     gui->addWidgetDown(new ofxUIRadio( radioSize , radioSize , "TRAIL TYPES", hnames, OFX_UI_ORIENTATION_HORIZONTAL));
     */

    gui->saveSettings("GUI/settings.xml") ;

    vector<Agent*>::iterator a ;
    for ( a = agents.begin() ; a != agents.end() ; a++ )
    {
        (*a)->maxVelocity = a_maxSpeed + ofRandom( -a_rOffsetMaxSpeed , a_rOffsetMaxSpeed ) ;
        (*a)->maxForce = a_maxForce  + ofRandom( -a_rOffsetMaxTurn , a_rOffsetMaxTurn ) ;
        (*a)->targetBufferDist = a_targetBuffer ;
        (*a)->pathSampling = a_pathSampling ;
    }
}
AgentInjector::~AgentInjector() {
	resetAgents();
}