EventHandler.h
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #ifndef SCENE_EVENT_HANDLER
00014 #define SCENE_EVENT_HANDLER
00015 
00016 #include "SimulatedIAUV.h"
00017 #include "ConfigXMLParser.h"
00018 #include "TrajectoryVisualization.h"
00019 
00020 class SceneEventHandler : public osgGA::GUIEventHandler
00021 {
00022 private:
00023   osg::ref_ptr<osgOceanScene> _scene;
00024   SceneBuilder * _sceneBuilder;
00025   osg::ref_ptr<TextHUD> _textHUD;
00026   std::vector<osg::ref_ptr<osgWidget::Window> > _windows;
00027   ConfigFile *_config;
00028 
00029   bool draw_frames_;
00030 public:
00031   //vehicle track indicates whether the camera must automatically track the vehicle node
00032   SceneEventHandler(std::vector<osg::ref_ptr<osgWidget::Window> > &windows, TextHUD* textHUD,
00033                     SceneBuilder * sceneBuilder, ConfigFile *config) :
00034       _scene(sceneBuilder->getScene()), _sceneBuilder(sceneBuilder), _textHUD(textHUD), _windows(windows), draw_frames_(false)
00035   {
00036     _textHUD->setSceneText("Clear Blue Sky");
00037     _config = config;
00038   }
00039 
00040   virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
00041   {
00042     switch (ea.getEventType())
00043     {
00044       case (osgGA::GUIEventAdapter::KEYUP):
00045       {
00046 
00047         if (ea.getKey() == '1')
00048         {
00049           ROS_INFO("Switch to Clear Blue Sky");
00050           _scene->changeScene(osgOceanScene::CLEAR);
00051           _scene->getOceanScene()->setUnderwaterFog(
00052               _config->fogDensity, osg::Vec4f(_config->fogColor[0], _config->fogColor[1], _config->fogColor[2], 1));
00053           _scene->getOceanScene()->setUnderwaterDiffuse(
00054               osg::Vec4f(_config->color[0], _config->color[1], _config->color[2], 1));
00055           _scene->getOceanScene()->setUnderwaterAttenuation(
00056               osg::Vec3f(_config->attenuation[0], _config->attenuation[1], _config->attenuation[2]));
00057 
00058           _textHUD->setSceneText("Clear Blue Sky");
00059           return false;
00060         }
00061         else if (ea.getKey() == '2')
00062         {
00063           _scene->changeScene(osgOceanScene::DUSK);
00064           _textHUD->setSceneText("Dusk");
00065           _scene->getOceanScene()->setUnderwaterFog(
00066               _config->fogDensity, osg::Vec4f(_config->fogColor[0], _config->fogColor[1], _config->fogColor[2], 1));
00067           _scene->getOceanScene()->setUnderwaterDiffuse(
00068               osg::Vec4f(_config->color[0], _config->color[1], _config->color[2], 1));
00069           _scene->getOceanScene()->setUnderwaterAttenuation(
00070               osg::Vec3f(_config->attenuation[0], _config->attenuation[1], _config->attenuation[2]));
00071           return false;
00072         }
00073         else if (ea.getKey() == '3')
00074         {
00075           _scene->changeScene(osgOceanScene::CLOUDY);
00076           _textHUD->setSceneText("Pacific Cloudy");
00077           _scene->getOceanScene()->setUnderwaterFog(
00078               _config->fogDensity, osg::Vec4f(_config->fogColor[0], _config->fogColor[1], _config->fogColor[2], 1));
00079           _scene->getOceanScene()->setUnderwaterDiffuse(
00080               osg::Vec4f(_config->color[0], _config->color[1], _config->color[2], 1));
00081           _scene->getOceanScene()->setUnderwaterAttenuation(
00082               osg::Vec3f(_config->attenuation[0], _config->attenuation[1], _config->attenuation[2]));
00083           return false;
00084         }
00085         else if (ea.getKey() == 'c')
00086         {
00087           for (unsigned int i = 0; i < _windows.size(); i++)
00088             if (_windows[i]->isVisible())
00089               _windows[i]->hide();
00090             else
00091               _windows[i]->show();
00092           return false;
00093         }
00094         else if (ea.getKey() == 'f')
00095         {
00096           //Search for 'switch_frames' nodes and toggle their values
00097           findNodeVisitor finder("switch_frames");
00098           _scene->localizedWorld->accept(finder);
00099           std::vector<osg::Node*> node_list = finder.getNodeList();
00100           (draw_frames_) ? draw_frames_ = false : draw_frames_ = true;
00101           for (unsigned int i = 0; i < node_list.size(); i++)
00102             (draw_frames_) ? node_list[i]->asSwitch()->setAllChildrenOn() :
00103                 node_list[i]->asSwitch()->setAllChildrenOff();
00104         }
00105         else if (ea.getKey() == 't')
00106         {
00107           //Search for 'switch_trajectory' nodes and toggle their values
00108           findNodeVisitor finder("switch_trajectory");
00109           _scene->localizedWorld->accept(finder);
00110           std::vector<osg::Node*> node_list = finder.getNodeList();
00111           (draw_frames_) ? draw_frames_ = false : draw_frames_ = true;
00112           for (unsigned int i = 0; i < node_list.size(); i++)
00113             (draw_frames_) ? node_list[i]->asSwitch()->setAllChildrenOn() :
00114                 node_list[i]->asSwitch()->setAllChildrenOff();
00115         }
00116         else if (ea.getKey() == 'r')
00117         {
00118           //search catchable objects and get them back to their original positions
00119           for (unsigned int i = 0; i < _sceneBuilder->objects.size(); i++)
00120           {
00121 
00122             osg::ref_ptr<NodeDataType> data = dynamic_cast<NodeDataType*>(_sceneBuilder->objects[i]->getUserData());
00123             if(data->catchable)
00124             { //No need to restart static objects
00125               osg::Matrixd matrix;
00126               matrix.makeRotate(
00127                 osg::Quat(data->originalRotation[0], osg::Vec3d(1, 0, 0), data->originalRotation[1],
00128                           osg::Vec3d(0, 1, 0), data->originalRotation[2], osg::Vec3d(0, 0, 1)));
00129               matrix.setTrans(data->originalPosition[0], data->originalPosition[1], data->originalPosition[2]);
00130 
00131               if(!data->rigidBody)
00132               { //No physics
00133                 _sceneBuilder->objects[i]->getParent(0)->getParent(0)->asTransform()->asMatrixTransform()->setMatrix(matrix);
00134  
00135                 //just in case an object picker picked it
00136                 _scene->localizedWorld->addChild(_sceneBuilder->objects[i]->getParent(0)->getParent(0));
00137                 _sceneBuilder->objects[i]->getParent(0)->getParent(0)->getParent(0)->removeChild(_sceneBuilder->objects[i]->getParent(0)->getParent(0));
00138               }
00139               if(data->rigidBody)
00140               {//Physics restart
00141                 
00142                 //Reset position for kinematic & static objects
00143                 _sceneBuilder->objects[i]->getParent(0)->getParent(0)->asTransform()->asMatrixTransform()->setMatrix(matrix);
00144                 _scene->localizedWorld->addChild(_sceneBuilder->objects[i]->getParent(0)->getParent(0));
00145                 _sceneBuilder->objects[i]->getParent(0)->getParent(0)->getParent(0)->removeChild(_sceneBuilder->objects[i]->getParent(0)->getParent(0));
00146 
00147                 //Get object position in OSG world to move it to Bullet
00148                 boost::shared_ptr<osg::Matrix> mat = getWorldCoords(_sceneBuilder->objects[i]->getParent(0)->getParent(0));
00149 
00150                 //Unset STATIC flag (catched objects)
00151                 data->rigidBody->setCollisionFlags(
00152                     data->rigidBody->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT );
00153 
00154                 //Reset dynamic properties
00155                 data->rigidBody->setCenterOfMassTransform(osgbCollision::asBtTransform(*mat));
00156                 data->rigidBody->clearForces();
00157                 data->rigidBody->setLinearVelocity(btVector3(0,0,0));
00158                 data->rigidBody->setAngularVelocity(btVector3(0,0,0));
00159               }
00160             }
00161           }
00162 
00163           //Search for vehicles and set them back to initial pose
00164 
00165           for (unsigned int i = 0; i < _sceneBuilder->iauvFile.size(); i++)
00166           {
00167             for (std::list<Vehicle>::iterator cfgVehicle = _config->vehicles.begin(); cfgVehicle != _config->vehicles.end(); cfgVehicle++)
00168               if (cfgVehicle->name == _sceneBuilder->iauvFile[i]->name){
00169                 _sceneBuilder->iauvFile[i]->setVehiclePosition(cfgVehicle->position[0], cfgVehicle->position[1],
00170                   cfgVehicle->position[2], cfgVehicle->orientation[0],cfgVehicle->orientation[1], cfgVehicle->orientation[2]);
00171 
00172                 _sceneBuilder->iauvFile[i]->urdf->setJointPosition(cfgVehicle->jointValues);
00173               }
00174 
00175             //restart object pickers
00176             for (unsigned int j = 0; j<_sceneBuilder->iauvFile[i]->object_pickers.size();j++){
00177               osg::ref_ptr<ObjectPickerUpdateCallback> callback =
00178                 dynamic_cast<ObjectPickerUpdateCallback*>(_sceneBuilder->iauvFile[i]->object_pickers[j].trackNode->getUpdateCallback());
00179               callback->picked = false;
00180             }
00181           }
00182 
00183           //Search for trajectory updaters and clearwaypoints?
00184         }
00185         else if (ea.getKey() == 'y') 
00186         { //Clear trajectories
00187           for (unsigned int i = 0; i < _sceneBuilder->trajectories.size(); i++)
00188           {
00189             osg::ref_ptr<TrajectoryUpdateCallback> callback =
00190               dynamic_cast<TrajectoryUpdateCallback*>(_sceneBuilder->trajectories[i]->getUpdateCallback()); 
00191             callback->reset();
00192           }
00193         }
00194 
00195       }
00196       default:
00197         return false;
00198     }
00199   }
00200 
00201   void getUsage(osg::ApplicationUsage& usage) const
00202   {
00203   }
00204 
00205 };
00206 
00207 #endif


uwsim
Author(s): Mario Prats , Javier Perez
autogenerated on Fri Aug 28 2015 13:28:58