aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/gui/iguifiledialog.cpp
blob: 8e038c5b201d32226df1ff193bee927b28038823 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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 "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 dialog to open a file.
The file creation window may have the following fields set for callbacks:

	.onDirectorySelect(self)
	.onFileSelect(self)
	.onCanceled(self)

@function gui.newfileopendialog()
@tparam? string title 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 path The path to open the file dialog to
@tparam? iguielement parent The parent element of the button.
@tparam? boolean modal If other gui elements can be interacted with before this element is closed
@treturn iguifileopendialog The button element
*/
//gui.newfileopendialog(["title"][,"path"][,parent][,modal])
static int newfileopendialog(lua_State* L){
	printf("Creating openfiledialog\n");
	wchar_t *title = (wchar_t*)calloc(sizeof(wchar_t),1);// = L"File open dialog";
	io::path::char_type *path = 0;// = L"";
	bool modal = true;
	IGUIElement* parent = 0;

	int nargs = lua_gettop(L);
	if(nargs > 3){
		modal = lua_toboolean(L,-1) == 1;//"title","path",{parent},modal
		lua_pop(L,1);//"title","path",{parent}
	}
	if(nargs > 2){
		if(!lua_isnil(L,-1)){
			lua_getfield(L,-1,"guielement");//"title","path",{parent},ud_parent
			parent = (IGUIElement*)lua_touserdata(L,-1);//"title","path",{parent},ud_parent
			lua_pop(L,2);//"title","path"
		}else{
			parent = device->getGUIEnvironment()->getRootGUIElement();
		}
	}
	if(nargs > 1){
		const char *pathc = lua_tostring(L,-1);//"title","path"
		lua_pop(L,1);//"title"
		path = (io::path::char_type*)pathc;
	}
	if(nargs > 0){
		const char *titlec = lua_tostring(L,-1);
		lua_pop(L,1);//
		size_t titlecslen = strlen(titlec);
		title = (wchar_t*)malloc(sizeof(wchar_t) * (titlecslen + 1));
		mbstowcs(title,titlec,titlecslen);
		title[titlecslen] = L'\0';
	}
	printf("Got all arguments\n");
	IGUIEnvironment *env = device->getGUIEnvironment();
	printf("Got gui environment: %p\n",(void*)env);
	printf("Test: %ls\n",title);
	IGUIFileOpenDialog *but = env->addFileOpenDialog(title,modal,parent,-1,false,path);
	//IGUIFileOpenDialog *but = env->addFileOpenDialog(L"test",true,0,-1,false,"test");
	printf("Created file open dialog\n");
	printf("Added file open dialog\n");
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,but);//{},ud_iguibutton
	lua_setfield(L,-2,"guielement");//{guielement}
	luaL_getmetatable(L,"gui.iguifileopendialog");//{guielement},{m_iguibutton}
	lua_setmetatable(L,-2);//{guielement}

	printf("Created lua representation\n");
	setelementcallback(L,EGET_DIRECTORY_SELECTED,"onDirectorySelect");//
	setelementcallback(L,EGET_FILE_SELECTED,"onFileSelect");
	setelementcallback(L,EGET_FILE_CHOOSE_DIALOG_CANCELLED,"onCanceled");
	printf("Finished registering callback\n");

	free(title);
	printf("Freed everything\n");
	return 1;
}

/***
Returns the direcotry the client wanted to open.
Returns the name of the directory once the client has pressed "open".
@function iguiopenfiledialog:getdir()
@treturn string The name of the directory the opened file is in.
*/
//{fileopendialog} -> "dir"
int getdir(lua_State *L){
	lua_getfield(L,-1,"guielement");//{fileopendialog},ud_fileopendialog
	IGUIFileOpenDialog *f = (IGUIFileOpenDialog*)lua_touserdata(L,-1);
	lua_pop(L,2);
	const char *dname = f->getDirectoryName().c_str();
	lua_pushstring(L,dname);
	return 1;
}

/***
Returns the name of the file opened.
Returns the name of the file, without the file path.
@function iguiopenfiledialog:getname()
@treturn string the name of the file the user wants to open
*/
//{fileopendialog} -> "name"
int getname(lua_State *L){
	lua_getfield(L, -1, "guielement");//{fileopendialog},ud_fileopendialog
	IGUIFileOpenDialog *f = (IGUIFileOpenDialog*)lua_touserdata(L,-1);
	lua_pop(L,2);//
	const wchar_t *name_w = f->getFileName();
	size_t slen = wcslen(name_w);
	char name_c[slen + 1]; // + 1 for null
	wcstombs(name_c,name_w,slen);
	name_c[slen] = '\0';
	lua_pushstring(L,name_c);
	return 1;
}

static const luaL_reg iguifileopendialog_f[] = {
	{"new",             newfileopendialog},
	{0,0},
};

static const luaL_reg iguifileopendialog_m[] = {
	{"getdir",          getdir},
	{"getname",         getname},
	{0,0},
};

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

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

	lua_pop(L,1);

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

	lua_pop(L,1);
}