diff options
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/callbackhandeler.cpp | 39 | ||||
| -rw-r--r-- | src/client/lua_api/gui/iguicombobox.cpp | 131 | ||||
| -rw-r--r-- | src/client/lua_api/gui/iguicombobox.hpp | 11 | ||||
| -rw-r--r-- | src/client/lua_api/gui/iguielement.cpp | 20 | ||||
| -rw-r--r-- | src/client/lua_api/gui/iguilabel.cpp | 1 | ||||
| -rw-r--r-- | src/client/lua_api/gui/iguiwindow.cpp | 2 | ||||
| -rw-r--r-- | src/client/lua_api/load_game.cpp | 13 | ||||
| -rw-r--r-- | src/client/lua_api/load_gui.cpp | 6 | ||||
| -rw-r--r-- | src/client/lua_api/load_video.cpp | 8 | ||||
| -rw-r--r-- | src/client/lua_api/video/itexture.cpp | 1 |
10 files changed, 220 insertions, 12 deletions
diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index 133635c..5d51ba6 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -17,7 +17,9 @@ using namespace gui; using namespace std; extern lua_State* L; - +/*** +@module GAME +*/ std::map<IGUIElement*, int> guielements; //For basic events @@ -50,7 +52,8 @@ void callMouse(lua_State* L, const char* funcname, double x, double y, double ev } lua_pop(L,2); }else{ - lua_pop(L,3); + //{GAME},errfunc,nil + lua_pop(L,3);// } } @@ -106,15 +109,20 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ case EGET_TREEVIEW_NODE_COLLAPSE: fieldname = "onNodeCollapse"; break; case EGET_COUNT: break; } - lua_getfield(L,-1,fieldname);//{guielement},func() + printf("About to push error func"); + pusherrorfunc(L);//{guielement},errfunc() + lua_getfield(L,-2,fieldname);//{guielement},errfunc(),func() if(lua_isnil(L,-1)){ - lua_pop(L,2); + printf("Element did not have a function %s, returning\n",fieldname); + lua_pop(L,3);// return false; } - lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement},func(),{guielement} - lua_call(L,1,1);//{guielement} + lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement},errfunc(),func(),{guielement} + printf("About to pcall in callback.cpp!\n"); + lua_pcall(L,1,1,-3);//{guielement} int n = lua_gettop(L); if(n > 1){ + printf("Got an argument back!\n"); int ans = lua_toboolean(L,-1); lua_pop(L,n); return ans; @@ -125,7 +133,15 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ break; case EET_MOUSE_INPUT_EVENT:{ //printf("Mouse event - X: %d Y: %d\n",se.X, se.Y); - +/*** +(Callback)Detects mouse movement. +Detects when the mouse moves across the game. +@function GAME.onMouseMove(x,y,event) +@tparam number x The key that the user pressed down +@tparam boolean down Was the key pressed down? +@tparam boolean pressed_ctrl Was control pressed? +@tparam boolean pressed_shift Was shift pressed? +*/ switch(se.Event){ case EMIE_MOUSE_MOVED:{ callMouse(L,"onMouseMove",se.X,se.Y,se.Event); @@ -166,6 +182,15 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ return false; } break; +/*** +(Callback)Detects key presses. +Detects any key presses from the game. +@function GAME.onKeyDown(key,down,pressed_ctrl,pressed_shift) +@tparam number key The key that the user pressed down +@tparam boolean down Was the key pressed down? +@tparam boolean pressed_ctrl Was control pressed? +@tparam boolean pressed_shift Was shift pressed? +*/ case EET_KEY_INPUT_EVENT:{ //printf("Got input event\n"); SEvent::SKeyInput se = e.KeyInput; diff --git a/src/client/lua_api/gui/iguicombobox.cpp b/src/client/lua_api/gui/iguicombobox.cpp new file mode 100644 index 0000000..66b19d2 --- /dev/null +++ b/src/client/lua_api/gui/iguicombobox.cpp @@ -0,0 +1,131 @@ +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <irrlicht.h> +#include "../guiparts.hpp" +#include "iguielement.hpp" +#include "client/callbackhandeler.hpp" +#include <shared/util/hashmap.hpp> +#include <shared/lua_api/common.hpp> +#include <client/lua_api/gui/iguicombobox.hpp> + +/*** +@module gui +*/ +using namespace irr; +using namespace core; +using namespace gui; + +extern IrrlichtDevice* device; + +/*** +Creates a new combo box. +Buttons may have the following fields set for callbacks: +`.onChange(self)` +@function newcombobox() +@tparam rect dimensions The rectangle to place the button at. If the box has a parent, +it is offset from the upper-left of the parent element. +@tparam ?iguielement parent The parent element of the button. +@treturn iguicombobox The combo box element +*/ +//gui.newcombobox({{sx,sy},{ex,ey}}[,parent]) +static int newiguicombobox(lua_State* L){ + //printf("Createing gui button!\n"); + + int nargs = lua_gettop(L); + IGUIElement* parent = NULL; + if(nargs == 2){ + lua_getfield(L,-1,"guielement"); + parent = (IGUIElement*)lua_touserdata(L,-1); + lua_pop(L,2);//{{sx,sy},{ex,ey}} + } + + long sx,sy,ex,ey; + poprecti(L,&sx,&sy,&ex,&ey);// + + rect<s32> dim = rect<s32>(sx,sy,ex,ey); + IGUIEnvironment* env = device->getGUIEnvironment(); + IGUIComboBox* but = env->addComboBox(dim,parent,-1); + + lua_newtable(L);//{} + lua_pushlightuserdata(L,but);//{},ud_iguicombobox + lua_setfield(L,-2,"guielement");//{guielement} + luaL_getmetatable(L,"gui.iguicombobox");//{guielement},{m_iguicombobox} + lua_setmetatable(L,-2);//{guielement} + + setelementcallback(L,EGET_COMBO_BOX_CHANGED,"onChange");// + + return 1; +} + +//{combobox}.addItem(self,text,id) +int additem(lua_State *L){ + int id = lua_tonumber(L,-1); + lua_pop(L,1);//self,text + const char *name_c = lua_tostring(L,-1); + size_t name_c_len = strlen(name_c); + printf("adding item to combo box: %s\n",name_c); + wchar_t name_w[name_c_len + 1]; + mbstowcs(name_w,name_c,name_c_len); + name_w[name_c_len] = L'\0'; + lua_pop(L,1);//self + lua_getfield(L,-1,"guielement");//self,ud_guielement + IGUIComboBox *box = (IGUIComboBox*)lua_touserdata(L,-1); + box->addItem(name_w,id); + lua_pop(L,2);// + return 0; +} + +// {combobox}.getselected(self) +int getselected(lua_State *L){ + lua_getfield(L,-1,"guielement");//self,ud_guielement + IGUIComboBox *box = (IGUIComboBox*)lua_touserdata(L,-1); + lua_pop(L,2);// + s32 sel = box->getSelected(); + lua_pushnumber(L,sel);//num_selected + return 1; +} + +//{combobox}.removeitem(self,id) +int removeitem(lua_State *L){ + int idx = lua_tonumber(L,-1);//self,id + lua_pop(L,1);//self + lua_getfield(L,-1,"guielement");//self,ud_guielement + IGUIComboBox *box = (IGUIComboBox*)lua_touserdata(L,-1); + lua_pop(L,2);// + box->removeItem(idx); + return 0; +} + +static const luaL_reg iguicombobox_f[] = { + {"new", newiguicombobox}, + {0,0}, +}; + +static const luaL_reg iguicombobox_m[] = { + {"addItem", additem}, + {"getSelected", getselected}, + {"removeItem", removeitem}, + {0,0}, +}; + +void iguicombobox_register(lua_State* L){ + tL = L; + + luaL_newmetatable(L, "gui.iguicombobox");//{m_iguibutton} + lua_newtable(L);//{m_iguibutton},{} + luaL_register(L,NULL,iguielement_m); + luaL_register(L,NULL,iguicombobox_m);//{m_iguibutton},{} + lua_setfield(L,-2,"__index");//{m_iguibutton} + + lua_pop(L,1); + + lua_getglobal(L,"gui"); + //luaL_register(L,NULL,iguicombobox_f) + lua_pushcfunction(L,newiguicombobox); + lua_setfield(L,-2,"newcombobox"); + + lua_pop(L,1); +} diff --git a/src/client/lua_api/gui/iguicombobox.hpp b/src/client/lua_api/gui/iguicombobox.hpp new file mode 100644 index 0000000..ff6ba97 --- /dev/null +++ b/src/client/lua_api/gui/iguicombobox.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> + +void iguicombobox_register(lua_State* L); diff --git a/src/client/lua_api/gui/iguielement.cpp b/src/client/lua_api/gui/iguielement.cpp index 5c51c15..af9d234 100644 --- a/src/client/lua_api/gui/iguielement.cpp +++ b/src/client/lua_api/gui/iguielement.cpp @@ -27,6 +27,7 @@ using namespace irr; using namespace core; using namespace gui; +extern IrrlichtDevice *device; /*** Move an element (by an offset) from it's current position @function guielement:move() @@ -145,8 +146,9 @@ int setiguitext(lua_State* L){ IGUIElement* el = (IGUIElement*)lua_touserdata(L,-1);//{guielement},ud_iguielement lua_pop(L,2); int textlen = strlen(text); - wchar_t* wtext = (wchar_t*)malloc(sizeof(wchar_t)*textlen); + wchar_t* wtext = (wchar_t*)malloc(sizeof(wchar_t)*textlen + 1);//+1 for null? mbstowcs(wtext,text,textlen); + wtext[textlen] = '\0'; el->setText(wtext); free(wtext); return 0; @@ -198,6 +200,21 @@ int removeiguielement(lua_State* L){ return 0; } +/*** +Focuses a gui element. +Will cause a ELEMENT_FOCUS_LOST followed by an ELEMENT_FOCUSED event. +If either events are trapped/returned from, then the focus will not change. +@function iguielement:focus() +*/ +//focus({self}) +int focus(lua_State *L){ + lua_getfield(L,-1,"guielement"); + IGUIElement *ele = (IGUIElement*)lua_touserdata(L,-1); + device->getGUIEnvironment()->setFocus(ele); + lua_pop(L,2); + return 0; +} + class guicallback{ private: @@ -250,5 +267,6 @@ extern const luaL_reg iguielement_m[] = { {"settext", setiguitext}, {"gettext", getiguitext}, {"remove", removeiguielement}, + {"focus", focus}, {NULL, NULL} }; diff --git a/src/client/lua_api/gui/iguilabel.cpp b/src/client/lua_api/gui/iguilabel.cpp index 336f81c..529d680 100644 --- a/src/client/lua_api/gui/iguilabel.cpp +++ b/src/client/lua_api/gui/iguilabel.cpp @@ -66,6 +66,7 @@ static int newiguilabel(lua_State* L){ -1, false ); + statictext->setWordWrap(true); lua_newtable(L); //{} lua_pushlightuserdata(L,statictext);//{},*statictext diff --git a/src/client/lua_api/gui/iguiwindow.cpp b/src/client/lua_api/gui/iguiwindow.cpp index fde8cf1..236fd5d 100644 --- a/src/client/lua_api/gui/iguiwindow.cpp +++ b/src/client/lua_api/gui/iguiwindow.cpp @@ -30,6 +30,8 @@ Creates a new gui window . @tparam string title_text The text to show in the title bar of the window @tparam ?iguielement parent The parent element of the window. @treturn iguiwindow The window element + +In return to the usual gui element calls, IGUI window may call a `onClose()` callback. */ //new({{sx,sy},{ex,ey}},"title"[,{guielement=parent}]) :: {guielement} static int newiguiwindow(lua_State* L){ diff --git a/src/client/lua_api/load_game.cpp b/src/client/lua_api/load_game.cpp index 469b219..8a6ad73 100644 --- a/src/client/lua_api/load_game.cpp +++ b/src/client/lua_api/load_game.cpp @@ -9,6 +9,7 @@ extern "C" { } #include <irrlicht.h> +#include <shared/lua_api/common.hpp> #include "gui/iguibutton.hpp" #include "gui/iguilabel.hpp" #include "../callbackhandeler.hpp" @@ -18,6 +19,7 @@ using namespace irr; using namespace gui; using namespace core; +extern video::SColor background; extern IrrlichtDevice* device; //exit() @@ -25,6 +27,12 @@ int exit_game(lua_State *L){ device->closeDevice(); return 0; } +int setbackgroundcolor(lua_State* L){ + long r,g,b; + popvector3i(L,&r,&g,&b); + background = video::SColor(255,r,g,b); + return 0; +} void load_gamefuncs(lua_State* L){ lua_newtable(L); @@ -33,5 +41,10 @@ void load_gamefuncs(lua_State* L){ lua_getglobal(L,"GAME"); lua_pushcfunction(L,exit_game); lua_setfield(L,-2,"exit"); + + lua_pushcfunction(L,setbackgroundcolor);//{game},setbackgroundcolor() + lua_setfield(L,-2,"setbackgroundcolor");//{game} + lua_pop(L,1); + lua_pop(L,1); } diff --git a/src/client/lua_api/load_gui.cpp b/src/client/lua_api/load_gui.cpp index 729633a..845661a 100644 --- a/src/client/lua_api/load_gui.cpp +++ b/src/client/lua_api/load_gui.cpp @@ -24,6 +24,7 @@ extern "C" { #include "gui/iguifiledialog.hpp" #include "gui/iguispinbox.hpp" #include "gui/iguitreeview.hpp" +#include "gui/iguicombobox.hpp" #include "../callbackhandeler.hpp" #include "guiparts.hpp" @@ -57,6 +58,7 @@ void load_guifuncs(lua_State* L){ tL = L; guidevice = device; + IGUIFont *font = device->getGUIEnvironment()->getFont("../data/res/font.xml"); //Set the transparency of new igui windows IGUISkin *skin = device->getGUIEnvironment()->getSkin(); for(int i = 0; i < EGDC_COUNT; i++){ @@ -64,6 +66,9 @@ void load_guifuncs(lua_State* L){ col.setAlpha(255); skin->setColor((EGUI_DEFAULT_COLOR)i, col); } + for(int i = 0; i < EGDF_COUNT; i++){ + skin->setFont(font,(EGUI_DEFAULT_FONT)i); + } //Various enums lua_newtable(L); @@ -78,6 +83,7 @@ void load_guifuncs(lua_State* L){ iguispinbox_register(L); iguitreeview_register(L); iguieditbox_register(L); + iguicombobox_register(L); lua_pushcfunction(L,screenwidth); lua_setglobal(L,"scrw"); diff --git a/src/client/lua_api/load_video.cpp b/src/client/lua_api/load_video.cpp index e0c7557..1266c4d 100644 --- a/src/client/lua_api/load_video.cpp +++ b/src/client/lua_api/load_video.cpp @@ -23,9 +23,9 @@ extern IVideoDriver* driver; //{texture},{x,y},{sourcerect},{color},use_alpha int draw2dimage(lua_State* L){ int nargs = lua_gettop(L); - //printf("Drawing a 2d image\n"); + printf("Drawing a 2d image\n"); if(nargs == 2){ - //printf("2-argument version\n"); + printf("2-argument version\n"); long x,y; popvector2i(L,&x,&y); lua_getfield(L,-1,"texture"); @@ -33,10 +33,10 @@ int draw2dimage(lua_State* L){ lua_pop(L,2); driver->draw2DImage(tex,position2d<s32>(x,y)); }else if(nargs == 5){ - //printf("5-argument version\n"); + printf("5-argument version\n"); int usealpha = lua_toboolean(L,-1); lua_pop(L,1); - //printf("Got usealpha: %d\n",usealpha); + printf("Got usealpha: %d\n",usealpha); long r,g,b,a; popvector4i(L,&r,&g,&b,&a); long ssx,ssy,sex,sey; diff --git a/src/client/lua_api/video/itexture.cpp b/src/client/lua_api/video/itexture.cpp index 587b8ee..c016d41 100644 --- a/src/client/lua_api/video/itexture.cpp +++ b/src/client/lua_api/video/itexture.cpp @@ -39,6 +39,7 @@ int newitexture(lua_State* L){ //newtexturefromfile(string file_name) -> {texture} int newitexturefromfile(lua_State* L){ + printf("New texture from file"); const char* strpath = lua_tostring(L,-1); lua_pop(L,1); ITexture* tex = driver->getTexture(strpath); |
