aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/gui
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2016-11-01 00:28:16 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2016-11-01 00:28:16 -0400
commitd2b36e6c65ec8126c0ebc96adb7e011e78a5eacc (patch)
tree9e005502ea2c125c90b5011f573f381f84ade0ef /src/client/lua_api/gui
downloadbrokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.tar.gz
brokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.tar.bz2
brokengine-d2b36e6c65ec8126c0ebc96adb7e011e78a5eacc.zip
Initial commit
Diffstat (limited to 'src/client/lua_api/gui')
-rw-r--r--src/client/lua_api/gui/iguibutton.cpp163
-rw-r--r--src/client/lua_api/gui/iguibutton.hpp11
-rw-r--r--src/client/lua_api/gui/iguielement.cpp93
-rw-r--r--src/client/lua_api/gui/iguielement.hpp15
-rw-r--r--src/client/lua_api/gui/iguilabel.cpp128
-rw-r--r--src/client/lua_api/gui/iguilabel.hpp11
6 files changed, 421 insertions, 0 deletions
diff --git a/src/client/lua_api/gui/iguibutton.cpp b/src/client/lua_api/gui/iguibutton.cpp
new file mode 100644
index 0000000..6c2fc5a
--- /dev/null
+++ b/src/client/lua_api/gui/iguibutton.cpp
@@ -0,0 +1,163 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <string>
+#include <functional>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+#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<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;
+}
+
+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;
+}
diff --git a/src/client/lua_api/gui/iguibutton.hpp b/src/client/lua_api/gui/iguibutton.hpp
new file mode 100644
index 0000000..4059767
--- /dev/null
+++ b/src/client/lua_api/gui/iguibutton.hpp
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+int iguibutton_register(lua_State* L, irr::IrrlichtDevice* d);
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;
+}
diff --git a/src/client/lua_api/gui/iguielement.hpp b/src/client/lua_api/gui/iguielement.hpp
new file mode 100644
index 0000000..5756a34
--- /dev/null
+++ b/src/client/lua_api/gui/iguielement.hpp
@@ -0,0 +1,15 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+int moveiguielement(lua_State* L);
+int setiguitext(lua_State* L);
+int removeiguielement(lua_State* L);
+int guigethandeler(lua_State* L);
+int guisethandeler(lua_State* L);
diff --git a/src/client/lua_api/gui/iguilabel.cpp b/src/client/lua_api/gui/iguilabel.cpp
new file mode 100644
index 0000000..da720f9
--- /dev/null
+++ b/src/client/lua_api/gui/iguilabel.cpp
@@ -0,0 +1,128 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <functional>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+#include "../guiparts.hpp"
+#include "iguielement.hpp"
+#include "../../callbackhandeler.hpp"
+
+using namespace irr;
+using namespace gui;
+
+//IrrlichtDevice* guidevice;
+
+static LIGUIElement* checkiguilabel(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"gui.iguilabel");
+ luaL_argcheck(L,ud != NULL, index, "'gui.iguilabel' expected");
+ return (LIGUIElement*) ud;
+}
+
+static LIGUIElement* checkiguilabel(lua_State* L){
+ return checkiguilabel(L,1);
+}
+
+static void iguilabelevent(irr::SEvent e){
+ printf("Oh no! an iguilabel generated an event!");
+}
+
+static int newiguilabel(lua_State* L){
+ printf("Createing label!\n");
+ int nargs = lua_gettop(L);
+ //The position of the text
+ 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* label;
+ const char* labelopt = luaL_optstring(L,5,"Label");
+ int bls = strlen(labelopt);
+ label = (wchar_t*)malloc(sizeof(wchar_t)*(bls));
+ mbstowcs(label,labelopt,bls);
+ printf("Got the string option\n");
+
+ //If the element has a parrent
+ int parent = 0;
+ if(nargs >= 6){
+ parent = luaL_optint(L,6,0);
+ printf("got the parrent\n");
+ }
+
+
+ //Create the button
+ IGUIEnvironment* env = guidevice->getGUIEnvironment();
+ //printf("Createing button with data %d %d %d %d, %p, %d, %s, %s\n", startx,starty,endx,endy,guielements[parent],gui_elenum,button_label,button_tooltip);
+ //Create the label
+ IGUIStaticText* llabel = (IGUIStaticText*) env->addStaticText(label,core::rect<s32>(startx,starty,endx,endy),false,false, guielements[parent], gui_elenum++, false);
+ printf("Created the button\n");
+
+ //Register it's callback
+ registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent);
+
+ //Create it's lua representation
+ LIGUIElement* ltext = (LIGUIElement*)lua_newuserdata(L, sizeof(LIGUIElement));
+ int tref = luaL_ref(L,LUA_REGISTRYINDEX);
+ iguielements[llabel] = 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.iguilabel");
+ lua_setmetatable(L, -2);
+
+ //Create the struct
+ ltext->e = llabel;
+ ltext->funcmap = hashmap_new();
+ ltext->type = "iguilabel";
+
+ //Free up anything made in this function
+ free(label);
+
+ //Put it on top and return it
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
+ return 1;
+}
+
+static const luaL_reg iguilabel_f[] = {
+ {"new", newiguilabel},
+ {"gethandeler", guigethandeler},
+ {"sethandeler", guisethandeler},
+ {0,0},
+};
+
+static const luaL_reg iguilabel_m[] = {
+ {"move", moveiguielement},
+ {"settext", setiguitext},
+ {"remove", removeiguielement},
+ {0, 0},
+};
+
+int iguilabel_register(lua_State* L, IrrlichtDevice* d){
+
+ luaL_newmetatable(L, "gui.iguilabel");
+
+ luaL_register(L,"iguilabel",iguilabel_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, iguilabel_m);
+
+ return 1;
+}
diff --git a/src/client/lua_api/gui/iguilabel.hpp b/src/client/lua_api/gui/iguilabel.hpp
new file mode 100644
index 0000000..c779c70
--- /dev/null
+++ b/src/client/lua_api/gui/iguilabel.hpp
@@ -0,0 +1,11 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+int iguilabel_register(lua_State* L, irr::IrrlichtDevice* d);