27 #include <unordered_map>
36 #include "eng3d/state.hpp"
37 #include "eng3d/log.hpp"
38 #include "eng3d/ui/ui.hpp"
39 #include "eng3d/ui/widget.hpp"
40 #include "eng3d/ui/components.hpp"
41 #include "eng3d/luavm.hpp"
45 this->
state = luaL_newstate();
46 luaL_openlibs(this->
state);
49 lua_register(this->
state,
"_", [](lua_State*
L) {
50 std::string msgid = luaL_checkstring(
L, 1);
51 lua_pushstring(
L, msgid.c_str());
55 lua_register(this->
state,
"ui_new_button", Eng3D::LuaVM::ui_new_button);
56 lua_register(this->
state,
"ui_new_div", Eng3D::LuaVM::ui_new_div);
57 lua_register(this->
state,
"ui_new_group", Eng3D::LuaVM::ui_new_group);
58 lua_register(this->
state,
"ui_new_image", Eng3D::LuaVM::ui_new_image);
59 lua_register(this->
state,
"ui_new_checkbox", Eng3D::LuaVM::ui_new_checkbox);
60 lua_register(this->
state,
"ui_set_checkbox_value", Eng3D::LuaVM::ui_set_checkbox_value);
61 lua_register(this->
state,
"ui_get_checkbox_value", Eng3D::LuaVM::ui_get_checkbox_value);
62 lua_register(this->
state,
"ui_new_slider", Eng3D::LuaVM::ui_new_slider);
63 lua_register(this->
state,
"ui_get_slider_value", Eng3D::LuaVM::ui_get_slider_value);
64 lua_register(this->
state,
"ui_set_slider_value", Eng3D::LuaVM::ui_set_slider_value);
65 lua_register(this->
state,
"ui_new_label", Eng3D::LuaVM::ui_new_label);
66 lua_register(this->
state,
"ui_new_window", Eng3D::LuaVM::ui_new_window);
67 lua_register(this->
state,
"ui_get_image", Eng3D::LuaVM::ui_get_image);
68 lua_register(this->
state,
"ui_set_image", Eng3D::LuaVM::ui_set_image);
69 lua_register(this->
state,
"ui_set_scroll", Eng3D::LuaVM::ui_set_scroll);
70 lua_register(this->
state,
"ui_set_text", Eng3D::LuaVM::ui_set_text);
71 lua_register(this->
state,
"ui_set_on_click", Eng3D::LuaVM::ui_set_on_click);
72 lua_register(this->
state,
"ui_set_window_on_click_close_btn", Eng3D::LuaVM::ui_set_window_on_click_close_btn);
73 lua_register(this->
state,
"ui_get_widget", Eng3D::LuaVM::ui_get_widget);
74 lua_register(this->
state,
"ui_widget_kill", Eng3D::LuaVM::ui_widget_kill);
75 lua_register(this->
state,
"ui_widget_set_tooltip", Eng3D::LuaVM::ui_widget_set_tooltip);
76 lua_register(this->
state,
"UI_RegisterCallback", Eng3D::LuaVM::ui_register_callback);
77 lua_register(this->
state,
"ui_widget_set_flex", Eng3D::LuaVM::ui_widget_set_flex);
86 static inline int get_unique_id() {
91 int Eng3D::LuaVM::ui_new_button(lua_State*
L) {
92 int x = luaL_checkinteger(
L, 1);
93 int y = luaL_checkinteger(
L, 2);
94 int w = luaL_checkinteger(
L, 3);
95 int h = luaL_checkinteger(
L, 4);
96 int parent_ref = luaL_checkinteger(
L, 5);
99 const auto widget_id = get_unique_id();
101 lua_pushinteger(
L, widget_id);
105 int Eng3D::LuaVM::ui_new_image(lua_State*
L) {
106 int x = luaL_checkinteger(
L, 1);
107 int y = luaL_checkinteger(
L, 2);
108 int w = luaL_checkinteger(
L, 3);
109 int h = luaL_checkinteger(
L, 4);
110 int parent_ref = luaL_checkinteger(
L, 5);
113 const auto widget_id = get_unique_id();
115 lua_pushinteger(
L, widget_id);
119 int Eng3D::LuaVM::ui_new_group(lua_State*
L) {
120 int x = luaL_checkinteger(
L, 1);
121 int y = luaL_checkinteger(
L, 2);
122 int w = luaL_checkinteger(
L, 3);
123 int h = luaL_checkinteger(
L, 4);
124 int parent_ref = luaL_checkinteger(
L, 5);
127 const auto widget_id = get_unique_id();
129 lua_pushinteger(
L, widget_id);
133 int Eng3D::LuaVM::ui_new_div(lua_State*
L) {
134 int x = luaL_checkinteger(
L, 1);
135 int y = luaL_checkinteger(
L, 2);
136 int w = luaL_checkinteger(
L, 3);
137 int h = luaL_checkinteger(
L, 4);
138 int parent_ref = luaL_checkinteger(
L, 5);
141 const auto widget_id = get_unique_id();
143 lua_pushinteger(
L, widget_id);
147 int Eng3D::LuaVM::ui_new_window(lua_State*
L) {
148 int x = luaL_checkinteger(
L, 1);
149 int y = luaL_checkinteger(
L, 2);
150 int w = luaL_checkinteger(
L, 3);
151 int h = luaL_checkinteger(
L, 4);
152 int parent_ref = luaL_checkinteger(
L, 5);
155 const auto widget_id = get_unique_id();
157 lua_pushinteger(
L, widget_id);
161 int Eng3D::LuaVM::ui_new_checkbox(lua_State*
L) {
162 int x = luaL_checkinteger(
L, 1);
163 int y = luaL_checkinteger(
L, 2);
164 int w = luaL_checkinteger(
L, 3);
165 int h = luaL_checkinteger(
L, 4);
166 int parent_ref = luaL_checkinteger(
L, 5);
169 const auto widget_id = get_unique_id();
171 lua_pushinteger(
L, widget_id);
175 int Eng3D::LuaVM::ui_set_checkbox_value(lua_State*
L) {
181 int Eng3D::LuaVM::ui_get_checkbox_value(lua_State*
L) {
187 int Eng3D::LuaVM::ui_new_slider(lua_State*
L) {
188 int x = luaL_checkinteger(
L, 1);
189 int y = luaL_checkinteger(
L, 2);
190 int w = luaL_checkinteger(
L, 3);
191 int h = luaL_checkinteger(
L, 4);
192 int parent_ref = luaL_checkinteger(
L, 5);
195 const auto widget_id = get_unique_id();
197 lua_pushinteger(
L, widget_id);
201 int Eng3D::LuaVM::ui_get_slider_value(lua_State*
L) {
207 int Eng3D::LuaVM::ui_set_slider_value(lua_State*
L) {
213 int Eng3D::LuaVM::ui_new_label(lua_State*
L) {
214 int x = luaL_checkinteger(
L, 1);
215 int y = luaL_checkinteger(
L, 2);
216 int parent_ref = luaL_checkinteger(
L, 3);
219 const auto widget_id = get_unique_id();
221 lua_pushinteger(
L, widget_id);
225 int Eng3D::LuaVM::ui_set_text(lua_State*
L) {
227 widget->
set_text(luaL_checkstring(
L, 2));
231 int Eng3D::LuaVM::ui_get_image(lua_State*
L) {
232 const std::string path = luaL_checkstring(
L, 1);
235 const auto widget_id = get_unique_id();
236 lua_textures[widget_id] = s.tex_man.load(s.package_man.get_unique(path));
237 lua_pushinteger(
L, widget_id);
241 int Eng3D::LuaVM::ui_set_image(lua_State*
L) {
248 int Eng3D::LuaVM::ui_set_scroll(lua_State*
L) {
254 int Eng3D::LuaVM::ui_set_on_click(lua_State*
L) {
259 lua_getglobal(
L,
"UI_DriverCallOnClick");
263 return e.second == &w;
265 lua_pushinteger(
L, it->first);
267 const std::string err_msg = lua_tostring(
L, -1);
276 int Eng3D::LuaVM::ui_set_window_on_click_close_btn(lua_State*
L) {
282 lua_getglobal(
L,
"UI_DriverCallOnClick");
283 lua_rawgeti(
L, LUA_REGISTRYINDEX, o.lua_on_close_btn);
286 return e.second == &o;
288 lua_pushinteger(
L, it->first);
289 if(call_func(
L, 2, 0)) {
290 const std::string err_msg = lua_tostring(
L, -1);
300 int Eng3D::LuaVM::ui_get_widget(lua_State*
L) {
302 lua_pushinteger(
L, widget->
width);
303 lua_pushinteger(
L, widget->
height);
304 lua_pushinteger(
L, widget->
x);
305 lua_pushinteger(
L, widget->
y);
309 int Eng3D::LuaVM::ui_widget_kill(lua_State*
L) {
310 const int widget_id = luaL_checkinteger(
L, 1);
317 int Eng3D::LuaVM::ui_widget_set_tooltip(lua_State*
L) {
323 int Eng3D::LuaVM::ui_register_callback(lua_State*
L) {
324 const std::string name = luaL_checkstring(
L, 1);
331 int Eng3D::LuaVM::ui_widget_set_flex(lua_State*
L) {
333 int flex_mode = luaL_checkinteger(
L, 2);
336 }
else if(flex_mode == 2) {
346 int hpos = lua_gettop(
L) - nargs;
348 lua_pushcfunction(
L, [](lua_State* Lua) {
349 lua_getglobal(Lua,
"debug");
350 lua_getfield(Lua, -1,
"traceback");
351 lua_pushvalue(Lua, 1);
352 lua_pushinteger(Lua, 1);
359 int r = lua_pcall(
L, nargs, nret, hpos);
367 return this->call_func(this->state, nargs, nret);
378 if(this->call_func(0, 0)) {
379 const std::string err_msg = lua_tostring(this->state, -1);
381 lua_pop(this->state, 1);
388 if(this->state !=
nullptr)
389 lua_close(this->state);
static State & get_instance()
Grouping to keep widgets together without triggering events.
Image widget, can display pictures or effects on the screen.
Simple widget for drawing text on the screen, no multiline support.
void set_value(const float _value)
Window widget, this widget is similar to a Group widget, the key difference is that this one can be m...
void set_close_btn_function(std::function< void(Widget &)> on_click)
std::map< int, std::shared_ptr< Eng3D::Texture > > lua_textures
std::map< int, UI::Widget * > lua_widgets
std::map< std::string, int > lua_ui_callbacks
void error(const std::string_view category, const std::string_view msg)
void invoke_registered_callback(const std::string &name)
Some UI functions are hardcoded, for example the main menu is hardcoded to appear when the game start...
static int call_func(lua_State *L, int nargs, int nret)
void set_value(bool checked)
A basic widget without any presets.
#define CXX_THROW(class,...)