aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/gui/iguielement.cpp
blob: fa3d48010dbea935005b616048d86f4063483747 (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
112
113
114
115
/*This file defines some things that all igui stuff can do*/
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}


#include <shared/lua_api/common.hpp>
#include <irrlicht.h>
#include "../guiparts.hpp"
#include <client/callbackhandeler.hpp>

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

//move({element},{x,y}) -> nil
int moveiguielement(lua_State* L){
	//printf("Got call to move element\n");
	long x,y;
	popvector2i(L,&x,&y); //{element}
	//printf("I want to move to %d %d\n",x,y);
	lua_getfield(L,-1,"guielement");//{element},*element
	IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);
	//printf("Found element to move: %p\n",el);
	lua_pop(L,2);//

	el->move(position2d<s32>(x,y));
	el->updateAbsolutePosition();
	return 0;
}

//getabsrect({element})-> {{sx,sy},{ex,ey}}
int getiguiclippingrect(lua_State* L){
	printf("Getting iguiclipping elemnt\n");
	lua_getfield(L,-1,"guielement");
	IGUIElement *el = (IGUIElement*)lua_touserdata(L,-1);//{element},ud_element
	lua_pop(L,2);
	core::rect<s32> rect = el->getAbsoluteClippingRect();
	pushrecti(
		L,
		rect.UpperLeftCorner.X,
		rect.UpperLeftCorner.Y,
		rect.LowerRightCorner.X,
		rect.LowerRightCorner.Y
	);
	return 1;
}

//setText({guielement},"text") :: nil
int setiguitext(lua_State* L){
	const char* text = lua_tostring(L, -1);
	lua_pop(L,1);//{guielement}
	lua_getfield(L,-1,"guielement");//{guielement},ud_iguielement
	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);
	mbstowcs(wtext,text,textlen);
	el->setText(wtext);
	free(wtext);
	return 0;
}

//remove({self})
int removeiguielement(lua_State* L){
	lua_getfield(L,-1,"guielement");
	IGUIElement *ele = (IGUIElement*)lua_touserdata(L,-1);
	ele->remove();
	lua_pop(L,2);
	return 0;
}


class guicallback{
private:
	int luaitem;
	const char* funcname;
	lua_State* L;
public:
	guicallback(lua_State* L, int lf, const char* fn){
		this->luaitem = lf;
		this->L = L;
		this->funcname = fn;
	}
	bool operator () (SEvent e) {
		lua_rawgeti(L,LUA_REGISTRYINDEX,this->luaitem);//{guielement}
		lua_getfield(L,-1,this->funcname);//{guielement},func()
		if(!lua_isnil(L,-1)){
			lua_rawgeti(L,LUA_REGISTRYINDEX,this->luaitem);//{guielement},func(),{guielement}
			lua_call(L,1,0);//{iguielement}
			lua_pop(L,1);//{}
			return true;
		}else{
			lua_pop(L,2);//{}
			return false;
		}
	}
};

//{guielement}
//popelementcallback(lua_State* L, gui::EGUI_EVENT_TYPE, char*)
void setelementcallback(lua_State* L, gui::EGUI_EVENT_TYPE et, const char* funcname){
	registerguielement(L,et,funcname);
}

//ud_iguielement
int guigetid(lua_State* L){
	IGUIElement* el = (IGUIElement*)lua_touserdata(L,-1);
	lua_pop(L,1);
	int id = el->getID();
	lua_pushnumber(L,id);
	return 1;
}