Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

src/StateControl.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Traplas visualisation.
00004 
00005 Copyright (C) 2006 Herbert de Vos & Willem Drost
00006 
00007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00010 
00011 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00012 
00013 (For full Licence see ../GPL-Licence.txt)
00014 
00015 */
00016 
00017 //TODO add scaling to mapping files.
00018 
00019 #ifndef STATECONTROL_H
00020 #include "StateControl.h"
00021 #endif
00022 
00023 #ifndef DISPATCHER_H
00024 #include "Dispatcher.h"
00025 #endif
00026 
00027 #ifndef _GLIBCXX_SSTREAM
00028 #include <sstream>
00029 #endif
00030 
00031 #ifndef _GLIBCXX_IOSTREAM
00032 #include <iostream>
00033 #endif
00034 
00035 #ifndef OSGDB_READFILE 
00036 #include <osgDB/ReadFile>
00037 #endif
00038 
00039 #ifndef OSG_VEC3D
00040 #include <osg/Vec3d>
00041 #endif
00042 
00043 #ifndef OSG_QUAT
00044 #include <osg/Quat>
00045 #endif
00046 
00047 #ifndef OSG_POSITIONATTITUDETRANSFORM
00048 #include <osg/PositionAttitudeTransform>
00049 #endif
00050 
00051 #ifndef OSG_AUTOTRANSFORM
00052 #include <osg/AutoTransform>
00053 #endif
00054 
00055 #ifndef OSG_VEC4
00056 #include <osg/Vec4>
00057 #endif
00058 
00059 #ifndef OSG_MATRIX
00060 #include <osg/Matrix>
00061 #endif
00062 
00063 #ifndef OSG_MATRIXTRANSFORM
00064 #include <osg/MatrixTransform>
00065 #endif
00066 
00067 #ifndef OSG_GEOMETRY
00068 #include <osg/Geometry>
00069 #endif
00070 
00071 #ifndef OSG_STATESET
00072 #include <osg/StateSet>
00073 #endif
00074 
00075 #ifndef OSGTEXT_TEXT
00076 #include <osgText/Text>
00077 #endif
00078 
00079 #ifndef OSGGA_NODETRACKERMANIPULATOR
00080 #include <osgGA/NodeTrackerManipulator>
00081 #endif
00082 
00083 
00084 //Own callback class:
00085 #ifndef ANIMATIONPATHTIMEDCALLBACK_H
00086 #include "AnimationPathTimedCallback.h"
00087 #endif
00088 
00089 #ifndef USERINTERFACE_H
00090 #include "UserInterface.h"
00091 #endif
00092 
00093 #ifndef _GLIBCXX_LIST
00094 #include <list>
00095 #endif
00096 
00097 #ifndef GLOBALS_H
00098 #include "Globals.h"
00099 #endif
00100 
00101 #include <algorithm>
00102 
00103 /*StateControl*/
00104 
00105 
00106 void* go(void* none){
00107         //sets up the osg viewer and hud, creates callbacks and UserInterface,  checks the dispatcher for messages, updates world, starts osg main loop.
00108         StateControl *st = static_cast<StateControl*>( none );  
00109         
00110         osg::Group* root = dynamic_cast<osg::Group*>( st->getOsgRoot() );
00111         assert( root != NULL );
00112         osgProducer::Viewer* viewer = new osgProducer::Viewer();
00113 
00114         st->view = viewer;
00115 
00116         viewer->setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
00117         viewer->setSceneData(root);
00118 
00119         root->addChild( st->ui->createHud() );
00120         //Event handler for picking and updating the HUD:
00121         viewer->getEventHandlerList().push_front(new UserEventHandler(viewer, st->ui ));
00122 
00123         /*      TODO 
00124         //add camera manipulator to the list:
00125         unsigned int cmid = viewer->addCameraManipulator(new osgGA::NodeTrackerManipulator());  
00126         
00127         list<string> tempNames;
00128         viewer->getCameraManipulatorNameList(tempNames);
00129         
00130         ((StateControl*)none)->trackingCamName = tempNames.back(); //assuming we've added to the end of the list.*/
00131 
00132         viewer->realize();
00133         //osg main loop:
00134         while( !viewer->done() ){
00135                 //check for messages in the queue
00136                 st->dis->dispatchMessage();
00137                 viewer->sync();
00138                 viewer->update();
00139                 viewer->frame();
00140         }
00141         cout << "Viewer exit." << endl;
00142         //      TODO    Call destructors.
00143         exit(0);
00144         
00145         return NULL;
00146 }
00147 
00148 StateControl::StateControl(Dispatcher* disp){
00149 
00150         dis = disp;
00151         ui = new UserInterface(this);
00152         view = NULL;
00153         pausetime = 0.0;
00154 
00155         readMapping(CARGO);
00156         readMapping(TRANSPORT);
00157         readMapping(LOCATION);
00158 
00159         osgRoot = new osg::Group;
00160         
00161         //create thread that can live in go():
00162         pthread_create(&osg_update_loop, NULL, go, (void*)this);
00163 }
00164 
00165 bool StateControl::readMapping(int type){
00166         //rewrite this bit for use here:
00167         string line;
00168         vector<string> mapping;
00169         ifstream mappingfile;
00170         switch(type){
00171                 case CARGO:
00172                 mappingfile.open("../cargo");
00173                 break;
00174                 case TRANSPORT:
00175                 mappingfile.open("../transport");
00176                 break;
00177                 case LOCATION:
00178                 mappingfile.open("../locations");
00179                 break;
00180                 default:
00181                 cout << "Warning: (Statecontrol::readMapping()) Error readMapping: Trying to read type " << type << ". Doesn't exist." << endl;
00182                 break;
00183         }
00184 
00185         if (mappingfile.is_open()){
00186                 while (!mappingfile.eof()){
00187                         getline (mappingfile, line);
00188                         mapping.push_back(line); //add line to vector
00189                 }
00190         } else {
00191                 cout << "Error: (Statecontrol::readMapping()) Cannot open file for type " << type << "." << endl;
00192                 exit(0);
00193         }
00194         mappingfile.close();
00195         
00196         int spaceIndex;
00197         if(type == LOCATION){   
00198                 vector<string>::iterator it = mapping.begin();
00199                 //1st element is the scaling of the tiles:
00200                 tilescale = atof((*it).c_str());
00201                 it++;   
00202                 for ( ; it != mapping.end() ; it++) {
00203                         spaceIndex = (*it).find(" ", 0);
00204                         string mapNrStr = it->substr(0, (spaceIndex));
00205                         string mapValue = it->substr((spaceIndex+1));
00206                         int mapNr= atoi(mapNrStr.c_str());
00207                         tileMap[mapNr]= ("../"+mapValue);
00208                 }
00209         } else if(type == CARGO) {
00210                 spaceIndex = (mapping.at(0)).find(" ",0);
00211                 cargomodel = ("../"+(mapping.at(0).substr(0,spaceIndex)));
00212                 cargoscale =  atof(mapping.at(0).substr(spaceIndex+1).c_str() );
00213         } else if(type==TRANSPORT) {
00214                 spaceIndex = (mapping.at(0)).find(" ",0);
00215                 transmodel = ("../"+(mapping.at(0).substr(0,spaceIndex)));
00216                 transscale = atof(mapping.at(0).substr(spaceIndex+1).c_str() );
00217         }
00218 
00219         return true;
00220 }
00221 
00222 StateControl::~StateControl(){
00223         delete ui;
00224 }
00225 
00226 bool StateControl::location(int loc_id, shared_ptr<string> desc, unsigned int capacity, double dist, double s, int tile, double width, double height, double x, double y, double rot){
00227 
00228         osg::Node* locModel = osgDB::readNodeFile(tileMap[tile]);
00229         assert( locModel != NULL );
00230         
00231         if ( world[loc_id] != NULL ) {
00232                 cout << "Warning: (StateControl::location()) Something with id " << loc_id << " allready exists! No location created." << endl;
00233                 #if STRICT
00234                         exit(1);
00235                 #endif
00236                 return false;
00237         }
00238  
00239         //      Check if width and height are positive
00240         if(width <=0 || height <=0 ){
00241                 cout << "Warning: (StateControl::location()) Incorrect tile dimensions: width and height must be > 0" << endl;
00242                 #if STRICT
00243                         exit(1);
00244                 #endif
00245                 //      Set width and height to 0. This causes the tile to be invisible.
00246                 width = 0;
00247                 height = 0;
00248         }
00249         
00250         // Create infrastructure object and add it to worldmapping.
00251         Infrastructure* inf     = new Infrastructure(loc_id, LOCATION, s, tile, capacity, dist, locModel);
00252         world[loc_id]=inf;
00253 
00254         // Create scaling.
00255         osg::AutoTransform* scaleTrans = new osg::AutoTransform();
00256         scaleTrans->setScale(osg::Vec3d(tilescale * (width/2.0), tilescale,tilescale * (height/2.0)));
00257 
00258         //      Create position.
00259         osg::PositionAttitudeTransform* locXform = new osg::PositionAttitudeTransform();
00260         locXform->setPosition(osg::Vec3d(x,0,y));
00261 
00262         //      Orientation in quaternions!
00263         double rrot = (rot/360.0)*(2.0*M_PI); //convert to rads
00264         locXform->setAttitude(osg::Quat(rrot, osg::Vec3d(0,1,0)));
00265 
00266         //      Add it to the OSG-tree
00267         scaleTrans->addChild(locModel);
00268         locXform->addChild(scaleTrans);
00269         osgRoot->addChild(locXform);
00270 
00271         //      Set label and description.
00272         inf->initLabel();
00273         inf->setLabel(desc);
00274         inf->setDescription(*desc);
00275 
00276   return true;
00277 }
00278 
00279 bool StateControl::drv(int Rt, int Ri_a, int Ri_b, int Ri_d, double ts, double te){
00280 
00281         Infrastructure *tmp_a = dynamic_cast<Infrastructure*>( world[Ri_a] );
00282         Infrastructure *tmp_b = dynamic_cast<Infrastructure*>( world[Ri_b] );
00283         Infrastructure *tmp_d = dynamic_cast<Infrastructure*>( world[Ri_d] );
00284         Transport *tmp_t = dynamic_cast<Transport*>( world[Rt] );
00285         assert ( !(tmp_a == NULL) && !(tmp_b == NULL) && !(tmp_d == NULL) && !(tmp_t == NULL) );
00286 
00287         if( !((Infrastructure*)tmp_a)->isArc(Ri_b) ){
00288                 cout << "Warning: (StateControl::drv()) No arc from " << Ri_a << " to " << Ri_b << ", drive action invalid." << endl;
00289                 #if STRICT
00290                         exit(1);
00291                 #endif
00292         }
00293 
00294         osg::AnimationPath* mypath = new osg::AnimationPath();
00295         mypath->setLoopMode(osg::AnimationPath::NO_LOOPING);
00296 
00297         osg::Vec3d pos_a = tmp_a->getPosition();
00298         osg::Vec3d pos_b = tmp_b->getPosition();
00299 
00300         //      Make the rotation thing to point the car and its cargo in the right direction.
00301         double angle = atan2( (pos_a.x() - pos_b.x()), (pos_b.z()-pos_a.z()) );
00302         osg::Quat myquat((-1*angle), osg::Vec3d(0,1,0));
00303 
00304         mypath->insert(0.0, osg::AnimationPath::ControlPoint(pos_a, myquat));
00305         //insert more controlpoints here for more fluid drive actions. (acceleration etc.)
00306         mypath->insert((double)(te - ts), osg::AnimationPath::ControlPoint(pos_b, myquat));
00307 
00308         osg::PositionAttitudeTransform* locXform = dynamic_cast<osg::PositionAttitudeTransform*>(tmp_t->getModel()->getParent(0)->getParent(0));
00309         assert( locXform != NULL);
00310 
00311         AnimationPathTimedCallback* timePath = new AnimationPathTimedCallback();
00312         timePath->setAnimationPath(mypath);
00313         timePath->setTimeOffset(0.0);
00314         timePath->setTimeReference(dis);
00315         timePath->setTimeMultiplier(dis->getTimeFlow());
00316 
00317         locXform->setUpdateCallback( ((osg::NodeCallback*)timePath) );
00318         
00319         tmp_t->setDestination( tmp_d->getModel()->getName() );
00320 
00321         return true;
00322 }
00323 
00324 bool StateControl::load(int Rt, int Ri, int C, double ts, double tf){
00325         Cargo *cgl = dynamic_cast<Cargo*>( world[C] );
00326         Infrastructure *infl = dynamic_cast<Infrastructure*>( world[Ri] );
00327         Transport *tnl = dynamic_cast<Transport*>( world[Rt] );
00328         assert ( !(cgl == NULL) && !(infl == NULL) && !(tnl == NULL) );
00329 
00330         int Cid = infl->removeCarg(C);
00331         if(Cid != C){
00332                 cout << "Warning: (StateControl::load())Cargo" << cgl->getModel()->getName() <<" not at location " << infl->getModel()->getName() << endl;
00333                 #if STRICT
00334                         exit(1);
00335                 #endif
00336                 return false;
00337         }
00338         tnl->setCargo(C , cgl->getModel()->getName() );
00339 
00340         //      OSG-tree manipulation
00341         //      Add the cargo to the PositionAttitudeTransform of the transport resource.
00342         osg::PositionAttitudeTransform *tr_pat = dynamic_cast<osg::PositionAttitudeTransform*>( tnl->getModel()->getParent(0)->getParent(0) );
00343         assert( tr_pat != NULL );
00344 
00345         osg::AutoTransform *carg_at = dynamic_cast<osg::AutoTransform*>( cgl->getModel()->getParent(0) );
00346         assert( carg_at != NULL );
00347         
00348         carg_at->setPosition( osg::Vec3d( 0, -2, 0) );  //      Remove the offset on the Infrastructure Resource, set offset for transport.
00349 
00350         tr_pat->addChild( carg_at );
00351 
00352         //      Now remove it from the PositionAttitudeTransform of the infrastructure resource.
00353         osg::PositionAttitudeTransform *inf_pat = dynamic_cast<osg::PositionAttitudeTransform*>(infl->getModel()->getParent(0)->getParent(0));
00354         assert( inf_pat != NULL );
00355 
00356         inf_pat->removeChild( carg_at );
00357 
00358         //update hub if selected:
00359         if (tnl->isSelected())updateHudText(tnl);
00360         return true;
00361 
00362 }
00363 
00364 bool StateControl::unload(int Rt, int Ri, int C, double ts, double te){
00365         Cargo *cgu = dynamic_cast<Cargo*>( world[C] );
00366         Infrastructure *infu = dynamic_cast<Infrastructure*>( world[Ri] );
00367         Transport *tnu = dynamic_cast<Transport*>( world[Rt] );
00368         assert ( !(cgu == NULL) && !(infu == NULL) && !(tnu == NULL) );
00369 
00370         if( tnu->getCargo() != C){
00371                 cout << "Warning: (StateControl::Cargo()) Cargo " << cgu->getModel()->getName() << " is not on transport " << tnu->getModel()->getName() << endl;
00372                 #if STRICT
00373                         exit(1);
00374                 #endif
00375                 return false;
00376         }
00377 
00378         //ok, remove cargo, add to location:
00379         if( !infu->addCarg( ( tnu->clearCargo() ) ) ){
00380                 cout << "Warning: (StateControl::unload()) Failed to unload cargo " << cgu->getModel()->getName() << " from transport " << tnu->getModel()->getName() << " to location  " << infu->getModel()->getName() << endl; 
00381                 #if STRICT
00382                         exit(1);
00383                 #endif
00384                 return false;
00385         }
00386         
00387         //      OSG-tree manipulation
00388         //      Add the AutoTransform of the cargo to the PositionAttitudeTransform of the location
00389         osg::PositionAttitudeTransform *loc_pat = dynamic_cast<osg::PositionAttitudeTransform*>( infu->getModel()->getParent(0)->getParent(0) );
00390         assert( loc_pat != NULL );
00391 
00392         osg::AutoTransform *carg_at = dynamic_cast<osg::AutoTransform*>( cgu->getModel()->getParent(0) );
00393         assert( carg_at != NULL );
00394         
00395         carg_at->setPosition( osg::Vec3d( -20, 0, 20) );        //      Place the cargo besides the road.
00396 
00397         loc_pat->addChild( carg_at );
00398 
00399         //      Now remove it from PositionAttitudeTransform of the transport.
00400         osg::PositionAttitudeTransform *tr_pat = dynamic_cast<osg::PositionAttitudeTransform*>( tnu->getModel()->getParent(0)->getParent(0) );
00401         assert( tr_pat != NULL );       
00402 
00403         tr_pat->removeChild( carg_at );
00404 
00405         //update hub if selected:
00406         if ( tnu->isSelected() )
00407                 updateHudText(tnu);
00408 
00409         return true;
00410 }
00411 
00412 bool StateControl::setspdinviz(int R, double s){
00413         
00414         Infrastructure *tmpR = dynamic_cast<Infrastructure*>( world[R] );
00415         if (tmpR == NULL)       //R is not an infrastructure resource, perhapes it is a transport resource.
00416                 Transport *tmpR = dynamic_cast<Transport*>( world[R] );
00417         assert( tmpR != NULL);          
00418         
00419         bool ret = tmpR->setSpeed(s);   
00420         if( tmpR->isSelected() )
00421                 updateHudText(tmpR);
00422         
00423         return ret;
00424 }
00425 
00426 bool StateControl::trnew(int Rt, shared_ptr<string> desc, int Ri, double s){
00427 
00428         osg::Node* trModel = osgDB::readNodeFile(transmodel);
00429         assert( trModel != NULL );
00430         
00431         if( world[Rt] != NULL ){        
00432                 cout << "Warning: (StateControl::trnew()) Something with id " << Rt << " allready exists. No transport created." << endl;
00433                 #if STRICT
00434                         exit(1);
00435                 #endif
00436                 return false;
00437         }
00438 
00439         //create object and add to worldmap
00440         Transport* trans = new Transport(Rt,TRANSPORT, s, trModel);
00441         world[Rt]=trans;
00442 
00443         Infrastructure* loc = dynamic_cast<Infrastructure*>( world[Ri] );
00444         assert (loc != NULL);
00445 
00446         //      Get the location of the infrastructure resource
00447         osg::Vec3d locpos = loc->getPosition(); 
00448         
00449         //      Make a PositionAttitudeTransform and fill it with the position.
00450         osg::PositionAttitudeTransform* trpos = new osg::PositionAttitudeTransform();
00451         trpos->setPosition(locpos);
00452 
00453         //      Make our AutoTransform
00454         osg::AutoTransform* mytransform = new osg::AutoTransform();
00455         mytransform->setScale(osg::Vec3d(transscale,transscale,transscale));
00456 
00457         //      Add it to the OSG-Tree.
00458         mytransform->addChild(trModel);
00459         trpos->addChild(mytransform);
00460         osgRoot->addChild(trpos);
00461 
00462         //      Set the label and description.
00463         trans->initLabel();
00464         trans->setLabel(desc);
00465         trans->setDescription(*desc);
00466         
00467         return true;
00468 }
00469 
00470 bool StateControl::trdel(int Rt){
00471         Transport* trn = dynamic_cast<Transport*>( world[Rt] );
00472         assert( trn != NULL );
00473 
00474         if( trn->isSelected() ) 
00475                 deselectObject(trn);
00476         
00477         if(trn->getCargo() != -1){ 
00478                 cout << "Warning: (StateControl::trdel()) Transport " << (trn->getModel())->getName() << " was carrying cargo with id " << trn->getCargo() << endl;
00479                 #if STRICT
00480                  exit(1);
00481                 #endif
00482         }
00483         //      Remove the transport from the OSG-tree
00484         osg::PositionAttitudeTransform* trpos = dynamic_cast<osg::PositionAttitudeTransform*>( trn->getModel()->getParent(0)->getParent(0) );
00485         assert( trpos != NULL );
00486 
00487         osgRoot->removeChild( trpos );
00488 
00489         return true;
00490 }
00491 
00492 bool StateControl::newcarg(int C, shared_ptr<string> desc, int Ri){
00493 
00494         if( world[C] != NULL ){
00495                 cout << "Warning: (StateControl::newcarg()) Something with" << C << "allready exists, no cargo made" << endl;
00496                 #if STRICT
00497                         exit(1);
00498                 #endif
00499                 return false;
00500         }
00501 
00502         Infrastructure* loc = dynamic_cast<Infrastructure*>( world[Ri] );
00503         assert( loc != NULL );
00504 
00505         osg::Node* cgModel = osgDB::readNodeFile(cargomodel);
00506         assert( cgModel != NULL );
00507 
00508         //create object and add to worldmap, location
00509         Cargo* carg = new Cargo(C, CARGO, cgModel);
00510         world[C]=carg;
00511         loc->addCarg(C);
00512 
00513         //The cargo is going to be added to the infrastructure resource, so we take the PositionAttitudeTransform of the infrastructure resource.
00514         osg::PositionAttitudeTransform* cgpos = dynamic_cast<osg::PositionAttitudeTransform*>( loc->getModel()->getParent(0)->getParent(0) );
00515         assert( cgpos != NULL );        
00516 
00517         //Make our own AutoTransform, here we can include scaling. Also we can include rotation and position relative to the infrastructure resource it is placed upon.
00518         osg::AutoTransform* mytransform = new osg::AutoTransform();
00519         mytransform->setScale( osg::Vec3d( cargoscale, cargoscale, cargoscale) );
00520         mytransform->setPosition( osg::Vec3d( 20, 0, 20) );     //      Place the cargo besides the road.
00521 
00522         //      Add cargo to the OSG-tree.
00523         mytransform->addChild(cgModel);
00524         cgpos->addChild(mytransform);
00525 
00526         //      Add label and description. 
00527         carg->initLabel();
00528         carg->setLabel(desc);
00529     carg->setDescription(*desc);
00530     
00531         return true;
00532 }
00533 
00534 bool StateControl::rmcarg(int C){
00535         //      NOTE Cargo is removed regardless of its location in the OSG-tree.
00536         Cargo* carg = dynamic_cast<Cargo*>( world[C] );
00537         assert( carg != NULL );
00538 
00539         if( carg->isSelected() )
00540                 deselectObject(carg);   
00541 
00542         //      Remove the cargo from the OSG-tree
00543         osg::PositionAttitudeTransform* cgpos = dynamic_cast<osg::PositionAttitudeTransform*>( carg->getModel()->getParent(0)->getParent(0) );
00544         assert( cgpos != NULL );        
00545 
00546         osg::AutoTransform* carg_at = dynamic_cast<osg::AutoTransform*>( carg->getModel()->getParent(0) );
00547         assert( carg_at != NULL );      
00548 
00549         cgpos->removeChild( carg_at );
00550 
00551         return true;
00552 }
00553 
00554 bool StateControl::arc(int Ri_a, int Ri_b){
00555         Infrastructure *a = dynamic_cast<Infrastructure*>( world[Ri_a] );
00556         Infrastructure *b = dynamic_cast<Infrastructure*>( world[Ri_b] );
00557         assert( !(a == NULL) && !(b == NULL) );
00558 
00559         //check if there isn't an arc yet:
00560         if( a->addArc(Ri_b) )
00561           return true;  
00562         else {
00563                 cout<< "Warning: (StateControl::arc()) Arc between " << a->getModel()->getName() << " and " << b->getModel()->getName() << " allready exists." << endl;
00564                 #if STRICT
00565                         exit(1);
00566                 #endif
00567                 return false;
00568         }
00569 }
00570 
00571 bool StateControl::adjustTimeFlow(double adj){
00572         
00573         double oldTime = dis->getTimeFlow();
00574         if( oldTime <= 0 && adj <= 0 ) 
00575                 return dis->setTimeFlow(0.0);   
00576         else
00577                 return dis->setTimeFlow( (dis->getTimeFlow() + adj));
00578 }
00579 
00580 double StateControl::getTimeFlow(){
00581         return dis->getTimeFlow();
00582 }
00583 
00584 double StateControl::getTime(){
00585         return dis->getTime();
00586 }
00587 
00588 void StateControl::pause(){
00589         if(dis->getTimeFlow() != 0.0){
00590                 pausetime = dis->getTimeFlow();
00591                 dis->setTimeFlow(0.0);
00592         }
00593         else{
00594                 dis->setTimeFlow(pausetime);
00595                 pausetime = 0.0;
00596         }
00597         
00598         return;
00599 }
00600 
00601 int StateControl::selectObject(TraplasObject* obj){
00602         
00603         obj->select();
00604         
00605         //TODO  trackThis(obj->getModel());  //set camera to track this object. 
00606 
00607         //set text on the hud.  
00608         ui->setHUDText(obj->toString());        
00609         //put in vector:
00610         selected.push_back(obj);
00611         return selected.size();
00612 }
00613 
00614 TraplasObject* StateControl::getLastSelected(){
00615         if(!selected.empty())
00616                 return selected.at((selected.size()-1));
00617         else 
00618                 return NULL;
00619 }
00620 
00621 int StateControl::deselectObject(TraplasObject* obj){
00622 
00623         //TODO  trackThis(osgRoot); //set camera to track the root.     
00624 
00625         vector<TraplasObject*>::iterator findDeselect = find( selected.begin() , selected.end() , obj );
00626         
00627         if( findDeselect != selected.end() ) {
00628                 (*findDeselect)->select();
00629                 ui->rmHUDText(obj->toString());
00630                 selected.erase(findDeselect);
00631         }
00632         
00633         return (selected.size());       
00634 }
00635 
00636 void StateControl::clearSelection(){
00637         
00638         //TODO  trackThis(osgRoot); //set camera to track the root.
00639 
00640         //still have to toggle all their selection flags.
00641         for ( vector<TraplasObject*>::iterator rit = selected.begin() ; rit != selected.end() ; rit++ ) {
00642                 //toggle selection flag:
00643                 (*rit)->select();       
00644                 //remove text from hud:
00645                 ui->rmHUDText((*rit)->toString());              
00646         }
00647         selected.clear();
00648 }
00649 
00650 /*TODO tracking of selected objects.
00651 
00652 void StateControl::track(){
00653         cout<<"Here there be tracking!\n";
00654         view->selectCameraManipulatorByName(trackingCamName);
00655 
00656         //update the matrices in the cameranode.
00657 
00658         cout << "number 5 selected\n";
00659 }
00660         
00661 void StateControl::trackThis(osg::Node* n){
00662         cout << "We Select!\n";
00663 
00664         osgGA::NodeTrackerManipulator* tmpManip = NULL;
00665         cout << "Yay!\n";
00666         cout << trackingCamName << endl;
00667   tmpManip = (osgGA::NodeTrackerManipulator*) view->getCameraManipulatorByName(trackingCamName);
00668   cout << "Hierzo! 1\n";
00669   if(tmpManip != NULL){
00670     tmpManip->setTrackNode( n);
00671   }
00672 }
00673 */
00674 
00675 void StateControl::updateHudText(TraplasObject* upd){
00676   ui->rmHUDText(upd->toString());
00677   ui->setHUDText(upd->toString());
00678 }

Generated on Mon Jun 19 10:22:04 2006 for TraplasVisualisation by  doxygen 1.4.4