aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/gui/iguicheckbox.cpp
blob: 365b75de6a24c800175aa558ad9c45ead812b4a8 (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
109
110
111
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>
#include "../guiparts.hpp"
#include "iguielement.hpp"
#include <shared/lua_api/common.hpp>

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

extern IrrlichtDevice* device;

/***
Creates a new checkbox.
Creates a new checkbox that can be checked and unchecked. Checkboxes may have the
following fields, which they will call for callbacks:

	.onCheck(self)

It may additionally call any @{iguielement} callbacks

@function gui.newcheckbox()
@tparam rect dimensions The rectangle to place the box 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 have in the edit box
@tparam? iguielement parent The parent element of the edit box
@treturn iguieditbox The edit box element
*/
//new({startx,starty},{endx,endy},"checkbox_name"[,ud_parent])
int newiguicheckbox(lua_State* L){
	IGUIElement* par = NULL;
	int nargs = lua_gettop(L);
	if(nargs == 3){
		lua_getfield(L,-1,"guielement");//{{sx,sy},{ex,ey}},"label",{parent},ud_parent
		par = (IGUIElement*)lua_touserdata(L,-1);
		lua_pop(L,2);//{{sx,sy},{ex,ey}},"chekboxname"
	}
	const char* text = lua_tostring(L,-1);//{{sx,sy},{ex,ey}},"label"
	//int tlen = strlen(text);
	lua_pop(L,1);//{startx,starty},{endx,endy}
	long startx,starty,endx,endy;
	poprecti(L,&startx,&starty,&endx,&endy);
	irr::gui::IGUICheckBox* cb = device->getGUIEnvironment()->addCheckBox(false,core::rect<int>(startx,starty,endx,endy),par,-1,stringw(text).c_str());
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,cb);//*checkbox
	lua_setfield(L,-2,"guielement");//{checkbox}
	luaL_getmetatable(L,"gui.checkbox");//{checkbox},m{gui.checkbox}
	lua_setmetatable(L,-2);//{checkbox}
	
	setelementcallback(L,EGET_CHECKBOX_CHANGED,"onCheck");
	
	return 1;
}

/***
Is the checkbox checked.
@function iguicheckbox:ischecked()
@treturn boolean Is this checkbox checked?
*/
//ischecked(self)
int ischecked(lua_State *L){
	lua_getfield(L,-1,"guielement");
	IGUICheckBox *e = (IGUICheckBox*)lua_touserdata(L,-1);
	lua_pop(L,2);
	bool checked = e->isChecked();
	lua_pushboolean(L,checked ? 1 : 0);
	return 1;
}

/***
Set the state of the checkbox.
@function iguicheckbox:setchecked(set)
@tparam boolean set Should this checkbox be checked? (false to uncheck)
*/
//setchecked(self, checked)
int setchecked(lua_State *L){
	int should = lua_toboolean(L,-1);
	lua_pop(L,1);
	lua_getfield(L,-1,"guielement");
	IGUICheckBox *e = (IGUICheckBox*)lua_touserdata(L,-1);
	lua_pop(L,2);
	e->setChecked(should == 1);
	return 0;
}

static const luaL_reg iguicheckbox_m[] = {
	{"ischecked",		ischecked},
	{"setchecked",		setchecked},
	{0,0},
};

int iguicheckbox_register(lua_State* L){//

	luaL_newmetatable(L,"gui.checkbox");//m{gui.checkbox}
	lua_newtable(L);
	luaL_register(L,NULL,iguielement_m);
	luaL_register(L,NULL,iguicheckbox_m);
	lua_setfield(L,-2,"__index");
	lua_pop(L,1);//

	lua_getglobal(L,"gui");//{gui}
	lua_pushstring(L,"newcheckbox");//{gui},new(),"newcheckbox"
	lua_pushcfunction(L,newiguicheckbox);//{gui},new()
	lua_settable(L,-3);//{gui}
	lua_pop(L,1);
	return 0;
}