aboutsummaryrefslogtreecommitdiff
path: root/src/client/callbackhandeler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/callbackhandeler.cpp')
-rw-r--r--src/client/callbackhandeler.cpp173
1 files changed, 118 insertions, 55 deletions
diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp
index 76fe693..1200074 100644
--- a/src/client/callbackhandeler.cpp
+++ b/src/client/callbackhandeler.cpp
@@ -17,102 +17,163 @@ using namespace std;
extern lua_State* L;
-std::map<IGUIElement*,std::map<EGUI_EVENT_TYPE, bool(*)(irr::SEvent)> > guifuncs;
+std::map<IGUIElement*, int> guielements;
//IrrlichtDevice* device;
-void registerguicallback(IGUIElement* element, EGUI_EVENT_TYPE event, bool (*func)(irr::SEvent)){
- printf("Callback registered\n");
- guifuncs[element][event] = func;
+//For basic events
+//{guielement}
+void registerguielement(lua_State* L){
+ int ref = luaL_ref(L,LUA_REGISTRYINDEX);//
+ lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement}
+ lua_getfield(L,-1,"guielement");//{guielement},ud_guielement
+ IGUIElement* el = (IGUIElement*)lua_touserdata(L,-1);//{guielement},ud_guielement
+ guielements[el] = ref;
+ lua_pop(L,1);//{guielement}
}
+//void registerguicallback(IGUIElement* element, EGUI_EVENT_TYPE event, bool (*func)(irr::SEvent)){
+ //printf("Callback registered\n");
+ //guifuncs[element][event] = func;
+//}
+////For objects that have overloaded ()
+//void registerguicallback(IGUIElement* element, EGUI_EVENT_TYPE event, std::function<bool(irr::SEvent)> obj){
+ //guifuncs[element][event] = obj;
+//}
GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){
+
//device = d;
}
+
+void callMouse(lua_State* L, const char* funcname, double x, double y, double event){
+ lua_getglobal(L,"GAME");
+ lua_getfield(L,-1,funcname);
+ if(!lua_isnil(L,-1)){
+ lua_pushnumber(L,x);
+ lua_pushnumber(L,y);
+ lua_pushnumber(L,event);
+ lua_call(L,3,0);
+ lua_pop(L,1);
+ }else{
+ lua_pop(L,2);
+ }
+}
+
bool GlobalEventReceiver::OnEvent(const SEvent& e){
+ //lua_State* L = this->L;
EEVENT_TYPE type = e.EventType;
+ SEvent::SMouseInput se = e.MouseInput;
//printf("Onevent called:%d\n",(int)type);
switch (type){
case EET_GUI_EVENT:{
+ //printf("Gui event\n");
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){
- //printf("Found a callback for this event\n");
- return guifuncs[caller][get](e);
+ if(guielements.find(caller) == guielements.end())
+ return false;
+ int ref = guielements[caller];
+ lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement}
+ const char* fieldname;
+ switch(get){
+ case EGET_ELEMENT_FOCUSED: fieldname = "onFocus"; break;
+ case EGET_ELEMENT_FOCUS_LOST: fieldname = "onUnfocus"; break;
+ case EGET_ELEMENT_HOVERED: fieldname = "onHover"; break;
+ case EGET_ELEMENT_LEFT: fieldname = "onLeave"; break;
+ case EGET_ELEMENT_CLOSED: fieldname = "onClose"; break;
+ case EGET_BUTTON_CLICKED: fieldname = "onClick"; break;
+ case EGET_SCROLL_BAR_CHANGED: fieldname = "onScroll"; break;
+ case EGET_CHECKBOX_CHANGED: fieldname = "onCheck"; break;
+ case EGET_LISTBOX_CHANGED: fieldname = "onChange"; break;
+ case EGET_LISTBOX_SELECTED_AGAIN: fieldname = "onSame"; break;
+ case EGET_FILE_SELECTED: fieldname = "onFileSelect"; break;
+ case EGET_DIRECTORY_SELECTED: fieldname = "onDirectorySelect"; break;
+ case EGET_FILE_CHOOSE_DIALOG_CANCELLED: fieldname = "onFileChooserCancel"; break;
+ case EGET_MESSAGEBOX_YES: fieldname = "onYes"; break;
+ case EGET_MESSAGEBOX_NO: fieldname = "onNo"; break;
+ case EGET_MESSAGEBOX_OK: fieldname = "onOk"; break;
+ case EGET_MESSAGEBOX_CANCEL: fieldname = "onCancel"; break;
+ case EGET_EDITBOX_ENTER: fieldname = "onEneter"; break;
+ case EGET_EDITBOX_CHANGED: fieldname = "onChange"; break;
+ case EGET_EDITBOX_MARKING_CHANGED: fieldname = "onMarkChange"; break;
+ case EGET_TAB_CHANGED: fieldname = "onTabChange"; break;
+ case EGET_MENU_ITEM_SELECTED: fieldname = "onSelect"; break;
+ case EGET_COMBO_BOX_CHANGED: fieldname = "onChange"; break;
+ case EGET_SPINBOX_CHANGED: fieldname = "onChange"; break;
+ case EGET_TABLE_CHANGED: fieldname = "onChange"; break;
+ case EGET_TABLE_HEADER_CHANGED: fieldname = "onHeadersChange"; break;
+ case EGET_TABLE_SELECTED_AGAIN: fieldname = "onReselect"; break;
+ case EGET_TREEVIEW_NODE_DESELECT: fieldname = "onNodeDeselect"; break;
+ case EGET_TREEVIEW_NODE_SELECT: fieldname = "onNodeSelect"; break;
+ case EGET_TREEVIEW_NODE_EXPAND: fieldname = "onNodeExpand"; break;
+ case EGET_TREEVIEW_NODE_COLLAPSE: fieldname = "onNodeCollapse"; break;
+ case EGET_COUNT: break;
}
- return false;
- break;
+
+ //printf("got fieldname:%s\n",fieldname);
+ lua_getfield(L,-1,fieldname);//{guielement},func()
+ if(lua_isnil(L,-1)){
+ lua_pop(L,2);
+ return false;
+ }
+ lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement},func(),{guielement}
+ lua_call(L,1,0);//{guielement}
+ lua_pop(L,1);//
+ return true;
}
+ break;
case EET_MOUSE_INPUT_EVENT:{
- SEvent::SMouseInput se = e.MouseInput;
- //printf("X: %d Y: %d\n",se.X, se.Y);
+ //printf("Mouse event - X: %d Y: %d\n",se.X, se.Y);
switch(se.Event){
case EMIE_MOUSE_MOVED:{
+ //printf("Mouse moved...\n");
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);
+ lua_pushnumber(L,se.X);//{},onMouseMove(),X
+ lua_pushnumber(L,se.Y);//{},onMouseMove(),Y
+ lua_call(L,2,0);//{}
+ lua_pop(L,1);//
}else{
lua_pop(L,2);
}
- break;
}
+ break;
case EMIE_LMOUSE_PRESSED_DOWN:
case EMIE_RMOUSE_PRESSED_DOWN:
case EMIE_MMOUSE_PRESSED_DOWN:{
- lua_getglobal(L,"GAME");
- lua_getfield(L,-1,"onMouseDown");
- if(!lua_isnil(L,-1)){
- lua_pushnumber(L,se.X);
- lua_pushnumber(L,se.Y);
- lua_pushnumber(L,se.Event);
- lua_call(L,3,0);
- lua_pop(L,1);
- }else{
- lua_pop(L,2);
- }
- break;
+ printf("Mouse down\n");
+ callMouse(L,"onMouseDown",se.X,se.Y,se.Event);
}
+ break;
case EMIE_LMOUSE_LEFT_UP:
case EMIE_RMOUSE_LEFT_UP:
case EMIE_MMOUSE_LEFT_UP:{
- lua_getglobal(L,"GAME");
- lua_getfield(L,-1,"onMouseUp");
- if(!lua_isnil(L,-1)){
- lua_pushnumber(L,se.X);
- lua_pushnumber(L,se.Y);
- lua_pushnumber(L,se.Event);
- lua_call(L,3,0);
- lua_pop(L,1);
- }else{
- lua_pop(L,2);
- }
- break;
+ printf("Mouse up\n");
+ callMouse(L,"onMouseUp",se.X,se.Y,se.Event);
}
+ break;
case EMIE_MOUSE_WHEEL:{
- lua_getglobal(L,"GAME");
- lua_getfield(L,-1,"onMouseWheel");
- if(!lua_isnil(L,-1)){
- lua_pushnumber(L,se.X);
- lua_pushnumber(L,se.Y);
- lua_pushnumber(L,se.Wheel);
- lua_call(L,3,0);
- lua_pop(L,1);
- }else{
- lua_pop(L,2);
- }
+ callMouse(L,"onMouseWheel",se.X,se.Y,se.Wheel);
}
+ break;
+ case EMIE_RMOUSE_DOUBLE_CLICK:
+ case EMIE_MMOUSE_DOUBLE_CLICK:
+ case EMIE_LMOUSE_DOUBLE_CLICK:{
+ callMouse(L,"onDoubleClick",se.X,se.Y,se.Event);
}
-
break;
+ case EMIE_RMOUSE_TRIPLE_CLICK:
+ case EMIE_MMOUSE_TRIPLE_CLICK:
+ case EMIE_LMOUSE_TRIPLE_CLICK:{
+ callMouse(L,"onTripleClick",se.X,se.Y,se.Event);
+ }
+ break;
+ case EMIE_COUNT:break;
+ }
}
+ break;
case EET_KEY_INPUT_EVENT:{
//printf("Got input event\n");
SEvent::SKeyInput se = e.KeyInput;
@@ -130,8 +191,10 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){
}
break;
}
- default:
+ default:{
//printf("Called an unknown event\n");
return false;
}
+ }
+ return true;
}