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
|
/*This file defines some things that all igui stuff can do*/
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include "../../../shared/lua_api/common.h"
#include <irrlicht.h>
#include "../guiparts.hpp"
using namespace irr;
using namespace core;
using namespace gui;
static LIGUIElement* toiguielement(lua_State* L,int index){
LIGUIElement* ret = (LIGUIElement*)lua_touserdata(L,index);
if(ret == NULL)
luaL_typerror(L,index,"LIGUIButton");
return ret;
}
static LIGUIElement* toiguielement(lua_State* L){
return toiguielement(L,1);
}
//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;
}
//getabsrect({element})-> {{sx,sy},{ex,ey}}
int getiguiclippingrect(lua_State* L){
printf("Getting iguiclipping elemnt\n");
lua_getfield(L,-1,"guielement");
IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);
core::rect<s32> rect = el->getAbsoluteClippingRect();
position2d<s32> ul = rect.UpperLeftCorner;
position2d<s32> lr = rect.LowerRightCorner;
lua_newtable(L);
lua_pushnumber(L,1);
double sx,sy,ex,ey;
pushvector2i(L,ul.X,ul.Y);
lua_settable(L,-3);
lua_pushnumber(L,2);
pushvector2i(L,lr.X,lr.Y);
lua_settable(L,-3);
return 1;
}
/*
int moveiguielement(lua_State* L){
LIGUIElement* ele = toiguielement(L,1);
int x = luaL_optint(L,2,0);
int y = luaL_optint(L,3,0);
ele->e->move(position2d<s32>(x,y));
return 0;
}
*/
int setiguitext(lua_State* L){
LIGUIElement* ele = toiguielement(L,1);
const char* str = luaL_optstring(L,2,"");
int size = strlen(str);
wchar_t* al = (wchar_t*)malloc(sizeof(wchar_t)*size);
mbstowcs(al,str,size);
ele->e->setText(al);
free(al);
return 0;
}
//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;
}
int guigethandeler(lua_State* L){
printf("Called the get index handeler!\n");
LIGUIElement* button = toiguielement(L);
if(!lua_isstring(L,2)){
luaL_error(L,"index of iguibutton must be of type 'string'");
}
const char* lstr = lua_tostring(L,2);
char* hashkey = (char*)malloc(sizeof(char)*strlen(lstr));
strcpy(hashkey,lstr);
int ref;
if(hashmap_get(button->funcmap,hashkey,(void**)&ref) == MAP_OK){
//We found the value in our hashmap!
lua_rawgeti(L,LUA_REGISTRYINDEX,ref);
return 1;
}else{
//Could not find in hashmap, check parrent
lua_getglobal(L,button->type);
printf("Button's type is %s\n",button->type);
lua_getfield(L,-1,lstr);
return 1;
}
}
int guisethandeler(lua_State* L){
printf("Called the set index handeler\n");
LIGUIElement* button = toiguielement(L);
if(!lua_isstring(L,2)){
luaL_error(L,"index of iguibutton must be of type 'string'");
}
const char* lstr = lua_tostring(L,2);
char* hashkey = (char*)malloc(sizeof(char)*strlen(lstr));
strcpy(hashkey,lstr);
int oldref;
if(hashmap_get(button->funcmap,hashkey,(void**)&oldref) == MAP_OK){
luaL_unref(L,LUA_REGISTRYINDEX,oldref);
hashmap_remove(button->funcmap,hashkey);
}
int ref = luaL_ref(L,LUA_REGISTRYINDEX);
if(hashmap_put(button->funcmap,hashkey,(void*)ref) != MAP_OK){
luaL_error(L,"Error while setting the hashkey on iguibutton");
}
return 0;
}
//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;
}
|