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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <memory>
#include <map>
#include <string>
#include <functional>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <irrlicht.h>
#include "../guiparts.hpp"
#include "iguielement.hpp"
#include "../../callbackhandeler.hpp"
#include "../../util/hashmap.h"
using namespace irr;
using namespace gui;
IrrlichtDevice* device;
char lhashkey[20];
static LIGUIElement* checkiguibutton(lua_State* L, int index){
void* ud = luaL_checkudata(L,index,"gui.iguibutton");
luaL_argcheck(L,ud != NULL, index, "'gui.iguibutton' expected");
return (LIGUIElement*) ud;
}
static LIGUIElement* checkiguibutton(lua_State* L){
return checkiguibutton(L,1);
}
//The callback even for all gui buttons
static void iguibuttonevent(irr::SEvent e){
IGUIElement* caller = (IGUIElement*)e.GUIEvent.Caller;
printf("Some gui something somewhere was pressed\n");
if(iguielements.find(caller) != iguielements.end()){
printf("I know what reference is linked to that button: %d!\n",iguielements[caller]);
int ref = iguielements[caller];
EGUI_EVENT_TYPE etype = e.GUIEvent.EventType;
if(etype == EGET_BUTTON_CLICKED){
lua_rawgeti(tL,LUA_REGISTRYINDEX,ref);
printf("getting raw, the thing on the top of stack is a %s\n",luaL_typename(tL,-1));
LIGUIElement* tbut = checkiguibutton(tL,-1);
int hashmapresponse;
char* hashkey = (char*)"onpress";
int terror = hashmap_get(tbut->funcmap,hashkey,(void**)&hashmapresponse);
if(terror == MAP_OK){
lua_rawgeti(tL,LUA_REGISTRYINDEX,hashmapresponse);
lua_call(tL,0,0);
}else{
printf("Tried to call onpress on something that dosen't have onpress\n");
}
}else if(etype == EGET_ELEMENT_HOVERED){
printf("Trying to call onhovered!\n");
lua_rawgeti(tL,LUA_REGISTRYINDEX,ref);
LIGUIElement* tbut = (LIGUIElement*)lua_touserdata(tL,-1);
int hashmapresponse;
int terror = hashmap_get(tbut->funcmap,(char*)"onhovered",(void**)&hashmapresponse);
if(terror == MAP_OK){
lua_rawgeti(tL,LUA_REGISTRYINDEX,hashmapresponse);
lua_call(tL,0,0);
}
}
}
}
static int newiguibutton(lua_State* L){
printf("Createing gui button!\n");
//The position of the button
int startx = luaL_optint(L,1,0);
int starty = luaL_optint(L,2,0);
int endx = luaL_optint(L,3,startx+100);
int endy = luaL_optint(L,4,starty+100);
//Label and tooltip
wchar_t* button_label;
wchar_t* button_tooltip;
const char* labelopt = luaL_optstring(L,5,"Button");
const char* tooltipopt = luaL_optstring(L,6,"Tooltip");
int bls = strlen(labelopt);
int bts = strlen(tooltipopt);
button_label = (wchar_t*)malloc(sizeof(wchar_t)*(bls));
button_tooltip = (wchar_t*)malloc(sizeof(wchar_t)*(bts));
mbstowcs(button_label,labelopt,bls+1);
mbstowcs(button_tooltip,tooltipopt,bts+1);
printf("Got the string options\n");
//If the element has a parrent
int parent = luaL_optint(L,7,0);
//Create the button
IGUIEnvironment* env = device->getGUIEnvironment();
IGUIButton* nbut = (IGUIButton*) env->addButton(core::rect<s32>(startx,starty,endx,endy), guielements[parent], gui_elenum++, button_label, button_tooltip);
//Register it's callback
registerguicallback(nbut,EGET_BUTTON_CLICKED,iguibuttonevent);
printf("Finished registering callback\n");
//Create the lua representation of the button
LIGUIElement* lbut = (LIGUIElement*)lua_newuserdata(L, sizeof(LIGUIElement));
//Make it callbackable
int tref = luaL_ref(L,LUA_REGISTRYINDEX);
iguielements[nbut] = tref;
lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
//Set it's metatable
luaL_getmetatable(L, "gui.iguibutton");
lua_setmetatable(L, -2);
//Create the struct
lbut->e = nbut;
lbut->funcmap = hashmap_new();
lbut->type = "iguibutton";
//Free up anything made in the function
free(button_label);
free(button_tooltip);
//Put it on top and return it
lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
return 1;
}
static const luaL_reg iguibutton_f[] = {
{"new", newiguibutton},
{"gethandeler", guigethandeler},
{"sethandeler", guisethandeler},
{0,0},
};
static const luaL_reg iguibutton_m[] = {
{"move", moveiguielement},
{"settext", setiguitext},
{"remove", removeiguielement},
{0,0},
};
int iguibutton_register(lua_State* L, IrrlichtDevice* d){
device = d;
tL = L;
luaL_newmetatable(L, "gui.iguibutton");
luaL_register(L,"iguibutton",iguibutton_f);
lua_pushstring(L,"__index");
lua_pushstring(L,"gethandeler");
lua_gettable(L,-3);
lua_settable(L,-4);
lua_pushstring(L,"__newindex");
lua_pushstring(L,"sethandeler");
lua_gettable(L,-3);
lua_settable(L,-4);
luaL_register(L, NULL, iguibutton_m);
return 1;
}
|