#include #include #include extern "C" { #include #include #include } #include #include #include "callbackhandeler.hpp" using namespace irr; using namespace gui; using namespace std; extern lua_State* L; std::map > guifuncs; //IrrlichtDevice* device; void registerguicallback(IGUIElement* element, EGUI_EVENT_TYPE event, bool (*func)(irr::SEvent)){ printf("Callback registered\n"); guifuncs[element][event] = func; } GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){ //device = d; } bool GlobalEventReceiver::OnEvent(const SEvent& e){ EEVENT_TYPE type = e.EventType; //printf("Onevent called:%d\n",(int)type); switch (type){ case EET_GUI_EVENT:{ IGUIElement* caller = e.GUIEvent.Caller; EGUI_EVENT_TYPE get = e.GUIEvent.EventType; printf("detected gui event: %d\n",get); bool callerregistered = guifuncs.find(caller) != guifuncs.end(); bool callerhasfunc = guifuncs[caller].find(get) != guifuncs[caller].end(); if (callerregistered && callerhasfunc){ return guifuncs[caller][get](e); } return false; break; } case EET_MOUSE_INPUT_EVENT:{ SEvent::SMouseInput se = e.MouseInput; //printf("X: %d Y: %d\n",se.X, se.Y); lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"onMouseMove");//{},onMouseMove() if(!lua_isnil(L,-1)){ lua_pushnumber(L,se.X); lua_pushnumber(L,se.Y); lua_call(L,2,0); lua_pop(L,1); }else{ lua_pop(L,2); } break; } case EET_KEY_INPUT_EVENT:{ printf("Got input event\n"); SEvent::SKeyInput se = e.KeyInput; lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"onKeyDown");//{},()|nil if(!lua_isnil(L,-1)){ lua_pushnumber(L,se.Key); lua_pushboolean(L,se.PressedDown); lua_pushboolean(L,se.Control); lua_pushboolean(L,se.Shift); lua_call(L,4,0); lua_pop(L,1); }else{ lua_pop(L,2); } break; } default: printf("Called an unknown event\n"); return false; } }