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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*This file defines some things that all igui stuff can do*/
/***
All gui elements inherit from iguielement.
Some functions (like settext()) do different things for different elements.
All gui elements can call the following callbacks:
onFocus(self)
onUnfous(self)
onHover(self)
onLeave(self)
@classmod iguielement
*/
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <shared/lua_api/common.hpp>
#include <irrlicht.h>
#include "../guiparts.hpp"
#include <client/callbackhandeler.hpp>
using namespace irr;
using namespace core;
using namespace gui;
/***
Move an element (by an offset) from it's current position
@function guielement:move()
@tparam vector2d position The offset to move this element by
*/
//move({element},{x,y}) -> nil
int moveiguielement(lua_State* L){
//printf("Got call to move element\n");
long x,y;
popvector2i(L,&x,&y); //{element}
//printf("I want to move to %d %d\n",x,y);
lua_getfield(L,-1,"guielement");//{element},*element
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);
//printf("Found element to move: %p\n",el);
lua_pop(L,2);//
el->move(position2d<s32>(x,y));
el->updateAbsolutePosition();
return 0;
}
/***
Set the visibility of this element
@function guielement:setvisible()
@tparam boolean visible Should this element be visible?
*/
int setvisible(lua_State *L){
int v = lua_toboolean(L,-1);
lua_pop(L,1);
lua_getfield(L,-1,"guielement");
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);//{element},ud_element
lua_pop(L,2);
el->setVisible(v == 1);
return 0;
}
/***
Find the rectangle that an element occupies
@function guielement:getabsrect()
@treturn rect The rectangle that this element occupies
*/
//getabsrect({element})-> {{sx,sy},{ex,ey}}
int getiguiclippingrect(lua_State* L){
lua_getfield(L,-1,"guielement");
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);//{element},ud_element
lua_pop(L,2);
core::rect<s32> rect = el->getAbsolutePosition();
pushrecti(
L,
rect.UpperLeftCorner.X,
rect.UpperLeftCorner.Y,
rect.LowerRightCorner.X,
rect.LowerRightCorner.Y
);
return 1;
}
/***
Find the rectangle that an element occupies that is visible to the user
@function guielement:getabsclippingrect()
@treturn rect The rectangle that this element occupies that is visible to the user
*/
//getabsclippingrect({element}) :: {{sx,sy},{ex,ey}}
int getiguiabsclippingrect(lua_State *L){
lua_getfield(L,-1,"guielement");
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);//{element},ud_element
lua_pop(L,2);
core::rect<s32> rect = el->getAbsoluteClippingRect();
pushrecti(
L,
rect.UpperLeftCorner.X,
rect.UpperLeftCorner.Y,
rect.LowerRightCorner.X,
rect.LowerRightCorner.Y
);
return 1;
}
/***
Find the relative rectangle that this element occupies
@function guielement:getrelrect()
@treturn rect The rectangle that this element occupies relative to it's parent
*/
int getiguirelrect(lua_State *L){
lua_getfield(L,-1,"guielement");
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);//{element},ud_element
lua_pop(L,2);
core::rect<s32> rect = el->getRelativePosition();
pushrecti(
L,
rect.UpperLeftCorner.X,
rect.UpperLeftCorner.Y,
rect.LowerRightCorner.X,
rect.LowerRightCorner.Y
);
return 1;
}
/***
Sets the text of the element
This function may do different things to different gui elements.
For example, on a window, it sets the title.
On a button, it sets the button's text.
@function guielement:settext()
@tparam string text The text to set on the element
*/
//setText({guielement},"text") :: nil
int setiguitext(lua_State* L){
const char* text = lua_tostring(L, -1);
lua_pop(L,1);//{guielement}
lua_getfield(L,-1,"guielement");//{guielement},ud_iguielement
IGUIElement* el = (IGUIElement*)lua_touserdata(L,-1);//{guielement},ud_iguielement
lua_pop(L,2);
int textlen = strlen(text);
wchar_t* wtext = (wchar_t*)malloc(sizeof(wchar_t)*textlen);
mbstowcs(wtext,text,textlen);
el->setText(wtext);
free(wtext);
return 0;
}
//setRect({guielement},{{sx,sy},{ex,ey}}) :: nil
int setrelrect(lua_State *L){
long sx,sy,ex,ey;
poprecti(L,&sx,&sy,&ex,&ey);
printf("Seting rect %ld %ld %ld %ld\n",sx,sy,ex,ey);
lua_getfield(L,-1,"guielement");//{guielement},ud_element
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);
lua_pop(L,2);
el->setRelativePosition(rect<s32>(sx,sy,ex,ey));
return 0;
}
/***
@function guielement:gettext()
@treturn string The caption text of the element. For input element like
editboxes, this returns the text that the edit box contains.
*/
//{guieditbox}:gettext() :: "caption_text"
int getiguitext(lua_State* L){
lua_getfield(L, -1, "guielement");//{guieditbox},ud_guielement
irr::gui::IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);
lua_pop(L,2);//
const wchar_t *t = el->getText();
size_t cstrlen = wcslen(t);
printf("In gui get text, cstrlen is %zu\n",cstrlen);
char output[cstrlen + 1];//+1 for \0
wcstombs(output,t,cstrlen);
output[cstrlen] = '\0';
lua_pushstring(L,output);//"str"
return 1;
}
/***
Removes a gui element, and any child elements
@function guielement:remove()
*/
//remove({self})
int removeiguielement(lua_State* L){
lua_getfield(L,-1,"guielement");
IGUIElement *ele = (IGUIElement*)lua_touserdata(L,-1);
ele->remove();
lua_pop(L,2);
return 0;
}
class guicallback{
private:
int luaitem;
const char* funcname;
lua_State* L;
public:
guicallback(lua_State* L, int lf, const char* fn){
this->luaitem = lf;
this->L = L;
this->funcname = fn;
}
bool operator () (SEvent e) {
lua_rawgeti(L,LUA_REGISTRYINDEX,this->luaitem);//{guielement}
lua_getfield(L,-1,this->funcname);//{guielement},func()
if(!lua_isnil(L,-1)){
lua_rawgeti(L,LUA_REGISTRYINDEX,this->luaitem);//{guielement},func(),{guielement}
lua_call(L,1,0);//{iguielement}
lua_pop(L,1);//{}
return true;
}else{
lua_pop(L,2);//{}
return false;
}
}
};
//{guielement}
//popelementcallback(lua_State* L, gui::EGUI_EVENT_TYPE, char*)
void setelementcallback(lua_State* L, gui::EGUI_EVENT_TYPE et, const char* funcname){
registerguielement(L,et,funcname);
}
//ud_iguielement
int guigetid(lua_State* L){
IGUIElement* el = (IGUIElement*)lua_touserdata(L,-1);
lua_pop(L,1);
int id = el->getID();
lua_pushnumber(L,id);
return 1;
}
extern const luaL_reg iguielement_m[] = {
{"move", moveiguielement},
{"setvisible", setvisible},
{"setrect", setrelrect},
{"getabsrect", getiguiclippingrect},
{"getabsclippingrect", getiguiabsclippingrect},
{"getrelrect", getiguirelrect},
{"settext", setiguitext},
{"gettext", getiguitext},
{"remove", removeiguielement},
{NULL, NULL}
};
|