aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/gui/iguispinbox.cpp
blob: d34d0fbd1f62fd1fc206ba9deba2f87c8a61b66d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
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>

using namespace irr;
using namespace core;
using namespace gui;

extern IrrlichtDevice* device;

/***
Creates a new spinbox 
Creates a gui element that has up and down arrows to control the value, and is editable by the user.
@function gui.newspinbox()
@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? string default_text The default text to display in the spinbox.
@tparam? iguielement parent The parent element of the button.
@tparam? boolean border Display a border around the spinbox
@treturn iguibutton The button element
*/
//gui.newspinbox({{sx,sy},{ex,ey}}[,"text"][,parent][,border])
static int newiguispinbox(lua_State* L){
	printf("Createing gui spinbox!\n");
	int nargs = lua_gettop(L);
	printf("Nargs was %d\n",nargs);

	IGUIElement* parent = NULL;
	const char *label_c = "";
	int border = 1;
	if(nargs > 3){
		border = lua_toboolean(L,-1);
		lua_pop(L,1);
	}
	if(nargs > 2){
		lua_getfield(L,-1,"guielement");
		parent = (IGUIElement*)lua_touserdata(L,-1);
		lua_pop(L,2);//{{sx,sy},{ex,ey}},"text"
	}
	if(nargs > 1){
		label_c = lua_tostring(L,-1);
		lua_pop(L,1);
	}
	
	size_t labellen = strlen(label_c);
	wchar_t label_w[labellen + 1]; //+1 for null
	mbstowcs(label_w,label_c,labellen);
	label_w[labellen] = L'\0';
	//const wchar_t* label_w = irr::core::stringw(label_c).c_str();//{{sx,sy},{ex,ey}},"text"
	//lua_pop(L,1);//{{sx,sy},{ex,ey}}
	printf("Created title\n");
	long sx,sy,ex,ey;
	poprecti(L,&sx,&sy,&ex,&ey);//

	printf("Got coords\n");
	rect<s32> dim = rect<s32>(sx,sy,ex,ey);
	IGUIEnvironment* env = device->getGUIEnvironment();
	IGUISpinBox* but = env->addSpinBox(label_w,dim,border,parent,-1);

	printf("Added spinbox\n");
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,but);//{},ud_iguibutton
	lua_setfield(L,-2,"guielement");//{guielement}
	luaL_getmetatable(L,"gui.iguispinbox");//{guielement},{m_iguibutton}
	lua_setmetatable(L,-2);//{guielement}

	printf("Created lua representation\n");
	setelementcallback(L,EGET_SPINBOX_CHANGED,"onChanged");//
	printf("Finished registering callback\n");
	
	return 1;
}

static const luaL_Reg iguispinbox_f[] = {
	{"new",             newiguispinbox},
	{0,0},
};

static const luaL_Reg iguispinbox_m[] = {
	{0,0},
};

void iguispinbox_register(lua_State* L){
	tL = L;

	luaL_newmetatable(L, "gui.iguispinbox");//{m_iguibutton}
	lua_newtable(L);//{m_iguibutton},{}
	luaL_register(L,NULL,iguielement_m);
	luaL_register(L,NULL,iguispinbox_m);//{m_iguibutton},{}
	lua_setfield(L,-2,"__index");//{m_iguibutton}

	lua_pop(L,1);

	lua_getglobal(L,"gui");
	lua_pushcfunction(L,newiguispinbox);
	lua_setfield(L,-2,"newspinbox");

	lua_pop(L,1);
}