00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "flexilog.h"
00013 #include "eventhandler.h"
00014 #include "kernel.h"
00015 #include "iomanager.h"
00016 #include "manipmanager.h"
00017 #include "generalmanager.h"
00018 #include "bvh.h"
00019 #include "guienvironment.h"
00020 #include "camera.h"
00021 #include "kernelmodule.h"
00022 #include "evtstructs.h"
00023
00024 using namespace VRUT;
00025
00026
00027 EventHandler::EventHandler()
00028 : wxEvtHandler(),
00029 timer(this),
00030 cloneSceneDataParamID(wxT("kernel"), wxT("cloneSceneData"), wxT("0")),
00031 evtsPerIterationParamID(wxT("kernel"), wxT("evtsPerIteration"), wxT("0")),
00032 evtCheckIntervalParamID(wxT("kernel"), wxT("evtCheckInterval"), wxT("0")),
00033 logSyncEveryMsgParamID(wxT("kernel"), wxT("logSyncEveryMsg"), wxT("0"))
00034 {
00035 wxASSERT_MSG(evtQueue.IsOk(), wxT("<EventHandler>Message queue initialization failed"));
00036
00037 cloneSceneData = false;
00038 wxCommandEvent paramEvt = Event::GET_EVT_PARAM_REGISTER(cloneSceneDataParamID, wxT("0"));
00039 PostEvent(paramEvt);
00040 GUIEnvironment::GUIParamInfo pi(cloneSceneDataParamID, GUIEnvironment::GUIParamInfo::GUI_TYPE_CHECKBOX);
00041 wxCommandEvent paramGuiEvt = Event::GET_EVT_PARAM_GUIREGISTER(pi, wxT("0"));
00042 PostEvent(paramGuiEvt);
00043
00044 evtsPerIteration = 128;
00045 wxCommandEvent paramEvt2 = Event::GET_EVT_PARAM_REGISTER(evtsPerIterationParamID, wxT("128"));
00046 PostEvent(paramEvt2);
00047 GUIEnvironment::GUIParamInfo pi2(evtsPerIterationParamID, GUIEnvironment::GUIParamInfo::GUI_TYPE_SPINCONTROL);
00048 pi2.minval = 1;
00049 pi2.maxval = 10000;
00050 wxCommandEvent paramGuiEvt2 = Event::GET_EVT_PARAM_GUIREGISTER(pi2, wxT("128"));
00051 PostEvent(paramGuiEvt2);
00052
00053 evtCheckInterval = 40;
00054 wxCommandEvent paramEvt3 = Event::GET_EVT_PARAM_REGISTER(evtCheckIntervalParamID, wxT("40"));
00055 PostEvent(paramEvt3);
00056 GUIEnvironment::GUIParamInfo pi3(evtCheckIntervalParamID, GUIEnvironment::GUIParamInfo::GUI_TYPE_SLIDER);
00057 pi3.minval = 1;
00058 pi3.maxval = 1000;
00059 wxCommandEvent paramGuiEvt3 = Event::GET_EVT_PARAM_GUIREGISTER(pi3, wxT("40"));
00060 PostEvent(paramGuiEvt3);
00061
00062 wxCommandEvent paramEvt4 = Event::GET_EVT_PARAM_REGISTER(logSyncEveryMsgParamID, wxT("0"));
00063 PostEvent(paramEvt4);
00064 GUIEnvironment::GUIParamInfo pi4(logSyncEveryMsgParamID, GUIEnvironment::GUIParamInfo::GUI_TYPE_CHECKBOX);
00065 wxCommandEvent paramGuiEvt4 = Event::GET_EVT_PARAM_GUIREGISTER(pi4, wxT("0"));
00066 PostEvent(paramGuiEvt4);
00067
00068 timer.Start(evtCheckInterval);
00069
00071 KERNEL->GetMainWindow()->Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(EventHandler::onIdle), (wxObject *)NULL, this);
00072 Connect(wxID_ANY, wxEVT_TIMER, wxTimerEventHandler(EventHandler::onTimer), (wxObject *)NULL, this);
00073 Connect(wxID_ANY, Event::EVT_EXIT, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00074 Connect(wxID_ANY, Event::EVT_LOG, wxCommandEventHandler(EventHandler::onLogEvent), (wxObject *)NULL, this);
00075 Connect(wxID_ANY, Event::EVT_LOG_LEVEL_SET, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00076 Connect(wxID_ANY, Event::EVT_MODULE_PREFER, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00077 Connect(wxID_ANY, Event::EVT_MODULE_FORBID, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00078 Connect(wxID_ANY, Event::EVT_MODULE_RELEASE, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00079 Connect(wxID_ANY, Event::EVT_MODULE_RUN, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00080 Connect(wxID_ANY, Event::EVT_PARAM_SET_DO, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00081 Connect(wxID_ANY, Event::EVT_PARAM_SET, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00082 Connect(wxID_ANY, Event::EVT_PARAM_REGISTER, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00083 Connect(wxID_ANY, Event::EVT_PARAM_UNREGISTER, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00084
00086 Connect(wxID_ANY, wxEVT_KEY_DOWN, wxKeyEventHandler(EventHandler::onKeyEvent), (wxObject *)NULL, this);
00087 Connect(wxID_ANY, wxEVT_KEY_UP, wxKeyEventHandler(EventHandler::onKeyEvent), (wxObject *)NULL, this);
00088
00090
00091 Connect(wxID_ANY, wxEVT_ENTER_WINDOW, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00092 Connect(wxID_ANY, wxEVT_LEAVE_WINDOW, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00093 Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00094 Connect(wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00095 Connect(wxID_ANY, wxEVT_LEFT_DCLICK, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00096 Connect(wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00097 Connect(wxID_ANY, wxEVT_RIGHT_UP, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00098 Connect(wxID_ANY, wxEVT_RIGHT_DCLICK, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00099 Connect(wxID_ANY, wxEVT_MIDDLE_DOWN, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00100 Connect(wxID_ANY, wxEVT_MIDDLE_UP, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00101 Connect(wxID_ANY, wxEVT_MIDDLE_DCLICK, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00102 Connect(wxID_ANY, wxEVT_MOTION, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00103 Connect(wxID_ANY, wxEVT_MOUSEWHEEL, wxMouseEventHandler(EventHandler::onMouseEvent), (wxObject *)NULL, this);
00104
00106 Connect(wxID_ANY, Event::EVT_SCENE_RAY_PICK, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00107 Connect(wxID_ANY, Event::EVT_SCENE_IMAGE_ADD, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00108 Connect(wxID_ANY, Event::EVT_SCENE_MATERIAL_ADD, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00109 Connect(wxID_ANY, Event::EVT_SCENE_GEOMETRY_ADD, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00110 Connect(wxID_ANY, Event::EVT_SCENE_NODE_COPY, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00111 Connect(wxID_ANY, Event::EVT_SCENE_NODE_MOVE, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00112 Connect(wxID_ANY, Event::EVT_SCENE_NODE_INSERT, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00113 Connect(wxID_ANY, Event::EVT_SCENE_NODE_REMOVE, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00114 Connect(wxID_ANY, Event::EVT_SCENE_NODE_UID_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00115 Connect(wxID_ANY, Event::EVT_SCENE_NODE_NAME_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00116 Connect(wxID_ANY, Event::EVT_SCENE_NODE_TRANSFORM_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00117 Connect(wxID_ANY, Event::EVT_SCENE_NODE_TRANSFORM, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00118 Connect(wxID_ANY, Event::EVT_SCENE_NODE_TRANSLATE_ABS, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00119 Connect(wxID_ANY, Event::EVT_SCENE_NODE_GEOMETRY_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00120 Connect(wxID_ANY, Event::EVT_SCENE_NODE_MATERIAL_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00121 Connect(wxID_ANY, Event::EVT_SCENE_NODE_LIGHT_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00122 Connect(wxID_ANY, Event::EVT_SCENE_CAM_FIT, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00123 Connect(wxID_ANY, Event::EVT_SCENE_CAM_FIT_NEAR_FAR, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00124 Connect(wxID_ANY, Event::EVT_SCENE_CAM_RESETVIEW, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00125 Connect(wxID_ANY, Event::EVT_SCENE_CAM_PROJECTION_SET, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00126 Connect(wxID_ANY, Event::EVT_SCENE_GRAPHVIZ, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00127 Connect(wxID_ANY, Event::EVT_SCENE_DUMP, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00128 Connect(wxID_ANY, Event::EVT_SCENE_BVH_UPDATE, wxCommandEventHandler(EventHandler::onSceneEvent), (wxObject *)NULL, this);
00129
00131 Connect(wxID_ANY, Event::EVT_IO_SCENE_IMPORT, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00132 Connect(wxID_ANY, Event::EVT_IO_SCENE_EXPORT, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00133 Connect(wxID_ANY, Event::EVT_IO_SCENE_IMPORT_DONE, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00134 Connect(wxID_ANY, Event::EVT_RENDER_SCENE, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00135 Connect(wxID_ANY, Event::EVT_RENDER_UPDATE, wxCommandEventHandler(EventHandler::onCommandEvent), (wxObject *)NULL, this);
00136
00137 moduleListeners.resize(Event::EVT_HIGHEST - Event::EVT_LOWEST - 1);
00138 listeners.resize(Event::EVT_HIGHEST - Event::EVT_LOWEST - 1);
00139 }
00140
00141
00142 EventHandler::~EventHandler()
00143 {
00145 KERNEL->GetMainWindow()->Disconnect(wxID_ANY, wxEVT_IDLE);
00146 Disconnect();
00147 listeners.clear();
00148 moduleListeners.clear();
00149 timer.Stop();
00151 while (evtQueue.GetSize())
00152 {
00153 MessageQueueError err;
00154 wxCommandEvent qevt = evtQueue.ReceiveTimeout(0, err);
00155 if (err == wxMSGQUEUE_NO_ERROR)
00156 {
00157 if (qevt.GetEventType() == Event::EVT_LOG)
00158 ProcessEvent(qevt);
00159 ReleaseEvent(qevt);
00160 }
00161 }
00163 SetNextHandler((wxEvtHandler *)NULL);
00164 }
00165
00166
00167 void EventHandler::RegisterListener(const MODULE_ID & id, const Event::EVENT_TYPE eventType)
00168 {
00169 if (eventType <= Event::EVT_LOWEST && eventType >= Event::EVT_HIGHEST)
00170 LOGERROR(wxT("<EventHandler>Error registering listener, invalid event type"));
00171 else
00172 {
00173 bool found = false;
00174 ModuleIDList * midList = &moduleListeners[eventType - Event::EVT_LOWEST - 1];
00175 for (ModuleIDList::const_iterator it = midList->begin();
00176 it != midList->end() && !found; it++)
00177 found = ( *it == id );
00178 if (!found)
00179 moduleListeners[eventType - Event::EVT_LOWEST - 1].push_back(id);
00180 }
00181 }
00182
00183
00184 void EventHandler::RegisterListener(wxEvtHandler * handler, const Event::EVENT_TYPE eventType)
00185 {
00186 if (eventType <= Event::EVT_LOWEST && eventType >= Event::EVT_HIGHEST)
00187 LOGERROR(wxT("<EventHandler>Error registering listener, invalid event type"));
00188 else
00189 {
00190 bool found = false;
00191 HandlerList * midList = &listeners[eventType - Event::EVT_LOWEST - 1];
00192 for (HandlerList::const_iterator it = midList->begin();
00193 it != midList->end() && !found; it++)
00194 found = ( *it == handler );
00195 if (!found)
00196 listeners[eventType - Event::EVT_LOWEST - 1].push_back(handler);
00197 }
00198 }
00199
00200
00201 void EventHandler::UnregisterListener(const MODULE_ID & id, const Event::EVENT_TYPE eventType)
00202 {
00204 if (eventType > Event::EVT_LOWEST && eventType < Event::EVT_HIGHEST)
00205 {
00206 for (ModuleIDList::iterator mit = moduleListeners[eventType - Event::EVT_LOWEST - 1].begin();
00207 mit != moduleListeners[eventType - Event::EVT_LOWEST - 1].end(); )
00208 {
00209 if (*mit == id)
00210 mit = moduleListeners[eventType - Event::EVT_LOWEST - 1].erase(mit);
00211 else
00212 mit++;
00213 }
00214 }
00215 else
00216 {
00217 for (EventModuleList::iterator it = moduleListeners.begin();
00218 it != moduleListeners.end(); it++)
00219 {
00220 for (ModuleIDList::iterator mit = it->begin(); mit != it->end(); )
00221 {
00222 if (*mit == id)
00223 mit = it->erase(mit);
00224 else
00225 mit++;
00226 }
00227 }
00228 }
00229 }
00230
00231
00232 void EventHandler::UnregisterListener(wxEvtHandler * handler, const Event::EVENT_TYPE eventType)
00233 {
00235 if (eventType > Event::EVT_LOWEST && eventType < Event::EVT_HIGHEST)
00236 {
00237 for (HandlerList::iterator hit = listeners[eventType - Event::EVT_LOWEST - 1].begin();
00238 hit != listeners[eventType - Event::EVT_LOWEST - 1].end(); )
00239 {
00240 if (*hit == handler)
00241 hit = listeners[eventType - Event::EVT_LOWEST - 1].erase(hit);
00242 else
00243 hit++;
00244 }
00245 }
00246 else
00247 {
00248 for (EventHandlerList::iterator it = listeners.begin();
00249 it != listeners.end(); it++)
00250 {
00251 for (HandlerList::iterator hit = it->begin(); hit != it->end(); )
00252 {
00253 if (*hit == handler)
00254 hit = it->erase(hit);
00255 else
00256 hit++;
00257 }
00258 }
00259 }
00260 }
00261
00262
00263 void EventHandler::RegisterKernelModule(const MODULE_ID & id)
00264 {
00265 Module * module = KERNEL->GetModule(id);
00266 if (module && module->IsOfType(MODULE_TYPE_KERNEL))
00267 kernelModules.push_back(id);
00268 }
00269
00270
00271 void EventHandler::PostEvent(wxCommandEvent & evt, bool highPriority)
00272 {
00273 MessageQueueError err = evtQueue.Post(evt, highPriority);
00274 wxASSERT_MSG(err == wxMSGQUEUE_NO_ERROR, wxT("<EventHandler>Error posting event - exceeded message queue limit"));
00275 }
00276
00277
00278 void EventHandler::ProcessEventsFrom(MODULE_HANDLE_ID hid)
00279 {
00280 std::deque<wxCommandEvent> temp;
00281 MessageQueueError err;
00282 while (evtQueue.GetSize())
00283 temp.push_back(evtQueue.ReceiveTimeout(0, err));
00284 for (std::deque<wxCommandEvent>::iterator it = temp.begin();
00285 it != temp.end(); it++)
00286 {
00287 if (it->GetClientData()
00288 && it->GetExtraLong()
00289 && MODULE_ID(it->GetExtraLong()).GetHandleID() == hid)
00290 {
00291 if (!KERNEL->IsExiting() || it->GetEventType() == Event::EVT_LOG)
00292 ProcessEvent(*it);
00293 ReleaseEvent(*it);
00294 }
00295 else
00296 PostEvent(*it);
00297 }
00298 }
00299
00300
00301 bool EventHandler::processEvents()
00302 {
00303 timer.Start(evtCheckInterval);
00304 scenesToRender.clear();
00305 MessageQueueError err;
00306 unsigned msgProcessed = 0;
00307 while (evtQueue.GetSize() && msgProcessed++ < evtsPerIteration)
00308 {
00310 wxCommandEvent qevt = evtQueue.ReceiveTimeout(0, err);
00311 if (err == wxMSGQUEUE_NO_ERROR)
00312 {
00313 if (!KERNEL->IsExiting() || qevt.GetEventType() == Event::EVT_LOG)
00314 {
00315 distributeToListeners(qevt);
00316 ProcessEvent(qevt);
00317 }
00318 ReleaseEvent(qevt);
00321 }
00322 }
00324 for (KernelModuleIDList::iterator modIt = kernelModules.begin();
00325 modIt != kernelModules.end(); modIt++)
00326 {
00327 Module * module = KERNEL->GetModule(*modIt);
00328 if (module && module->IsOfType(MODULE_TYPE_KERNEL))
00329 ((KernelModule *)module)->kernelRun();
00330 }
00332 for (SceneList::const_iterator it = scenesToRender.begin();
00333 it != scenesToRender.end(); it++)
00334 KERNEL->renderManager->RenderUpdate(it->first);
00335
00336 return (msgProcessed <= evtsPerIteration);
00337 }
00338
00339
00340 void EventHandler::onIdle(wxIdleEvent & evt)
00341 {
00342 if (!processEvents())
00343 evt.RequestMore();
00344 }
00345
00346
00347 void EventHandler::onTimer(wxTimerEvent & WXUNUSED(evt))
00348 {
00349 processEvents();
00350 }
00351
00352
00353 void EventHandler::onLogEvent(wxCommandEvent & evt)
00354 {
00355 FlexiLog::LogMessageFlexi(CloneWxString(evt.GetString()), evt.GetInt());
00356 evt.Skip();
00357 }
00358
00359
00360 void EventHandler::onCommandEvent(wxCommandEvent & evt)
00361 {
00362 if (KERNEL->IsExiting())
00363 return;
00364
00365 switch (evt.GetEventType())
00366 {
00367 case Event::EVT_EXIT:
00368 {
00369 wxCloseEvent closeEvt(wxEVT_CLOSE_WINDOW, KERNEL->GetMainWindow()->GetId());
00370 closeEvt.SetEventObject(KERNEL->GetMainWindow());
00371 KERNEL->GetMainWindow()->AddPendingEvent(closeEvt);
00372 }
00373 break;
00374 case Event::EVT_LOG_LEVEL_SET:
00375 FlexiLog::SetLogLevel(wxLogLevel(evt.GetInt()));
00376 break;
00377 case Event::EVT_MODULE_PREFER:
00378 KERNEL->PreferModule(evt.GetString());
00379 break;
00380 case Event::EVT_MODULE_FORBID:
00381 KERNEL->ForbidModule(evt.GetString());
00382 break;
00383 case Event::EVT_MODULE_RELEASE:
00384 {
00385 MODULE_ID mid(evt.GetExtraLong());
00386 KERNEL->ReleaseModule(mid);
00387 UnregisterListener(mid);
00388 }
00389 break;
00390 case Event::EVT_MODULE_RUN:
00391 KERNEL->generalManager->RunModule(evt.GetString());
00392 break;
00393 case Event::EVT_PARAM_SET_DO:
00394 {
00395 KERNEL->environment.SetParam(Parameter::ParameterIdentificator::FromEvent(evt),
00396 ((wxString *)evt.GetClientData())->AfterFirst('='), (evt.GetInt() != 0));
00397 }
00398 break;
00399 case Event::EVT_PARAM_REGISTER:
00400 {
00401 Parameter::ParameterIdentificator pi = Parameter::ParameterIdentificator::FromParamName(((wxString *)evt.GetClientData())->BeforeFirst('='));
00402 KERNEL->environment.RegisterParam(pi, ((wxString *)evt.GetClientData())->AfterFirst('='));
00403 }
00404 break;
00405 case Event::EVT_PARAM_UNREGISTER:
00406 {
00407 Parameter::ParameterIdentificator pi = Parameter::ParameterIdentificator::FromParamName(*((wxString *)evt.GetClientData()));
00408 KERNEL->environment.UnregisterParam(pi);
00409 }
00410 break;
00411 case Event::EVT_PARAM_SET:
00412 UPDATE_PARAM_FROM_EVENT_BOOL(cloneSceneDataParamID, cloneSceneData, evt);
00413 UPDATE_PARAM_FROM_EVENT_UNSIGNED(evtsPerIterationParamID, evtsPerIteration, evt);
00414 UPDATE_PARAM_FROM_EVENT_UNSIGNED(evtCheckIntervalParamID, evtCheckInterval, evt);
00415 if (IS_PARAM(evt, logSyncEveryMsgParamID))
00416 {
00417 bool syncAll = false;
00418 UPDATE_PARAM_FROM_EVENT_BOOL(logSyncEveryMsgParamID, syncAll, evt);
00419 FlexiLog::GetInstance()->SyncEveryMsg(syncAll);
00420 }
00421 break;
00422 case Event::EVT_IO_SCENE_IMPORT:
00423 {
00424 SCENE_ID sceneID = KERNEL->ioManager->ImportScene(evt.GetString());
00425 wxCommandEvent evCache = Event::GET_EVT_RENDER_CACHE_START(sceneID);
00426 PostEvent(evCache);
00427 }
00428 break;
00429 case Event::EVT_IO_SCENE_EXPORT:
00430 {
00431 Scene * scene = KERNEL->sceneManager->GetScene(SCENE_ID(evt.GetId()));
00432 if (!scene)
00433 LOGERROR(wxT("<EventHandler>Invalid scene ID in event"));
00434 else
00435 KERNEL->ioManager->ExportScene(scene, evt.GetString());
00436 }
00437 break;
00438 case Event::EVT_IO_SCENE_IMPORT_DONE:
00439 {
00440 SCENE_ID sceneID = SCENE_ID(evt.GetId());
00441 Scene * scene = KERNEL->sceneManager->GetScene(sceneID);
00442 if (!scene)
00443 LOGERROR(wxT("<EventHandler>Invalid scene ID in event"));
00444 else if (scene->GetBVH())
00445 scene->UpdateBVH()->Rebuild();
00446 LOG(wxString::Format(wxT("<EventHandler>Scene import done, ID %i"), sceneID));
00447 wxCommandEvent evCache = Event::GET_EVT_RENDER_CACHE_STOP(sceneID);
00448 PostEvent(evCache);
00449 }
00450 break;
00451 case Event::EVT_RENDER_SCENE:
00452 {
00453 SCENE_ID sceneID = SCENE_ID(evt.GetId());
00454 Scene * scene = KERNEL->sceneManager->GetScene(sceneID);
00455 if (!scene)
00456 LOGERROR(wxT("<EventHandler>Invalid scene ID in event"));
00457 else
00458 {
00459 wxString camUid = wxT("default_camera");
00460 int tmp=0;
00461 while (scene->GetNodeID(camUid) != NODE_ID_NONE)
00462 {
00463 camUid=wxString::Format(wxT("default_camera_%i"), tmp);
00464 tmp++;
00465 }
00466 NODE_ID camID = scene->Insert(new Camera(camUid, camUid), scene->GetRootID());
00467 scene->ResetView(camID);
00468 RENDER_WINDOW_ID winID = KERNEL->renderManager->RenderScene(scene, camID);
00469 if (winID != RENDER_WINDOW_ID_NONE)
00470 KERNEL->manipManager->ManipulateCamera(sceneID, camID, winID);
00471 wxCommandEvent ev1 = Event::GET_EVT_SCENE_CAM_FIT(sceneID, camID);
00472 PostEvent(ev1);
00473 wxCommandEvent ev2 = Event::GET_EVT_RENDER_UPDATE(sceneID);
00474 PostEvent(ev2);
00475 }
00476 }
00477 break;
00478 case Event::EVT_RENDER_UPDATE:
00479 {
00480 SCENE_ID sceneID = evt.GetId();
00481 if (scenesToRender.find(sceneID) == scenesToRender.end())
00482 scenesToRender.insert(SceneList::value_type(sceneID, true));
00483 }
00484 break;
00485 default:
00486 break;
00487 }
00488 evt.Skip();
00489 }
00490
00491
00492 void EventHandler::onKeyEvent(wxKeyEvent & evt)
00493 {
00494 #ifdef EVENT_DEBUG
00495 LOG(wxT("<EventHandler>key event"));
00496 #endif
00497
00498 for (ModuleIDList::const_iterator it = moduleListeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].begin();
00499 it != moduleListeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].end(); it++)
00500 {
00501 Module * module = KERNEL->GetModule(*it);
00502 if (module && module->IsOfType(MODULE_TYPE_MANIPULATOR))
00503 ((ManipulatorModule *)module)->PostKeyEvent(evt);
00504 }
00505 for (HandlerList::const_iterator it = listeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].begin();
00506 it != listeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].end(); it++)
00507 {
00508 (*it)->AddPendingEvent(evt);
00509 }
00510 evt.Skip();
00511 }
00512
00513
00514 void EventHandler::onMouseEvent(wxMouseEvent & evt)
00515 {
00516 #ifdef EVENT_DEBUG
00517 LOG(wxT("<EventHandler>mouse event"));
00518 #endif
00519
00520 for (ModuleIDList::const_iterator it = moduleListeners[Event::EVT_INPUT_MOUSE - Event::EVT_LOWEST - 1].begin();
00521 it != moduleListeners[Event::EVT_INPUT_MOUSE - Event::EVT_LOWEST - 1].end(); it++)
00522 {
00523 Module * module = KERNEL->GetModule(*it);
00524 if (module && module->IsOfType(MODULE_TYPE_MANIPULATOR))
00525 ((ManipulatorModule *)module)->PostMouseEvent(evt);
00526 }
00527 for (HandlerList::const_iterator it = listeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].begin();
00528 it != listeners[Event::EVT_INPUT_KEY - Event::EVT_LOWEST - 1].end(); it++)
00529 {
00530 (*it)->AddPendingEvent(evt);
00531 }
00532 evt.Skip();
00533 }
00534
00535
00536 void EventHandler::onSceneEvent(wxCommandEvent & evt)
00537 {
00538 if (KERNEL->IsExiting())
00539 return;
00540
00542 if (evt.GetEventType() == Event::EVT_SCENE_IMAGE_ADD)
00543 {
00544 KERNEL->sceneManager->GetImage(evt.GetString());
00545 evt.Skip();
00546 return;
00547 }
00548
00549 Scene * scene = KERNEL->sceneManager->GetScene(SCENE_ID(evt.GetId()));
00550 if (!scene)
00551 {
00552 wxString sender = MODULE_ID(evt.GetExtraLong()).ToString();
00553 if (evt.GetExtraLong() == 0)
00554 sender = wxT("kernel");
00555 else
00556 sender.Prepend(wxT("module "));
00557 LOGERROR(wxT("<EventHandler>Invalid scene ID in scene event sent by ") + sender);
00558 return;
00559 }
00560
00561 switch (evt.GetEventType())
00562 {
00563 case Event::EVT_SCENE_RAY_PICK:
00564 {
00565 Ray * ray = (Ray *)evt.GetClientData();
00566 BVH::IntersectionInfo info;
00567 if (scene->GetBVH() && scene->GetBVH()->BoundProbe(*ray, &info))
00568 {
00569 const SceneNode * node = scene->GetNode(info.nodeID);
00570 if (node)
00571 LOG(wxString::Format(wxT("Picked node '%s' ID%i from scene %i"), node->GetName().c_str(), node->GetID(), evt.GetId()));
00572 }
00573 }
00574 break;
00575 case Event::EVT_SCENE_MATERIAL_ADD:
00576 if (!evt.GetClientData())
00577 LOGERROR(wxT("<EventHandler>No material defined in scene event"));
00578 else
00579 {
00580 Material * material = (Material *)evt.GetClientData();
00581 if (cloneSceneData)
00582 scene->AddMaterial(material->Clone());
00583 else
00584 scene->AddMaterial(material);
00585 }
00586 break;
00587 case Event::EVT_SCENE_GEOMETRY_ADD:
00588 if (!evt.GetClientData())
00589 LOGERROR(wxT("<EventHandler>No geometry defined in scene event"));
00590 else
00591 {
00592 Geometry * geometry = (Geometry *)evt.GetClientData();
00593 if (cloneSceneData)
00594 scene->AddGeometry(geometry->Clone());
00595 else
00596 scene->AddGeometry(geometry);
00597 }
00598 break;
00599 case Event::EVT_SCENE_NODE_COPY:
00600 {
00601 std::vector<NODE_ID> ids;
00602 if (Event::ExtractNodeIDs(evt, ids, scene) == 2)
00603 scene->Copy(scene, ids[0], ids[1]);
00604 else
00605 LOGERROR(wxString::Format(wxT("<Scene>Invalid node identificator count or format in '%s'"), evt.GetString().c_str()));
00606 }
00607 break;
00608 case Event::EVT_SCENE_NODE_MOVE:
00609 {
00610 std::vector<NODE_ID> ids;
00611 if (Event::ExtractNodeIDs(evt, ids, scene) == 2)
00612 scene->Move(ids[0], ids[1]);
00613 else
00614 LOGERROR(wxString::Format(wxT("<Scene>Invalid node identificator count or format in '%s'"), evt.GetString().c_str()));
00615 }
00616 break;
00617 case Event::EVT_SCENE_NODE_INSERT:
00618 if (!evt.GetClientData())
00619 LOGERROR(wxT("<EventHandler>No node defined in scene event"));
00620 else
00621 {
00622 NODE_ID id = Event::ExtractNodeID(evt, scene);
00623 SceneNode * node = (SceneNode *)evt.GetClientData();
00624 if (cloneSceneData)
00625 scene->Insert(node->Clone(), id);
00626 else
00627 scene->Insert(node, id);
00628 }
00629 break;
00630 case Event::EVT_SCENE_NODE_REMOVE:
00631 scene->Remove(Event::ExtractNodeID(evt, scene));
00632 break;
00633 case Event::EVT_SCENE_NODE_UID_SET:
00634 if (!evt.GetClientData())
00635 LOGERROR(wxT("<EventHandler>No node uid defined in scene event"));
00636 else
00637 scene->SetUid(Event::ExtractNodeID(evt, scene), *((wxString *)evt.GetClientData()));
00638 break;
00639 case Event::EVT_SCENE_NODE_NAME_SET:
00640 if (!evt.GetClientData())
00641 LOGERROR(wxT("<EventHandler>No node name defined in scene event"));
00642 else
00643 scene->SetName(Event::ExtractNodeID(evt, scene), *((wxString *)evt.GetClientData()));
00644 break;
00645 case Event::EVT_SCENE_NODE_TRANSFORM_SET:
00646 if (!evt.GetClientData())
00647 LOGERROR(wxT("<EventHandler>No matrix defined in scene event"));
00648 else
00649 scene->SetTransformation(Event::ExtractNodeID(evt, scene), *((MATRIX *)evt.GetClientData()));
00650 break;
00651 case Event::EVT_SCENE_NODE_TRANSFORM:
00652 if (!evt.GetClientData())
00653 LOGERROR(wxT("<EventHandler>No matrix defined in scene event"));
00654 else
00655 scene->Transform(Event::ExtractNodeID(evt, scene), *((MATRIX *)evt.GetClientData()));
00656 break;
00657 case Event::EVT_SCENE_NODE_TRANSLATE_ABS:
00658 if (!evt.GetClientData())
00659 LOGERROR(wxT("<EventHandler>No vector defined in scene event"));
00660 else
00661 scene->TranslateAbs(Event::ExtractNodeID(evt, scene), *((VECTOR3 *)evt.GetClientData()));
00662 break;
00663 case Event::EVT_SCENE_NODE_GEOMETRY_SET:
00664 if (!evt.GetClientData())
00665 LOGERROR(wxT("<EventHandler>No geometry name defined in scene event"));
00666 else
00667 scene->SetGeometry(Event::ExtractNodeID(evt, scene), scene->GetGeometryID(*((wxString *)evt.GetClientData())));
00668 break;
00669 case Event::EVT_SCENE_NODE_MATERIAL_SET:
00670 if (!evt.GetClientData())
00671 LOGERROR(wxT("<EventHandler>No material name defined in scene event"));
00672 else
00673 scene->SetMaterial(Event::ExtractNodeID(evt, scene), scene->GetMaterialID(*((wxString *)evt.GetClientData())));
00674 break;
00675 case Event::EVT_SCENE_NODE_LIGHT_SET:
00676 {
00677 NODE_ID id = Event::ExtractNodeID(evt, scene);
00678 Light * light = (Light *)evt.GetClientData();
00679 if (cloneSceneData)
00680 scene->SetLight(id, light->Clone());
00681 else
00682 scene->SetLight(id, light);
00683 }
00684 break;
00685 case Event::EVT_SCENE_CAM_FIT:
00686 case Event::EVT_SCENE_CAM_FIT_NEAR_FAR:
00687 {
00688 NODE_ID camID = NODE_ID(evt.GetInt());
00689 scene->WriteLock(camID);
00690 scene->Fit(camID, true, (evt.GetEventType() == Event::EVT_SCENE_CAM_FIT_NEAR_FAR));
00691 scene->WriteUnlock(camID);
00692 }
00693 break;
00694 case Event::EVT_SCENE_CAM_RESETVIEW:
00695 {
00696 NODE_ID camID = NODE_ID(evt.GetInt());
00697 scene->WriteLock(camID);
00698 scene->ResetView(camID);
00699 scene->WriteUnlock(camID);
00700 }
00701 break;
00702 case Event::EVT_SCENE_CAM_PROJECTION_SET:
00703 {
00704 NODE_ID camID = Event::ExtractNodeID(evt, scene);
00705 wxStringTokenizer params(*((wxString *)evt.GetClientData()), wxT("; |:"));
00706 if (params.CountTokens() != 5)
00707 LOGERROR(wxT("<EventHandler>Cannot set camera parameters, parameters not defined properly"));
00708 else
00709 {
00710 long projWidth = 0;
00711 long projHeight = 0;
00712 double projNear = 0.0;
00713 double projFar = 0.0;
00714 double projFov = 0.0;
00715 params.GetNextToken().ToLong(&projWidth);
00716 params.GetNextToken().ToLong(&projHeight);
00717 params.GetNextToken().ToDouble(&projNear);
00718 params.GetNextToken().ToDouble(&projFar);
00719 params.GetNextToken().ToDouble(&projFov);
00720 scene->WriteLock(camID);
00721 scene->SetProjectionParams(camID, unsigned(projWidth), unsigned(projHeight), float(projNear), float(projFar), float(projFov));
00722 scene->WriteUnlock(camID);
00723 }
00724 }
00725 break;
00726 case Event::EVT_SCENE_GRAPHVIZ:
00727 #ifdef GRAPHVIZ_SUPPORT
00728 scene->CreateGraphVizFile();
00729 if (scene->GetBVH())
00730 scene->UpdateBVH()->CreateGraphVizFile();
00731 #else
00732 LOGERROR(wxT("GraphViz not supported, compile with GRAPHVIZ_SUPPORT"));
00733 #endif
00734 break;
00735 case Event::EVT_SCENE_DUMP:
00736 #ifdef __WXDEBUG__
00737 scene->Dump();
00738 #else
00739 LOGERROR(wxT("Dump available only in debug mode"));
00740 #endif
00741 break;
00742 case Event::EVT_SCENE_BVH_UPDATE:
00743 {
00744 scene->UpdateBVH();
00745 wxCommandEvent evBvh = Event::GET_EVT_SCENE_BVH_UPDATED(scene->GetID());
00746 PostEvent(evBvh);
00747 break;
00748 }
00749 default:
00750 break;
00751 }
00752 evt.Skip();
00753 }
00754
00755
00756 void EventHandler::ReleaseEvent(wxCommandEvent & evt, bool forceSceneData)
00757 {
00758 if (evt.GetClientData())
00759 {
00760 switch (evt.GetEventType())
00761 {
00762 case Event::EVT_SCENE_RAY_PICK:
00763 delete (Ray *)evt.GetClientData();
00764 break;
00765 case Event::EVT_SCENE_COLLISION:
00766 delete (CollisionReport *)evt.GetClientData();
00767 break;
00768 case Event::EVT_LOG_SET:
00770 break;
00771 case Event::EVT_INPUT_TRACKING:
00772 delete (DeviceData *)evt.GetClientData();
00773 break;
00774 case Event::EVT_IO_SCENE_IMPORT_DO:
00777 break;
00778 case Event::EVT_SCENE_MATERIAL_ADD:
00779 if (forceSceneData || cloneSceneData || !KERNEL || KERNEL->IsExiting())
00780 {
00781 delete (Material *)evt.GetClientData();
00782 evt.SetClientData(NULL);
00783 }
00784 break;
00785 case Event::EVT_SCENE_GEOMETRY_ADD:
00786 if (forceSceneData || cloneSceneData || !KERNEL || KERNEL->IsExiting())
00787 {
00788 delete (Geometry *)evt.GetClientData();
00789 evt.SetClientData(NULL);
00790 }
00791 break;
00792 case Event::EVT_SCENE_NODE_INSERT:
00793 if (forceSceneData || cloneSceneData || !KERNEL || KERNEL->IsExiting())
00794 {
00795 delete (SceneNode *)evt.GetClientData();
00796 evt.SetClientData(NULL);
00797 }
00798 break;
00799 case Event::EVT_SCENE_NODE_GEOMETRY_SET:
00800 case Event::EVT_SCENE_NODE_MATERIAL_SET:
00801 case Event::EVT_SCENE_NODE_UID_SET:
00802 case Event::EVT_SCENE_NODE_NAME_SET:
00803 case Event::EVT_SCENE_CAM_PROJECTION_SET:
00804 case Event::EVT_PARAM_SET_DO:
00805 case Event::EVT_PARAM_SET:
00806 case Event::EVT_PARAM_REGISTER:
00807 case Event::EVT_PARAM_UNREGISTER:
00808 case Event::EVT_PARAM_GUIUNREGISTER:
00809 delete (wxString *)evt.GetClientData();
00810 evt.SetClientData(NULL);
00811 break;
00812 case Event::EVT_PARAM_GUIREGISTER:
00813 delete (GUIEnvironment::GUIParamInfo *)evt.GetClientData();
00814 evt.SetClientData(NULL);
00815 break;
00816 case Event::EVT_SCENE_NODE_LIGHT_SET:
00817 if (forceSceneData || cloneSceneData || !KERNEL || KERNEL->IsExiting())
00818 {
00819 delete (Light *)evt.GetClientData();
00820 evt.SetClientData(NULL);
00821 }
00822 break;
00823 case Event::EVT_SCENE_NODE_TRANSFORM_SET:
00824 case Event::EVT_SCENE_NODE_TRANSFORM:
00825 delete (MATRIX *)evt.GetClientData();
00826 evt.SetClientData(NULL);
00827 break;
00828 case Event::EVT_SCENE_NODE_TRANSLATE_ABS:
00829 delete (VECTOR3 *)evt.GetClientData();
00830 evt.SetClientData(NULL);
00831 break;
00832 default:
00833 LOGWARNING(wxT("<EventHandler>Event with unknown client data, data could not be released"));
00834 break;
00835 }
00836 }
00837 }
00838
00839
00840 wxCommandEvent EventHandler::CloneEvent(const wxCommandEvent & evt)
00841 {
00842 if (evt.GetClientData())
00843 {
00844 wxCommandEvent ret(evt);
00845 switch (evt.GetEventType())
00846 {
00847 case Event::EVT_SCENE_RAY_PICK:
00848 ret.SetClientData(((Ray *)evt.GetClientData())->Clone());
00849 break;
00850 case Event::EVT_SCENE_COLLISION:
00851 ret.SetClientData(new CollisionReport(*(CollisionReport *)evt.GetClientData()));
00852 break;
00853 case Event::EVT_INPUT_TRACKING:
00854 ret.SetClientData(new DeviceData(*(DeviceData *)evt.GetClientData()));
00855 break;
00856 case Event::EVT_SCENE_MATERIAL_ADD:
00857 ret.SetClientData(((Material *)evt.GetClientData())->Clone());
00858 break;
00859 case Event::EVT_SCENE_GEOMETRY_ADD:
00860 ret.SetClientData(((Geometry *)evt.GetClientData())->Clone());
00861 break;
00862 case Event::EVT_SCENE_NODE_INSERT:
00863 ret.SetClientData(((SceneNode *)evt.GetClientData())->Clone());
00864 break;
00865 case Event::EVT_SCENE_NODE_GEOMETRY_SET:
00866 case Event::EVT_SCENE_NODE_MATERIAL_SET:
00867 case Event::EVT_SCENE_NODE_UID_SET:
00868 case Event::EVT_SCENE_NODE_NAME_SET:
00869 case Event::EVT_SCENE_CAM_PROJECTION_SET:
00870 case Event::EVT_PARAM_SET_DO:
00871 case Event::EVT_PARAM_SET:
00872 case Event::EVT_PARAM_REGISTER:
00873 case Event::EVT_PARAM_UNREGISTER:
00874 case Event::EVT_PARAM_GUIUNREGISTER:
00875 ret.SetClientData(new wxString(CloneWxString(*((wxString *)evt.GetClientData()))));
00876 break;
00877 case Event::EVT_PARAM_GUIREGISTER:
00878 ret.SetClientData(new GUIEnvironment::GUIParamInfo(*((GUIEnvironment::GUIParamInfo *)evt.GetClientData())));
00879 break;
00880 case Event::EVT_SCENE_NODE_LIGHT_SET:
00881 ret.SetClientData(((Light *)evt.GetClientData())->Clone());
00882 break;
00883 case Event::EVT_SCENE_NODE_TRANSFORM_SET:
00884 case Event::EVT_SCENE_NODE_TRANSFORM:
00885 ret.SetClientData(((MATRIX *)evt.GetClientData())->Clone());
00886 break;
00887 case Event::EVT_SCENE_NODE_TRANSLATE_ABS:
00888 ret.SetClientData(((VECTOR3 *)evt.GetClientData())->Clone());
00889 break;
00890 default:
00891 LOGWARNING(wxT("<EventHandler>Event with unknown client data, data could not be cloned"));
00892 break;
00893 }
00894 return ret;
00895 }
00896 else
00897 return evt;
00898 }
00899
00900
00901 void EventHandler::distributeToListeners(wxCommandEvent & evt)
00902 {
00903 bool highPrior = ( evt.GetEventType() == Event::EVT_EXIT );
00904 if (evt.GetEventType() > Event::EVT_LOWEST && evt.GetEventType() < Event::EVT_HIGHEST)
00905 {
00907 ModuleIDList * modlisteners_ = &moduleListeners[Event::EVT_ALL - Event::EVT_LOWEST - 1];
00908 for (ModuleIDList::const_iterator mit = modlisteners_->begin();
00909 mit != modlisteners_->end(); mit++)
00910 {
00911 Module * module = KERNEL->GetModule(*mit);
00912 if (module)
00913 {
00914 wxCommandEvent clonedEvt = CloneEvent(evt);
00915 module->PostEvent(clonedEvt, highPrior);
00916 }
00917 }
00918 HandlerList * listeners_ = &listeners[Event::EVT_ALL - Event::EVT_LOWEST - 1];
00919 for (HandlerList::const_iterator hit = listeners_->begin();
00920 hit != listeners_->end(); hit++)
00921 {
00923 (*hit)->ProcessEvent(evt);
00924 }
00925
00927 size_t index = evt.GetEventType() - Event::EVT_LOWEST - 1;
00928 for (ModuleIDList::const_iterator mit = moduleListeners[index].begin();
00929 mit != moduleListeners[index].end(); mit++)
00930 {
00931 Module * module = KERNEL->GetModule(*mit);
00932 if (module)
00933 {
00934 wxCommandEvent clonedEvt = CloneEvent(evt);
00935 module->PostEvent(clonedEvt, highPrior);
00936 }
00937 }
00938 for (HandlerList::const_iterator hit = listeners[index].begin();
00939 hit != listeners[index].end(); hit++)
00940 {
00942 (*hit)->ProcessEvent(evt);
00943 }
00944 }
00945 }