[10] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
[8] | 5 | // created: 2013/03/27
|
---|
| 6 | // filename: Gui.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: classe definissant une Gui
|
---|
| 14 | //
|
---|
| 15 | /*********************************************************************/
|
---|
| 16 | #ifdef GL
|
---|
| 17 |
|
---|
| 18 | #include "Gui_impl.h"
|
---|
| 19 | #include "Gui.h"
|
---|
| 20 | #include "Simulator.h"
|
---|
| 21 | #include "GenericObject.h"
|
---|
| 22 | #include "Model.h"
|
---|
| 23 | #include "Model_impl.h"
|
---|
| 24 | #include "AnimPoursuite.h"
|
---|
| 25 | #include <Object.h>
|
---|
| 26 | #include <Euler.h>
|
---|
| 27 | #include <irrlicht.h>
|
---|
| 28 | #include <unistd.h>
|
---|
| 29 | #include <sstream>
|
---|
| 30 |
|
---|
| 31 | using namespace irr;
|
---|
| 32 | using namespace irr::video;
|
---|
| 33 | using namespace irr::core;
|
---|
| 34 | using namespace irr::scene;
|
---|
| 35 | using namespace irr::gui;
|
---|
| 36 | using namespace flair::core;
|
---|
| 37 | using namespace flair::simulator;
|
---|
| 38 |
|
---|
| 39 | class MyEventReceiver : public IEventReceiver
|
---|
| 40 | {
|
---|
| 41 | public:
|
---|
| 42 | // This is the one method that we have to implement
|
---|
| 43 | virtual bool OnEvent(const SEvent& event)
|
---|
| 44 | {
|
---|
| 45 | // Remember whether each key is down or up
|
---|
| 46 | if (event.EventType == EET_KEY_INPUT_EVENT )
|
---|
| 47 | KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
|
---|
| 48 |
|
---|
| 49 | //Send all other events to camera
|
---|
| 50 | if (event.EventType == EET_MOUSE_INPUT_EVENT && camera)
|
---|
| 51 | //return camera->OnEvent(event);
|
---|
| 52 | return camera->MouseMoved(event,cursorControl->getRelativePosition());
|
---|
| 53 |
|
---|
| 54 | if(model)
|
---|
| 55 | return model->OnEvent(event);
|
---|
| 56 |
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // This is used to check whether a key is being held down
|
---|
| 61 | virtual bool IsKeyDown(EKEY_CODE keyCode)
|
---|
| 62 | {
|
---|
| 63 | if(KeyIsDown[keyCode]==true)
|
---|
| 64 | {
|
---|
| 65 | KeyIsDown[keyCode]=false;
|
---|
| 66 | return true;
|
---|
| 67 | }
|
---|
| 68 | else
|
---|
| 69 | {
|
---|
| 70 | return false;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | MyEventReceiver(ICursorControl* cursorControl)
|
---|
| 75 | {
|
---|
| 76 | this->cursorControl=cursorControl;
|
---|
| 77 | camera=NULL;
|
---|
| 78 | model=NULL;
|
---|
| 79 | for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
|
---|
| 80 | KeyIsDown[i] = false;
|
---|
| 81 | }
|
---|
| 82 | void SetCamera(AnimPoursuite* camera)
|
---|
| 83 | {
|
---|
| 84 | this->camera=camera;
|
---|
| 85 | }
|
---|
| 86 | void SetModel(Model* model)
|
---|
| 87 | {
|
---|
| 88 | this->model=model;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private:
|
---|
| 92 | // We use this array to store the current state of each key
|
---|
| 93 | bool KeyIsDown[KEY_KEY_CODES_COUNT];
|
---|
| 94 | AnimPoursuite* camera;
|
---|
| 95 | Model* model;
|
---|
| 96 | ICursorControl* cursorControl;
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | Gui_impl::Gui_impl(Gui* self,int app_width, int app_height,int scene_width, int scene_height,std::string media_path,E_DRIVER_TYPE driver_type)
|
---|
| 100 | {
|
---|
| 101 | this->self=self;
|
---|
| 102 | dbtFile_w=NULL;
|
---|
| 103 | dbtFile_r=NULL;
|
---|
| 104 | this->media_path=media_path;
|
---|
| 105 | this->scene_width=scene_width;
|
---|
| 106 | this->scene_height=scene_height;
|
---|
| 107 |
|
---|
| 108 | device =createDevice(driver_type, dimension2d<u32>(app_width, app_height),16, false, false, false);
|
---|
| 109 | receiver=new MyEventReceiver(device->getCursorControl());
|
---|
| 110 | device->setEventReceiver(receiver);
|
---|
| 111 | device->getLogger()->setLogLevel(ELL_NONE);
|
---|
| 112 |
|
---|
| 113 | device->getCursorControl()->setVisible(false);
|
---|
| 114 | device->setResizable(false);
|
---|
| 115 |
|
---|
| 116 | //font = device->getGUIEnvironment()->getBuiltInFont();
|
---|
| 117 | driver = device->getVideoDriver();
|
---|
| 118 | smgr = device->getSceneManager();
|
---|
| 119 |
|
---|
| 120 | smgr->setAmbientLight(video::SColorf(1,1,1));
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | env = device->getGUIEnvironment();
|
---|
| 125 | IGUISkin* skin = env->getSkin();
|
---|
| 126 | font = env->getFont("./fonthaettenschweiler.bmp");
|
---|
| 127 |
|
---|
| 128 | if (font)
|
---|
| 129 | skin->setFont(font);
|
---|
| 130 |
|
---|
| 131 | // create menu
|
---|
| 132 |
|
---|
| 133 | IGUIContextMenu* menu = env->addMenu();
|
---|
| 134 | menu->setMinSize(core::dimension2du(640,20));
|
---|
| 135 | menu->addItem(L"File", -1, true, true);
|
---|
| 136 | menu->addItem(L"View", -1, true, true);
|
---|
| 137 | menu->addItem(L"Camera", -1, true, true);
|
---|
| 138 | menu->addItem(L"Help", -1, true, true);
|
---|
| 139 |
|
---|
| 140 | // disable alpha
|
---|
| 141 |
|
---|
| 142 | for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
|
---|
| 143 | {
|
---|
| 144 | video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
|
---|
| 145 | col.setAlpha(255);
|
---|
| 146 | env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
|
---|
| 147 | }
|
---|
| 148 | */
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | Gui_impl::~Gui_impl()
|
---|
| 152 | {
|
---|
| 153 | //printf("del Gui_impl\n");
|
---|
| 154 | device->drop();
|
---|
| 155 |
|
---|
| 156 | delete receiver;
|
---|
| 157 | // printf("del Gui_impl ok\n");
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void Gui_impl::setMesh(std::string file,vector3df position,vector3df rotation,vector3df scale)
|
---|
| 161 | {
|
---|
| 162 | IAnimatedMesh* mesh = smgr->getMesh(file.c_str());
|
---|
| 163 |
|
---|
| 164 | if (!mesh) {
|
---|
| 165 | // model could not be loaded
|
---|
| 166 | self->Err("Model %s could not be loaded\n",file.c_str());
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
|
---|
| 172 | node->setPosition(position);
|
---|
| 173 | rotation+=irr::core::vector3df(90,0,Euler::ToDegree(getSimulator()->Yaw()));
|
---|
| 174 | node->setRotation(rotation);
|
---|
| 175 | for(int i=0; i<node->getMaterialCount();i++){
|
---|
| 176 | node->getMaterial(i).TextureLayer->TextureWrapU = video::ETC_REPEAT;
|
---|
| 177 | node->getMaterial(i).TextureLayer->TextureWrapV = video::ETC_REPEAT;
|
---|
| 178 | }
|
---|
| 179 | //Ceillig
|
---|
| 180 | //node->getMaterial(0).getTextureMatrix(0).setTextureScale(scale.X/2.0,scale.Z/2.0);
|
---|
| 181 | //Walls
|
---|
| 182 | node->getMaterial(1).getTextureMatrix(0).setTextureScale(1/(scale.Y/2.5),1/(scale.Z/2.5));
|
---|
| 183 | //Floor
|
---|
| 184 | node->getMaterial(2).getTextureMatrix(0).setTextureScale(1/(scale.X/20.0),1/(scale.Z/20.0));
|
---|
| 185 |
|
---|
| 186 | node->setScale(scale);
|
---|
| 187 | //selector
|
---|
| 188 | selector = smgr->createOctreeTriangleSelector(node->getMesh(), node, 128);
|
---|
| 189 | node->setTriangleSelector(selector);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void Gui_impl::RunGui(std::vector<Model*> models,std::vector<GenericObject*> objects)
|
---|
| 193 | {
|
---|
| 194 | int lastFPS = -1;
|
---|
| 195 | int cam_id=0;
|
---|
| 196 |
|
---|
| 197 | receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
|
---|
| 198 | receiver->SetModel(models.at(cam_id));
|
---|
| 199 |
|
---|
| 200 | for(size_t i=0;i<models.size();i++)
|
---|
| 201 | {
|
---|
| 202 | models.at(i)->Draw();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | for(size_t i=0;i<models.size();i++)
|
---|
| 206 | {
|
---|
| 207 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(selector);
|
---|
| 208 | for(size_t j=0;j<objects.size();j++)
|
---|
| 209 | {
|
---|
| 210 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(objects.at(j)->TriangleSelector());
|
---|
| 211 | }
|
---|
| 212 | for(size_t j=0;j<models.size();j++)
|
---|
| 213 | {
|
---|
| 214 | if(i==j) continue;
|
---|
| 215 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(models.at(j)->pimpl_->TriangleSelector());
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | selector->drop(); // As soon as we're done with the selector, drop it.*/
|
---|
| 220 |
|
---|
| 221 | //wait all models to be started
|
---|
| 222 | for(size_t i=0;i<models.size();i++)
|
---|
| 223 | {
|
---|
| 224 | models.at(i)->pimpl_->SynchronizationPoint();
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | setWindowCaption(models.at(0),0);
|
---|
| 228 |
|
---|
| 229 | while(device->run())
|
---|
| 230 | {
|
---|
| 231 | if(dbtFile_r!=NULL)//rejeu
|
---|
| 232 | {
|
---|
| 233 | takeScreenshot();//on enregistre l'image precedente
|
---|
| 234 | road_time_t time;
|
---|
| 235 | road_timerange_t tr = 0;
|
---|
| 236 | if(read_hdfile(dbtFile_r,(void*)dbtbuf,&time,&tr)!=0)
|
---|
| 237 | {
|
---|
| 238 | vector3df vect;
|
---|
| 239 | char *buf=dbtbuf;
|
---|
| 240 | for(size_t i=0;i<models.size();i++)
|
---|
| 241 | {
|
---|
| 242 | models.at(i)->ReaddbtBuf(buf);
|
---|
| 243 | buf+=models.at(i)->dbtSize();
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | else
|
---|
| 247 | {
|
---|
| 248 | //Printf("fin play\n");
|
---|
| 249 | close_hdfile(dbtFile_r);
|
---|
| 250 | dbtFile_r=NULL;
|
---|
| 251 | free(dbtbuf);
|
---|
| 252 | for(size_t i=0;i<models.size();i++)
|
---|
| 253 | {
|
---|
| 254 | models.at(i)->pimpl_->Resume();
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | else//mode normal
|
---|
| 259 | {
|
---|
| 260 | for(size_t i=0;i<models.size();i++)
|
---|
| 261 | {
|
---|
| 262 | models.at(i)->pimpl_->UpdatePos();
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | driver->beginScene(true, true, video::SColor(255,200,200,200));
|
---|
| 267 |
|
---|
| 268 | //vue poursuite
|
---|
| 269 | smgr->setActiveCamera(models.at(cam_id)->pimpl_->camera);
|
---|
| 270 | driver->setViewPort(core::rect<s32>(0,0,scene_width,scene_height));
|
---|
| 271 | smgr->drawAll();//commente voir plus bas
|
---|
| 272 | /*
|
---|
| 273 | env->drawAll();
|
---|
| 274 | if (font)
|
---|
| 275 | {
|
---|
| 276 | font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
|
---|
| 277 | core::rect<s32>(130,10,300,50),
|
---|
| 278 | video::SColor(255,255,255,255));
|
---|
| 279 | }
|
---|
| 280 | else
|
---|
| 281 | {
|
---|
| 282 | printf("err\n");
|
---|
| 283 | }
|
---|
| 284 | device->setWindowCaption(L"toto");*/
|
---|
| 285 |
|
---|
| 286 | if(dbtFile_r==NULL)//mode normal
|
---|
| 287 | {
|
---|
| 288 | for(size_t i=0;i<models.size();i++)
|
---|
| 289 | {
|
---|
| 290 | models.at(i)->pimpl_->CheckCollision();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | for(size_t i=0;i<models.size();i++)
|
---|
| 295 | {
|
---|
| 296 | models.at(i)->ProcessUpdate(NULL);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | //on fait ca ici, devrait etre un peu plus haut
|
---|
| 300 | //mais a priori souci avec models.at(i)->pimpl_->CheckCollision(); (setelipsoid?)
|
---|
| 301 | smgr->setActiveCamera(models.at(cam_id)->pimpl_->camera);
|
---|
| 302 | driver->setViewPort(core::rect<s32>(0,0,scene_width,scene_height));
|
---|
| 303 | smgr->drawAll();
|
---|
| 304 |
|
---|
| 305 | driver->endScene();
|
---|
| 306 |
|
---|
| 307 | int fps =driver->getFPS();
|
---|
| 308 | //printf("fps %i\n",fps);
|
---|
| 309 | if (lastFPS != fps)
|
---|
| 310 | {
|
---|
| 311 | setWindowCaption(models.at(cam_id),fps);
|
---|
| 312 | lastFPS = fps;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | if(receiver->IsKeyDown(KEY_PRIOR))
|
---|
| 316 | {
|
---|
| 317 | cam_id++;
|
---|
| 318 | if(cam_id>=(int)models.size()) cam_id=0;
|
---|
| 319 | receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
|
---|
| 320 | receiver->SetModel(models.at(cam_id));
|
---|
| 321 | setWindowCaption(models.at(cam_id),fps);
|
---|
| 322 | }
|
---|
| 323 | if(receiver->IsKeyDown(KEY_NEXT))
|
---|
| 324 | {
|
---|
| 325 | cam_id--;
|
---|
| 326 | if(cam_id<0) cam_id=models.size()-1;
|
---|
| 327 | receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
|
---|
| 328 | receiver->SetModel(models.at(cam_id));
|
---|
| 329 | setWindowCaption(models.at(cam_id),fps);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | //enregistrement DBT
|
---|
| 333 | if(receiver->IsKeyDown(KEY_KEY_R) && dbtFile_w==NULL)
|
---|
| 334 | {
|
---|
| 335 | dbtFile_w = inithdFile((char*)"./record.dbt",UAV,dbtSize(models));
|
---|
| 336 | dbtbuf=(char*)malloc(dbtSize(models));
|
---|
| 337 | }
|
---|
| 338 | if(receiver->IsKeyDown(KEY_KEY_S) && dbtFile_w!=NULL)
|
---|
| 339 | {
|
---|
| 340 | close_hdfile(dbtFile_w);
|
---|
| 341 | dbtFile_w=NULL;
|
---|
| 342 | free(dbtbuf);
|
---|
| 343 | //rt_printf("stop rec\n");
|
---|
| 344 | }
|
---|
| 345 | if(dbtFile_w!=NULL)
|
---|
| 346 | {
|
---|
| 347 | Time time=GetTime();
|
---|
| 348 | vector3df vect;
|
---|
| 349 | char *buf=dbtbuf;
|
---|
| 350 |
|
---|
| 351 | for(size_t i=0;i<models.size();i++)
|
---|
| 352 | {
|
---|
| 353 | models.at(i)->WritedbtBuf(buf);
|
---|
| 354 | buf+=models.at(i)->dbtSize();
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | write_hdfile(dbtFile_w,dbtbuf,(road_time_t)(time/1000),(road_timerange_t)(time%1000),dbtSize(models));
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | //lecture dbt
|
---|
| 361 | if(receiver->IsKeyDown(KEY_KEY_P) && dbtFile_r==NULL)
|
---|
| 362 | {
|
---|
| 363 | dbtFile_r = open_hdfile((char*)"./record.dbt",READ_MODE);
|
---|
| 364 | dbtbuf=(char*)malloc(dbtSize(models));
|
---|
| 365 | //on suspend les models pour ne pas interferer
|
---|
| 366 | for(size_t i=0;i<models.size();i++)
|
---|
| 367 | {
|
---|
| 368 | models.at(i)->pimpl_->Suspend();
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | if(receiver->IsKeyDown(KEY_KEY_S) && dbtFile_r!=NULL)
|
---|
| 372 | {
|
---|
| 373 | //rt_printf("stop play\n");
|
---|
| 374 | close_hdfile(dbtFile_r);
|
---|
| 375 | dbtFile_r=NULL;
|
---|
| 376 | free(dbtbuf);
|
---|
| 377 | //on resume les models
|
---|
| 378 | for(size_t i=0;i<models.size();i++)
|
---|
| 379 | {
|
---|
| 380 | models.at(i)->pimpl_->Resume();
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | receiver->SetCamera(NULL);
|
---|
| 387 | receiver->SetModel(NULL);
|
---|
| 388 |
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | void Gui_impl::setWindowCaption(Object* object, int fps)
|
---|
| 392 | {
|
---|
| 393 | std::ostringstream text;
|
---|
| 394 | text << "Cam: " << object->ObjectName().c_str() << ", Kbd: " << object->ObjectName().c_str() << ", FPS: " << fps;
|
---|
| 395 |
|
---|
| 396 | device->setWindowCaption(stringw(text.str().c_str()).c_str());
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | void Gui_impl::takeScreenshot(void)
|
---|
| 400 | {
|
---|
| 401 | static int cpt=0;
|
---|
| 402 | //get image from the last rendered frame
|
---|
| 403 | IImage* const image = driver->createScreenShot();
|
---|
| 404 | if (image) //should always be true, but you never know. ;)
|
---|
| 405 | {
|
---|
| 406 | //construct a filename, consisting of local time and file extension
|
---|
| 407 | c8 filename[64];
|
---|
| 408 | //snprintf(filename, 64, "screenshot_%u.png", device->getTimer()->getRealTime());
|
---|
| 409 | snprintf(filename, 64, "screenshot_%u.png", cpt);
|
---|
| 410 | cpt++;
|
---|
| 411 | //write screenshot to file
|
---|
| 412 | if (!driver->writeImageToFile(image, filename))
|
---|
| 413 | device->getLogger()->log(L"Failed to take screenshot.", ELL_WARNING);
|
---|
| 414 |
|
---|
| 415 | //Don't forget to drop image since we don't need it anymore.
|
---|
| 416 | image->drop();
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | size_t Gui_impl::dbtSize(std::vector<Model*> models)
|
---|
| 421 | {
|
---|
| 422 | size_t size=0;
|
---|
| 423 | for(size_t i=0;i<models.size();i++)
|
---|
| 424 | {
|
---|
| 425 | size+=models.at(i)->dbtSize();
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | return size;
|
---|
| 429 | }
|
---|
| 430 | #endif //GL
|
---|