00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "EventManager.h"
00019
00020
00021
00022
00023 EventManager::EventManager()
00024 {
00025 init();
00026 }
00027
00028
00029 EventManager::~EventManager()
00030 {
00031 clear();
00032 }
00033
00034
00035 void EventManager::init()
00036 {
00037 generateKeyMap();
00038
00039
00040 createEventGroup("EventManager");
00041 std::map<std::string, EventSlot> &eventGroup = mEvents["EventManager"];
00042
00043
00044 createEventSlot("EventManager", "onMouseMoved");
00045 createEventSlot("EventManager", "onMouseDown#Left");
00046 createEventSlot("EventManager", "onMouseDown#Right");
00047 createEventSlot("EventManager", "onMouseDown#Middle");
00048 createEventSlot("EventManager", "onMouseUp#Left");
00049 createEventSlot("EventManager", "onMouseUp#Right");
00050 createEventSlot("EventManager", "onMouseUp#Middle");
00051 createEventSlot("EventManager", "onMouseWheel");
00052
00053
00054 std::string strOnKeyDown = "onKeyDown#";
00055 std::string strOnKeyUp = "onKeyUp#";
00056
00057 std::map<EKEY_CODE, std::string>::iterator it;
00058 for(it = mKeyMap.begin(); it != mKeyMap.end(); it++)
00059 {
00060 EventSlot slot1, slot2;
00061 eventGroup[strOnKeyDown + it->second] = slot1;
00062 eventGroup[strOnKeyUp + it->second] = slot2;
00063 }
00064
00065
00066 createEventSlot("EventManager", "onGUIEvent");
00067 }
00068
00069
00070 void EventManager::clear()
00071 {
00072 mEvents.clear();
00073 }
00074
00075
00076 void EventManager::generateKeyMap()
00077 {
00078 #define addToKeyMap(x) mKeyMap[x] = #x
00079 addToKeyMap(KEY_LBUTTON);
00080 addToKeyMap(KEY_RBUTTON);
00081 addToKeyMap(KEY_CANCEL);
00082 addToKeyMap(KEY_MBUTTON);
00083 addToKeyMap(KEY_XBUTTON1);
00084 addToKeyMap(KEY_XBUTTON2);
00085 addToKeyMap(KEY_BACK);
00086 addToKeyMap(KEY_TAB);
00087 addToKeyMap(KEY_CLEAR);
00088 addToKeyMap(KEY_RETURN);
00089 addToKeyMap(KEY_SHIFT);
00090 addToKeyMap(KEY_CONTROL);
00091 addToKeyMap(KEY_MENU);
00092 addToKeyMap(KEY_PAUSE);
00093 addToKeyMap(KEY_CAPITAL);
00094 addToKeyMap(KEY_KANA);
00095 addToKeyMap(KEY_HANGUEL);
00096 addToKeyMap(KEY_HANGUL);
00097 addToKeyMap(KEY_JUNJA);
00098 addToKeyMap(KEY_FINAL);
00099 addToKeyMap(KEY_HANJA);
00100 addToKeyMap(KEY_KANJI);
00101 addToKeyMap(KEY_ESCAPE);
00102 addToKeyMap(KEY_CONVERT);
00103 addToKeyMap(KEY_NONCONVERT);
00104 addToKeyMap(KEY_ACCEPT);
00105 addToKeyMap(KEY_MODECHANGE);
00106 addToKeyMap(KEY_SPACE);
00107 addToKeyMap(KEY_PRIOR);
00108 addToKeyMap(KEY_NEXT);
00109 addToKeyMap(KEY_END);
00110 addToKeyMap(KEY_HOME);
00111 addToKeyMap(KEY_LEFT);
00112 addToKeyMap(KEY_UP);
00113 addToKeyMap(KEY_RIGHT);
00114 addToKeyMap(KEY_DOWN);
00115 addToKeyMap(KEY_SELECT);
00116 addToKeyMap(KEY_PRINT);
00117 addToKeyMap(KEY_EXECUT);
00118 addToKeyMap(KEY_SNAPSHOT);
00119 addToKeyMap(KEY_INSERT);
00120 addToKeyMap(KEY_DELETE);
00121 addToKeyMap(KEY_HELP);
00122 addToKeyMap(KEY_KEY_0);
00123 addToKeyMap(KEY_KEY_1);
00124 addToKeyMap(KEY_KEY_2);
00125 addToKeyMap(KEY_KEY_3);
00126 addToKeyMap(KEY_KEY_4);
00127 addToKeyMap(KEY_KEY_5);
00128 addToKeyMap(KEY_KEY_6);
00129 addToKeyMap(KEY_KEY_7);
00130 addToKeyMap(KEY_KEY_8);
00131 addToKeyMap(KEY_KEY_9);
00132 addToKeyMap(KEY_KEY_A);
00133 addToKeyMap(KEY_KEY_B);
00134 addToKeyMap(KEY_KEY_C);
00135 addToKeyMap(KEY_KEY_D);
00136 addToKeyMap(KEY_KEY_E);
00137 addToKeyMap(KEY_KEY_F);
00138 addToKeyMap(KEY_KEY_G);
00139 addToKeyMap(KEY_KEY_H);
00140 addToKeyMap(KEY_KEY_I);
00141 addToKeyMap(KEY_KEY_J);
00142 addToKeyMap(KEY_KEY_K);
00143 addToKeyMap(KEY_KEY_L);
00144 addToKeyMap(KEY_KEY_M);
00145 addToKeyMap(KEY_KEY_N);
00146 addToKeyMap(KEY_KEY_O);
00147 addToKeyMap(KEY_KEY_P);
00148 addToKeyMap(KEY_KEY_Q);
00149 addToKeyMap(KEY_KEY_R);
00150 addToKeyMap(KEY_KEY_S);
00151 addToKeyMap(KEY_KEY_T);
00152 addToKeyMap(KEY_KEY_U);
00153 addToKeyMap(KEY_KEY_V);
00154 addToKeyMap(KEY_KEY_W);
00155 addToKeyMap(KEY_KEY_X);
00156 addToKeyMap(KEY_KEY_Y);
00157 addToKeyMap(KEY_KEY_Z);
00158 addToKeyMap(KEY_LWIN);
00159 addToKeyMap(KEY_RWIN);
00160 addToKeyMap(KEY_APPS);
00161 addToKeyMap(KEY_SLEEP);
00162 addToKeyMap(KEY_NUMPAD0);
00163 addToKeyMap(KEY_NUMPAD1);
00164 addToKeyMap(KEY_NUMPAD2);
00165 addToKeyMap(KEY_NUMPAD3);
00166 addToKeyMap(KEY_NUMPAD4);
00167 addToKeyMap(KEY_NUMPAD5);
00168 addToKeyMap(KEY_NUMPAD6);
00169 addToKeyMap(KEY_NUMPAD7);
00170 addToKeyMap(KEY_NUMPAD8);
00171 addToKeyMap(KEY_NUMPAD9);
00172 addToKeyMap(KEY_MULTIPLY);
00173 addToKeyMap(KEY_ADD);
00174 addToKeyMap(KEY_SEPARATOR);
00175 addToKeyMap(KEY_SUBTRACT);
00176 addToKeyMap(KEY_DECIMAL);
00177 addToKeyMap(KEY_DIVIDE);
00178 addToKeyMap(KEY_F1);
00179 addToKeyMap(KEY_F2);
00180 addToKeyMap(KEY_F3);
00181 addToKeyMap(KEY_F4);
00182 addToKeyMap(KEY_F5);
00183 addToKeyMap(KEY_F6);
00184 addToKeyMap(KEY_F7);
00185 addToKeyMap(KEY_F8);
00186 addToKeyMap(KEY_F9);
00187 addToKeyMap(KEY_F10);
00188 addToKeyMap(KEY_F11);
00189 addToKeyMap(KEY_F12);
00190 addToKeyMap(KEY_F13);
00191 addToKeyMap(KEY_F14);
00192 addToKeyMap(KEY_F15);
00193 addToKeyMap(KEY_F16);
00194 addToKeyMap(KEY_F17);
00195 addToKeyMap(KEY_F18);
00196 addToKeyMap(KEY_F19);
00197 addToKeyMap(KEY_F20);
00198 addToKeyMap(KEY_F21);
00199 addToKeyMap(KEY_F22);
00200 addToKeyMap(KEY_F23);
00201 addToKeyMap(KEY_F24);
00202 addToKeyMap(KEY_NUMLOCK);
00203 addToKeyMap(KEY_SCROLL);
00204 addToKeyMap(KEY_LSHIFT);
00205 addToKeyMap(KEY_RSHIFT);
00206 addToKeyMap(KEY_LCONTROL);
00207 addToKeyMap(KEY_RCONTROL);
00208 addToKeyMap(KEY_LMENU);
00209 addToKeyMap(KEY_RMENU);
00210 addToKeyMap(KEY_PLUS);
00211 addToKeyMap(KEY_COMMA);
00212 addToKeyMap(KEY_MINUS);
00213 addToKeyMap(KEY_PERIOD);
00214 addToKeyMap(KEY_ATTN);
00215 addToKeyMap(KEY_CRSEL);
00216 addToKeyMap(KEY_EXSEL);
00217 addToKeyMap(KEY_EREOF);
00218 addToKeyMap(KEY_PLAY);
00219 addToKeyMap(KEY_ZOOM);
00220 addToKeyMap(KEY_PA1);
00221 addToKeyMap(KEY_OEM_CLEAR);
00222 #undef addToKeyMap
00223 }
00224
00225
00226 void EventManager::grab()
00227 {
00228
00229 }
00230
00231
00232 void EventManager::drop()
00233 {
00234
00235 }
00236
00237
00238 bool EventManager::createEventGroup(const std::string &groupName)
00239 {
00240
00241 std::map<std::string, std::map<std::string, EventSlot> >::iterator it;
00242 it = mEvents.find(groupName);
00243
00244 if(it != mEvents.end())
00245 return false;
00246
00247
00248 std::map<std::string, EventSlot> group;
00249 mEvents[groupName] = group;
00250
00251 return true;
00252 }
00253
00254
00255 bool EventManager::createEventSlot(const std::string &groupName, const std::string &slotName)
00256 {
00257
00258 std::map<std::string, std::map<std::string, EventSlot> >::iterator itGroups;
00259 itGroups = mEvents.find(groupName);
00260
00261 if(itGroups == mEvents.end())
00262 return false;
00263
00264 std::map<std::string, EventSlot> &group = itGroups->second;
00265
00266
00267 std::map<std::string, EventSlot>::iterator itSlots = group.find(slotName);
00268
00269 if(itSlots != group.end())
00270 return false;
00271
00272
00273 EventSlot slot;
00274 group[slotName] = slot;
00275
00276 return true;
00277 }
00278
00279
00280 bool EventManager::emitEvent(const std::string &groupName, const std::string &slotName, void *p)
00281 {
00282
00283 std::map<std::string, std::map<std::string, EventSlot> >::iterator itGroups;
00284 itGroups = mEvents.find(groupName);
00285
00286 if(itGroups == mEvents.end())
00287 return false;
00288
00289 std::map<std::string, EventSlot> &group = itGroups->second;
00290
00291
00292 std::map<std::string, EventSlot>::iterator itSlots = group.find(slotName);
00293
00294 if(itSlots == group.end())
00295 return false;
00296
00297 EventSlot &slot = itSlots->second;
00298
00299
00300 slot.emit(p);
00301 return true;
00302 }
00303
00304
00305 bool EventManager::getIsKeyDown(EKEY_CODE keyCode) const
00306 {
00307 return mKeyState[keyCode];
00308 }
00309
00310
00311 bool EventManager::getIsKeyUp(EKEY_CODE keyCode) const
00312 {
00313 return !mKeyState[keyCode];
00314 }
00315
00316
00317 s32 EventManager::getMouseX() const
00318 {
00319 return mMousePos.X;
00320 }
00321
00322
00323 s32 EventManager::getMouseY() const
00324 {
00325 return mMousePos.Y;
00326 }
00327
00328
00329 const vector2di& EventManager::getMousePos() const
00330 {
00331 return mMousePos;
00332 }
00333
00334
00335 f32 EventManager::getMouseWheel() const
00336 {
00337 return mMouseWheel;
00338 }
00339
00340
00341
00342 void EventManager::removeEventGroups()
00343 {
00344
00345 mEvents.clear();
00346 }
00347
00348
00349 bool EventManager::removeEventSlots(const std::string &groupName)
00350 {
00351
00352 std::map<std::string, std::map<std::string, EventSlot> >::iterator itGroups;
00353 itGroups = mEvents.find(groupName);
00354
00355 if(itGroups == mEvents.end())
00356 return false;
00357
00358 std::map<std::string, EventSlot> &group = itGroups->second;
00359
00360
00361 group.clear();
00362 return true;
00363 }
00364
00365
00366 bool EventManager::removeEventGroup(const std::string &groupName)
00367 {
00368
00369 std::map<std::string, std::map<std::string, EventSlot> >::iterator itGroups;
00370 itGroups = mEvents.find(groupName);
00371
00372 if(itGroups == mEvents.end())
00373 return false;
00374
00375
00376 mEvents.erase(itGroups);
00377 return true;
00378 }
00379
00380
00381 bool EventManager::removeEventSlot(const std::string &groupName, const std::string &slotName)
00382 {
00383
00384 std::map<std::string, std::map<std::string, EventSlot> >::iterator itGroups;
00385 itGroups = mEvents.find(groupName);
00386
00387 if(itGroups == mEvents.end())
00388 return false;
00389
00390 std::map<std::string, EventSlot> &group = itGroups->second;
00391
00392
00393 std::map<std::string, EventSlot>::iterator itSlots = group.find(slotName);
00394
00395 if(itSlots == group.end())
00396 return false;
00397
00398
00399 group.erase(itSlots);
00400 return true;
00401 }
00402
00403
00404 bool EventManager::OnEvent(const SEvent &event)
00405 {
00406
00407 if(event.EventType == EET_KEY_INPUT_EVENT)
00408 {
00409
00410 mKeyState[event.KeyInput.Key] = event.KeyInput.PressedDown;
00411
00412
00413 std::string slotName;
00414
00415 if(event.KeyInput.PressedDown)
00416 slotName = "onKeyDown#";
00417
00418 else slotName = "onKeyUp#";
00419
00420 mEvents["EventManager"][slotName + mKeyMap[event.KeyInput.Key]].emit((void*)&event.KeyInput);
00421 }
00422
00423
00424 if(event.EventType == EET_MOUSE_INPUT_EVENT)
00425 {
00426 switch(event.MouseInput.Event)
00427 {
00428 case EMIE_MOUSE_MOVED:
00429 mMousePos.X = event.MouseInput.X;
00430 mMousePos.Y = event.MouseInput.Y;
00431 mEvents["EventManager"]["onMouseMoved"].emit((void*)&event.MouseInput);
00432 break;
00433
00434 case EMIE_LMOUSE_PRESSED_DOWN:
00435 mEvents["EventManager"]["onMouseDown#Left"].emit((void*)&event.MouseInput);
00436 break;
00437
00438 case EMIE_RMOUSE_PRESSED_DOWN:
00439 mEvents["EventManager"]["onMouseDown#Right"].emit((void*)&event.MouseInput);
00440 break;
00441
00442 case EMIE_MMOUSE_PRESSED_DOWN:
00443 mEvents["EventManager"]["onMouseDown#Middle"].emit((void*)&event.MouseInput);
00444 break;
00445
00446 case EMIE_LMOUSE_LEFT_UP:
00447 mEvents["EventManager"]["onMouseUp#Left"].emit((void*)&event.MouseInput);
00448 break;
00449
00450 case EMIE_RMOUSE_LEFT_UP:
00451 mEvents["EventManager"]["onMouseUp#Right"].emit((void*)&event.MouseInput);
00452 break;
00453
00454 case EMIE_MMOUSE_LEFT_UP:
00455 mEvents["EventManager"]["onMouseUp#Middle"].emit((void*)&event.MouseInput);
00456 break;
00457
00458 case EMIE_MOUSE_WHEEL:
00459 mMouseWheel = event.MouseInput.Wheel;
00460 mEvents["EventManager"]["onMouseWheel"].emit((void*)&event.MouseInput);
00461 break;
00462
00463 default: break;
00464 }
00465 }
00466
00467
00468 if(event.EventType == EET_GUI_EVENT)
00469 mEvents["EventManager"]["onGUIEvent"].emit((void*)&event.GUIEvent);
00470
00471 return false;
00472 }
00473
00474