1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#include <stdio.h>
#include <stdlib.h>
#include <vector>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <irrlicht.h>
#include <map>
#include "callbackhandeler.hpp"
using namespace irr;
using namespace gui;
using namespace std;
extern lua_State* L;
std::map<IGUIElement*,std::map<EGUI_EVENT_TYPE, bool(*)(irr::SEvent)> > 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){
//printf("Found a callback for this event\n");
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);
switch(se.Event){
case EMIE_MOUSE_MOVED:{
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 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;
}
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;
}
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);
}
}
}
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;
}
}
|