diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-11-01 00:28:16 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-11-01 00:28:16 -0400 |
| commit | d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc (patch) | |
| tree | 9e005502ea2c125c90b5011f573f381f84ade0ef /src/client/lua_api/gui/iguielement.cpp | |
| download | brokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.tar.gz brokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.tar.bz2 brokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.zip | |
Initial commit
Diffstat (limited to 'src/client/lua_api/gui/iguielement.cpp')
| -rw-r--r-- | src/client/lua_api/gui/iguielement.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/client/lua_api/gui/iguielement.cpp b/src/client/lua_api/gui/iguielement.cpp new file mode 100644 index 0000000..cb395af --- /dev/null +++ b/src/client/lua_api/gui/iguielement.cpp @@ -0,0 +1,93 @@ +/*This file defines some things that all igui stuff can do*/ +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <irrlicht.h> +#include "../guiparts.hpp" + +using namespace irr; +using namespace core; + +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); +} + +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; +} + +int removeiguielement(lua_State* L){ + LIGUIElement* ele = toiguielement(L,1); + ele->e->remove(); + hashmap_free(ele->funcmap); + free(ele); + 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; +} |
