aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2018-04-11 19:30:55 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2018-04-11 19:32:31 -0400
commit14cf4b9ef17b01fa60014505db86fe01166c068f (patch)
tree698eeedcee4a82d99b658d39096f1206110fcc0a /src
parent254982b8991aacce7a54cc57fa5cba6b3f75d430 (diff)
downloadbrokengine-14cf4b9ef17b01fa60014505db86fe01166c068f.tar.gz
brokengine-14cf4b9ef17b01fa60014505db86fe01166c068f.tar.bz2
brokengine-14cf4b9ef17b01fa60014505db86fe01166c068f.zip
Made gui windows use metatables
GUI windows now use metatables, onClose() renamed to close()
Diffstat (limited to 'src')
-rw-r--r--src/client/lua_api/gui/iguiwindow.cpp81
-rw-r--r--src/client/lua_api/load_gui.cpp2
2 files changed, 44 insertions, 39 deletions
diff --git a/src/client/lua_api/gui/iguiwindow.cpp b/src/client/lua_api/gui/iguiwindow.cpp
index 9a42dff..14fba99 100644
--- a/src/client/lua_api/gui/iguiwindow.cpp
+++ b/src/client/lua_api/gui/iguiwindow.cpp
@@ -20,8 +20,6 @@ extern "C" {
using namespace irr;
using namespace gui;
-//IrrlichtDevice* guidevice;
-
static LIGUIElement* checkiguiwindow(lua_State* L, int index){
void* ud = luaL_checkudata(L,index,"gui.iguiwindow");
luaL_argcheck(L,ud != NULL, index, "'gui.iguiwindow' expected");
@@ -33,49 +31,48 @@ static LIGUIElement* checkiguiwindow(lua_State* L){
}
static bool iguiwindowevent(irr::SEvent e){
- IGUIElement* caller = (IGUIElement*)e.GUIEvent.Caller;
- int ref = iguielements[caller];
- EGUI_EVENT_TYPE etype = e.GUIEvent.EventType;
- printf("Detected window event\n");
- if(etype == EGET_ELEMENT_CLOSED){
- 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 = checkiguiwindow(tL,-1);
- int hashmapresponse;
- char* hashkey = (char*)"onclose";
- int terror = hashmap_get(tbut->funcmap,hashkey,(void**)&hashmapresponse);
- if(terror == MAP_OK){ //Only call if we actually have that function.
- printf("Looks like we have an onclose function, calling!\n");
- lua_rawgeti(tL,LUA_REGISTRYINDEX,hashmapresponse); //push the function
- lua_rawgeti(tL,LUA_REGISTRYINDEX,ref); //push the referance to iguielement
- lua_call(tL,1,1);
- //int b = lua_isnoneornil(tL,1);
- int a = lua_toboolean(tL,-1);
- printf("a:%d\n",a);
- return a;
- }
- }
- printf("Oh no! an iguiwindow generated an event!");
- return false;
+ printf("I got a guiwindow event on %p!\n",e.GUIEvent.Caller);
+ int ref = iguielements[e.GUIEvent.Caller];
+ printf("Ref for this guielement is:%d\n",ref);
+ lua_rawgeti(tL,LUA_REGISTRYINDEX,ref);
+ lua_getfield(tL,-1,"close");
+
+ int i = lua_isfunction(tL,-1);
+ printf("Is function: %d\n",i);
+ lua_rawgeti(tL,LUA_REGISTRYINDEX,ref);
+ lua_call(tL,1,1);
+
+ int shouldclose = lua_toboolean(tL,-1);
+ printf("Should close: %d\n",shouldclose);
+ return shouldclose == 1;
+
+ return false;
}
-//new({width,height},{posx,posy},"title"[,parent])
+//window:close()
+static int iguiwindowclose(lua_State* L){
+ IGUIElement* self = (IGUIElement*)lua_touserdata(L,-1);
+ lua_pop(L,1);
+}
+
+//new({posx,posy},{width,height},"title"[,parent])
static int newiguiwindow(lua_State* L){
printf("Creating window\n");
-
+ IGUIElement* parent = NULL;
int numargs = lua_gettop(L);
-
- int parentid = lua_tointeger(L,-1);
- lua_pop(L,1);
+ if(numargs == 4){
+ parent = (IGUIElement*)lua_touserdata(L,-1);
+ lua_pop(L,1);
+ }
const char* title_c = lua_tostring(L,-1);
const wchar_t* title_w = irr::core::stringw(title_c).c_str();
lua_pop(L,1);
//Frame position
long x,y,w,h;
- popvector2i(L,&x,&y);
popvector2i(L,&w,&h);
+ popvector2i(L,&x,&y);
printf("I want to make a frame at (%d,%d) size (%d,%d)\n",x,y,w,h);
@@ -85,17 +82,23 @@ static int newiguiwindow(lua_State* L){
core::rect<s32>(x,y,x+w,y+h),
false,
title_w,
- guielements[parentid],
+ parent,
-1
);
- lua_newtable(L);//{}
lua_pushlightuserdata(L,wi);
- lua_setfield(L,-2,"element");
-
- luaL_getmetatable(L,"gui.window");
- lua_setmetatable(L,-2);
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,wi);//{},{ud_window}
+ lua_setfield(L,-2,"element");//{element=ud_window}
+
+ luaL_getmetatable(L,"gui.window");//{element=ud_window},{m_gui.window}
+ lua_setmetatable(L,-2);//{element=ud_window, __meta=gui.window}
+
+ int ref = luaL_ref(L,LUA_REGISTRYINDEX);//ref
+ lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//ref,{element=ud_window, __meta=gui.window}
+ iguielements[wi] = ref;
+
registerguicallback(wi,EGET_ELEMENT_CLOSED,iguiwindowevent);
return 1;
}
@@ -105,6 +108,8 @@ static const luaL_reg iguiwindow_m[] = {
{"settext", setiguitext},
{"remove", removeiguielement},
{"getid", guigetid},
+// bool :: iguiwindow:close() -- Called when window is closed, returning
+// -- Anything but false or nil prevents close
{0, 0},
};
diff --git a/src/client/lua_api/load_gui.cpp b/src/client/lua_api/load_gui.cpp
index b2e941b..eb611ef 100644
--- a/src/client/lua_api/load_gui.cpp
+++ b/src/client/lua_api/load_gui.cpp
@@ -40,7 +40,6 @@ void load_guifuncs(lua_State* L){
gui_elenum = 0;
guielements[0] = NULL;
- iguibutton_register(L,device);
iguilabel_register(L,device);
lua_pop(L, 1);
@@ -52,6 +51,7 @@ void load_guifuncs(lua_State* L){
iguicheckbox_register(L);
iguiwindow_register(L,device);
iguiimage_register(L);
+ iguibutton_register(L);
lua_pushcfunction(L,screenwidth);
lua_setglobal(L,"scrw");