#include #include #include #include #include #include #include extern "C" { #include #include #include } #include #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(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; }