diff options
Diffstat (limited to 'src/client/lua_api/gui/iguiwindow.cpp')
| -rw-r--r-- | src/client/lua_api/gui/iguiwindow.cpp | 80 |
1 files changed, 29 insertions, 51 deletions
diff --git a/src/client/lua_api/gui/iguiwindow.cpp b/src/client/lua_api/gui/iguiwindow.cpp index 9a42dff..150ba68 100644 --- a/src/client/lua_api/gui/iguiwindow.cpp +++ b/src/client/lua_api/gui/iguiwindow.cpp @@ -20,82 +20,59 @@ 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"); - return (LIGUIElement*) ud; -} +static bool iguiwindowevent(irr::SEvent e){ + int ref = iguielements[e.GUIEvent.Caller]; + lua_rawgeti(tL,LUA_REGISTRYINDEX,ref); + lua_getfield(tL,-1,"close"); -static LIGUIElement* checkiguiwindow(lua_State* L){ - return checkiguiwindow(L,1); -} + lua_rawgeti(tL,LUA_REGISTRYINDEX,ref); + lua_call(tL,1,1); -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; + int shouldclose = lua_toboolean(tL,-1); + return shouldclose == 1; } -//new({width,height},{posx,posy},"title"[,parent]) +//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); - //Create the window IGUIEnvironment* env = guidevice->getGUIEnvironment(); IGUIWindow* wi = env->addWindow( 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,11 +82,12 @@ 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}, }; int iguiwindow_register(lua_State* L, IrrlichtDevice* d){ - printf("Loading window\n"); luaL_newmetatable(L,"gui.window");//m{gui.checkbox} luaL_register(L,NULL,iguiwindow_m); lua_pop(L,1);// |
