aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2018-04-11 19:08:48 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2018-04-11 19:08:48 -0400
commit9555bf488cb82c079853a1711c3e903003f5b242 (patch)
treeebb83a465bdeec9ac0b8af9cbacb75551a41a192 /src/client/lua_api
parent6cf098e3450ba99c238cf0499c6cecaa246f4d50 (diff)
downloadbrokengine-9555bf488cb82c079853a1711c3e903003f5b242.tar.gz
brokengine-9555bf488cb82c079853a1711c3e903003f5b242.tar.bz2
brokengine-9555bf488cb82c079853a1711c3e903003f5b242.zip
GUI buttons use metatables
GUI buttons now use metatables, and are dependent on some of the shared code. Makefile updated accordingly
Diffstat (limited to 'src/client/lua_api')
-rw-r--r--src/client/lua_api/gui/iguibutton.cpp132
-rw-r--r--src/client/lua_api/gui/iguibutton.hpp2
2 files changed, 53 insertions, 81 deletions
diff --git a/src/client/lua_api/gui/iguibutton.cpp b/src/client/lua_api/gui/iguibutton.cpp
index 494e9f9..3295e6e 100644
--- a/src/client/lua_api/gui/iguibutton.cpp
+++ b/src/client/lua_api/gui/iguibutton.cpp
@@ -15,8 +15,10 @@ extern "C" {
#include "iguielement.hpp"
#include "../../callbackhandeler.hpp"
#include "../../util/hashmap.h"
+#include "../../../shared/lua_api/common.h"
using namespace irr;
+using namespace core;
using namespace gui;
extern IrrlichtDevice* device;
@@ -68,97 +70,67 @@ static bool iguibuttonevent(irr::SEvent e){
return false;
}
+//gui.newbutton({x,y},{width,height},text[,parent])
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<s32>(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;
+ printf("Createing gui button!\n");
+
+ int nargs = lua_gettop(L);
+ IGUIElement* parent = NULL;
+ if(nargs == 4){
+ parent = (IGUIElement*)lua_touserdata(L,-1);
+ lua_pop(L,1);
+ }
+
+
+ const char* label_c = lua_tostring(L,-1);
+ const wchar_t* label_w = irr::core::stringw(label_c).c_str();
+ lua_pop(L,1);
+
+ long x,y,w,h;
+ popvector2i(L,&w,&h);
+ popvector2i(L,&x,&y);
+
+ rect<s32> dim = rect<s32>(x,y,x+w,y+h);
+ IGUIEnvironment* env = device->getGUIEnvironment();
+ IGUIButton* but = env->addButton(dim,parent,-2,label_w,L"");
+
+ lua_pushlightuserdata(L,but);//
+ luaL_getmetatable(L,"gui.iguibutton");
+ lua_setmetatable(L,-2);
+
+ registerguicallback(but,EGET_BUTTON_CLICKED,iguibuttonevent);
+ printf("Finished registering callback\n");
+
+ return 1;
}
static const luaL_reg iguibutton_f[] = {
- {"new", newiguibutton},
- {"gethandeler", guigethandeler},
- {"sethandeler", guisethandeler},
- {0,0},
+ {"new", newiguibutton},
+ {"gethandeler", guigethandeler},
+ {"sethandeler", guisethandeler},
+ {0,0},
};
static const luaL_reg iguibutton_m[] = {
- {"move", moveiguielement},
- {"settext", setiguitext},
- {"remove", removeiguielement},
- {0,0},
+ {"move", moveiguielement},
+ {"settext", setiguitext},
+ {"remove", removeiguielement},
+ {0,0},
};
-int iguibutton_register(lua_State* L, IrrlichtDevice* d){
- //device = d;
- tL = L;
+int iguibutton_register(lua_State* L){
+ tL = L;
- luaL_newmetatable(L, "gui.iguibutton");
+ luaL_newmetatable(L, "gui.iguibutton");//{m_iguibutton}
+ lua_newtable(L);//{m_iguibutton},{}
+ luaL_register(L,NULL,iguibutton_m);//{m_iguibutton},{}
+ lua_setfield(L,-2,"__index");//{m_iguibutton}
- luaL_register(L,"iguibutton",iguibutton_f);
+ lua_pop(L,1);
- lua_pushstring(L,"__index");
- lua_pushstring(L,"gethandeler");
- lua_gettable(L,-3);
- lua_settable(L,-4);
+ lua_getglobal(L,"gui");
+ lua_pushcfunction(L,newiguibutton);
+ lua_setfield(L,-2,"newbutton");
- lua_pushstring(L,"__newindex");
- lua_pushstring(L,"sethandeler");
- lua_gettable(L,-3);
- lua_settable(L,-4);
-
- luaL_register(L, NULL, iguibutton_m);
-
- return 1;
+ lua_pop(L,1);
}
diff --git a/src/client/lua_api/gui/iguibutton.hpp b/src/client/lua_api/gui/iguibutton.hpp
index 4059767..abccbf0 100644
--- a/src/client/lua_api/gui/iguibutton.hpp
+++ b/src/client/lua_api/gui/iguibutton.hpp
@@ -8,4 +8,4 @@ extern "C" {
}
#include <irrlicht.h>
-int iguibutton_register(lua_State* L, irr::IrrlichtDevice* d);
+int iguibutton_register(lua_State* L);