fix: lua coroutines support

This commit is contained in:
MihailRis
2024-06-11 01:52:28 +03:00
parent 590f9757c0
commit 7973a9c32b
15 changed files with 707 additions and 740 deletions
+151 -151
View File
@@ -50,8 +50,8 @@ static DocumentNode getDocumentNode(lua_State*, const std::string& name, const s
static DocumentNode getDocumentNode(lua_State* L, int idx=1) {
lua_getfield(L, idx, "docname");
lua_getfield(L, idx, "name");
auto docname = state->requireString(-2);
auto name = state->requireString(-1);
auto docname = lua::require_string(L, -2);
auto name = lua::require_string(L, -1);
auto node = getDocumentNode(L, docname, name);
lua_pop(L, 2);
return node;
@@ -74,7 +74,7 @@ static int l_menu_reset(lua_State* L) {
static int l_textbox_paste(lua_State* L) {
auto node = getDocumentNode(L);
auto box = dynamic_cast<TextBox*>(node.node.get());
auto text = state->requireString(2);
auto text = lua::require_string(L, 2);
box->paste(util::str2wstr_utf8(text));
return 0;
}
@@ -82,7 +82,7 @@ static int l_textbox_paste(lua_State* L) {
static int l_container_add(lua_State* L) {
auto docnode = getDocumentNode(L);
auto node = dynamic_cast<Container*>(docnode.node.get());
auto xmlsrc = state->requireString(2);
auto xmlsrc = lua::require_string(L, 2);
try {
auto subnode = guiutil::create(xmlsrc, docnode.document->getEnvironment());
node->add(subnode);
@@ -115,10 +115,10 @@ static int l_container_clear(lua_State* L) {
static int l_container_set_interval(lua_State* L) {
auto node = getDocumentNode(L, 1);
auto interval = state->tointeger(2) / 1000.0f;
auto interval = lua_tointeger(L, 2) / 1000.0f;
if (auto container = std::dynamic_pointer_cast<Container>(node.node)) {
state->pushvalue(3);
auto runnable = state->createRunnable();
lua_pushvalue(L, 3);
auto runnable = state->createRunnable(L);
container->listenInterval(interval, runnable);
}
return 0;
@@ -131,212 +131,212 @@ static int l_move_into(lua_State* L) {
return 0;
}
static int p_get_inventory(UINode* node) {
static int p_get_inventory(UINode* node, lua_State* L) {
if (auto inventory = dynamic_cast<InventoryView*>(node)) {
auto inv = inventory->getInventory();
return state->pushinteger(inv ? inv->getId() : 0);
return lua::pushinteger(L, inv ? inv->getId() : 0);
}
return 0;
}
static int p_get_reset(UINode* node) {
static int p_get_reset(UINode* node, lua_State* L) {
if (dynamic_cast<Menu*>(node)) {
return state->pushcfunction(l_menu_reset);
return lua::pushcfunction(L, l_menu_reset);
}
return 0;
}
static int p_get_back(UINode* node) {
static int p_get_back(UINode* node, lua_State* L) {
if (dynamic_cast<Menu*>(node)) {
return state->pushcfunction(l_menu_back);
return lua::pushcfunction(L, l_menu_back);
}
return 0;
}
static int p_get_paste(UINode* node) {
static int p_get_paste(UINode* node, lua_State* L) {
if (dynamic_cast<TextBox*>(node)) {
return state->pushcfunction(l_textbox_paste);
return lua::pushcfunction(L, l_textbox_paste);
}
return 0;
}
static int p_get_page(UINode* node) {
static int p_get_page(UINode* node, lua_State* L) {
if (auto menu = dynamic_cast<Menu*>(node)) {
return state->pushstring(menu->getCurrent().name);
return lua::pushstring(L, menu->getCurrent().name);
}
return 0;
}
static int p_is_checked(UINode* node) {
static int p_is_checked(UINode* node, lua_State* L) {
if (auto box = dynamic_cast<CheckBox*>(node)) {
return state->pushboolean(box->isChecked());
return lua::pushboolean(L, box->isChecked());
} else if (auto box = dynamic_cast<FullCheckBox*>(node)) {
return state->pushboolean(box->isChecked());
return lua::pushboolean(L, box->isChecked());
}
return 0;
}
static int p_get_value(UINode* node) {
static int p_get_value(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return state->pushnumber(bar->getValue());
return lua::pushnumber(L, bar->getValue());
}
return 0;
}
static int p_get_min(UINode* node) {
static int p_get_min(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return state->pushnumber(bar->getMin());
return lua::pushnumber(L, bar->getMin());
}
return 0;
}
static int p_get_max(UINode* node) {
static int p_get_max(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return state->pushnumber(bar->getMax());
return lua::pushnumber(L, bar->getMax());
}
return 0;
}
static int p_get_step(UINode* node) {
static int p_get_step(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return state->pushnumber(bar->getStep());
return lua::pushnumber(L, bar->getStep());
}
return 0;
}
static int p_get_track_width(UINode* node) {
static int p_get_track_width(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return state->pushnumber(bar->getTrackWidth());
return lua::pushnumber(L, bar->getTrackWidth());
}
return 0;
}
static int p_get_track_color(UINode* node) {
static int p_get_track_color(UINode* node, lua_State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
return lua::pushcolor_arr(state->getLua(), bar->getTrackColor());
return lua::pushcolor_arr(L, bar->getTrackColor());
}
return 0;
}
static int p_is_valid(UINode* node) {
static int p_is_valid(UINode* node, lua_State* L) {
if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushboolean(box->validate());
return lua::pushboolean(L, box->validate());
}
return 0;
}
static int p_get_caret(UINode* node) {
static int p_get_caret(UINode* node, lua_State* L) {
if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushinteger(static_cast<integer_t>(box->getCaret()));
return lua::pushinteger(L, static_cast<integer_t>(box->getCaret()));
}
return 0;
}
static int p_get_placeholder(UINode* node) {
static int p_get_placeholder(UINode* node, lua_State* L) {
if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushstring(util::wstr2str_utf8(box->getPlaceholder()));
return lua::pushwstring(L, box->getPlaceholder());
}
return 0;
}
static int p_get_text(UINode* node) {
static int p_get_text(UINode* node, lua_State* L) {
if (auto button = dynamic_cast<Button*>(node)) {
return state->pushstring(util::wstr2str_utf8(button->getText()));
return lua::pushwstring(L, button->getText());
} else if (auto label = dynamic_cast<Label*>(node)) {
return state->pushstring(util::wstr2str_utf8(label->getText()));
return lua::pushwstring(L, label->getText());
} else if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushstring(util::wstr2str_utf8(box->getText()));
return lua::pushwstring(L, box->getText());
}
return 0;
}
static int p_get_editable(UINode* node) {
static int p_get_editable(UINode* node, lua_State* L) {
if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushboolean(box->isEditable());
return lua::pushboolean(L, box->isEditable());
}
return 0;
}
static int p_get_src(UINode* node) {
static int p_get_src(UINode* node, lua_State* L) {
if (auto image = dynamic_cast<Image*>(node)) {
return state->pushstring(image->getTexture());
return lua::pushstring(L, image->getTexture());
}
return 0;
}
static int p_get_add(UINode* node) {
static int p_get_add(UINode* node, lua_State* L) {
if (dynamic_cast<Container*>(node)) {
return state->pushcfunction(lua_wrap_errors<l_container_add>);
return lua::pushcfunction(L, lua_wrap_errors<l_container_add>);
}
return 0;
}
static int p_get_destruct(UINode*) {
return state->pushcfunction(lua_wrap_errors<l_node_destruct>);
static int p_get_destruct(UINode*, lua_State* L) {
return lua::pushcfunction(L, lua_wrap_errors<l_node_destruct>);
}
static int p_get_clear(UINode* node) {
static int p_get_clear(UINode* node, lua_State* L) {
if (dynamic_cast<Container*>(node)) {
return state->pushcfunction(lua_wrap_errors<l_container_clear>);
return lua::pushcfunction(L, lua_wrap_errors<l_container_clear>);
}
return 0;
}
static int p_set_interval(UINode* node) {
static int p_set_interval(UINode* node, lua_State* L) {
if (dynamic_cast<Container*>(node)) {
return state->pushcfunction(lua_wrap_errors<l_container_set_interval>);
return lua::pushcfunction(L, lua_wrap_errors<l_container_set_interval>);
}
return 0;
}
static int p_get_color(UINode* node) {
return lua::pushcolor_arr(state->getLua(), node->getColor());
static int p_get_color(UINode* node, lua_State* L) {
return lua::pushcolor_arr(L, node->getColor());
}
static int p_get_hover_color(UINode* node) {
return lua::pushcolor_arr(state->getLua(), node->getHoverColor());
static int p_get_hover_color(UINode* node, lua_State* L) {
return lua::pushcolor_arr(L, node->getHoverColor());
}
static int p_get_pressed_color(UINode* node) {
return lua::pushcolor_arr(state->getLua(), node->getPressedColor());
static int p_get_pressed_color(UINode* node, lua_State* L) {
return lua::pushcolor_arr(L, node->getPressedColor());
}
static int p_get_tooltip(UINode* node) {
return state->pushstring(util::wstr2str_utf8(node->getTooltip()));
static int p_get_tooltip(UINode* node, lua_State* L) {
return lua::pushwstring(L, node->getTooltip());
}
static int p_get_tooltip_delay(UINode* node) {
return state->pushnumber(node->getTooltipDelay());
static int p_get_tooltip_delay(UINode* node, lua_State* L) {
return lua::pushnumber(L, node->getTooltipDelay());
}
static int p_get_pos(UINode* node) {
return lua::pushvec2_arr(state->getLua(), node->getPos());
static int p_get_pos(UINode* node, lua_State* L) {
return lua::pushvec2_arr(L, node->getPos());
}
static int p_get_wpos(UINode* node) {
return lua::pushvec2_arr(state->getLua(), node->calcPos());
static int p_get_wpos(UINode* node, lua_State* L) {
return lua::pushvec2_arr(L, node->calcPos());
}
static int p_get_size(UINode* node) {
return lua::pushvec2_arr(state->getLua(), node->getSize());
static int p_get_size(UINode* node, lua_State* L) {
return lua::pushvec2_arr(L, node->getSize());
}
static int p_is_interactive(UINode* node) {
return state->pushboolean(node->isInteractive());
static int p_is_interactive(UINode* node, lua_State* L) {
return lua::pushboolean(L, node->isInteractive());
}
static int p_is_visible(UINode* node) {
return state->pushboolean(node->isVisible());
static int p_is_visible(UINode* node, lua_State* L) {
return lua::pushboolean(L, node->isVisible());
}
static int p_is_enabled(UINode* node) {
return state->pushboolean(node->isEnabled());
static int p_is_enabled(UINode* node, lua_State* L) {
return lua::pushboolean(L, node->isEnabled());
}
static int p_move_into(UINode*) {
return state->pushcfunction(l_move_into);
static int p_move_into(UINode*, lua_State* L) {
return lua::pushcfunction(L, l_move_into);
}
static int p_get_focused(UINode* node) {
return state->pushboolean(node->isFocused());
static int p_get_focused(UINode* node, lua_State* L) {
return lua::pushboolean(L, node->isFocused());
}
static int l_gui_getattr(lua_State* L) {
auto docname = state->requireString(1);
auto element = state->requireString(2);
auto attr = state->requireString(3);
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3);
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
static const std::unordered_map<std::string_view, std::function<int(UINode*)>> getters {
static const std::unordered_map<std::string_view, std::function<int(UINode*,lua_State*)>> getters {
{"color", p_get_color},
{"hoverColor", p_get_hover_color},
{"pressedColor", p_get_pressed_color},
@@ -375,118 +375,118 @@ static int l_gui_getattr(lua_State* L) {
};
auto func = getters.find(attr);
if (func != getters.end()) {
return func->second(node.get());
return func->second(node.get(), L);
}
return 0;
}
static void p_set_color(UINode* node, int idx) {
node->setColor(state->tocolor(idx));
static void p_set_color(UINode* node, lua_State* L, int idx) {
node->setColor(lua::tocolor(L, idx));
}
static void p_set_hover_color(UINode* node, int idx) {
node->setHoverColor(state->tocolor(idx));
static void p_set_hover_color(UINode* node, lua_State* L, int idx) {
node->setHoverColor(lua::tocolor(L, idx));
}
static void p_set_pressed_color(UINode* node, int idx) {
node->setPressedColor(state->tocolor(idx));
static void p_set_pressed_color(UINode* node, lua_State* L, int idx) {
node->setPressedColor(lua::tocolor(L, idx));
}
static void p_set_tooltip(UINode* node, int idx) {
node->setTooltip(util::str2wstr_utf8(state->requireString(idx)));
static void p_set_tooltip(UINode* node, lua_State* L, int idx) {
node->setTooltip(lua::require_wstring(L, idx));
}
static void p_set_tooltip_delay(UINode* node, int idx) {
node->setTooltipDelay(state->tonumber(idx));
static void p_set_tooltip_delay(UINode* node, lua_State* L, int idx) {
node->setTooltipDelay(lua_tonumber(L, idx));
}
static void p_set_pos(UINode* node, int idx) {
node->setPos(state->tovec2(idx));
static void p_set_pos(UINode* node, lua_State* L, int idx) {
node->setPos(lua::tovec2(L, idx));
}
static void p_set_wpos(UINode* node, int idx) {
node->setPos(state->tovec2(idx)-node->calcPos());
static void p_set_wpos(UINode* node, lua_State* L, int idx) {
node->setPos(lua::tovec2(L, idx)-node->calcPos());
}
static void p_set_size(UINode* node, int idx) {
node->setSize(state->tovec2(idx));
static void p_set_size(UINode* node, lua_State* L, int idx) {
node->setSize(lua::tovec2(L, idx));
}
static void p_set_interactive(UINode* node, int idx) {
node->setInteractive(state->toboolean(idx));
static void p_set_interactive(UINode* node, lua_State* L, int idx) {
node->setInteractive(lua_toboolean(L, idx));
}
static void p_set_visible(UINode* node, int idx) {
node->setVisible(state->toboolean(idx));
static void p_set_visible(UINode* node, lua_State* L, int idx) {
node->setVisible(lua_toboolean(L, idx));
}
static void p_set_enabled(UINode* node, int idx) {
node->setEnabled(state->toboolean(idx));
static void p_set_enabled(UINode* node, lua_State* L, int idx) {
node->setEnabled(lua_toboolean(L, idx));
}
static void p_set_placeholder(UINode* node, int idx) {
static void p_set_placeholder(UINode* node, lua_State* L, int idx) {
if (auto box = dynamic_cast<TextBox*>(node)) {
box->setPlaceholder(util::str2wstr_utf8(state->requireString(idx)));
box->setPlaceholder(lua::require_wstring(L, idx));
}
}
static void p_set_text(UINode* node, int idx) {
static void p_set_text(UINode* node, lua_State* L, int idx) {
if (auto label = dynamic_cast<Label*>(node)) {
label->setText(util::str2wstr_utf8(state->requireString(idx)));
label->setText(lua::require_wstring(L, idx));
} else if (auto button = dynamic_cast<Button*>(node)) {
button->setText(util::str2wstr_utf8(state->requireString(idx)));
button->setText(lua::require_wstring(L, idx));
} else if (auto box = dynamic_cast<TextBox*>(node)) {
box->setText(util::str2wstr_utf8(state->requireString(idx)));
box->setText(lua::require_wstring(L, idx));
}
}
static void p_set_caret(UINode* node, int idx) {
static void p_set_caret(UINode* node, lua_State* L, int idx) {
if (auto box = dynamic_cast<TextBox*>(node)) {
box->setCaret(static_cast<ptrdiff_t>(state->tointeger(idx)));
box->setCaret(static_cast<ptrdiff_t>(lua_tointeger(L, idx)));
}
}
static void p_set_editable(UINode* node, int idx) {
static void p_set_editable(UINode* node, lua_State* L, int idx) {
if (auto box = dynamic_cast<TextBox*>(node)) {
box->setEditable(state->toboolean(idx));
box->setEditable(lua_toboolean(L, idx));
}
}
static void p_set_src(UINode* node, int idx) {
static void p_set_src(UINode* node, lua_State* L, int idx) {
if (auto image = dynamic_cast<Image*>(node)) {
image->setTexture(state->requireString(idx));
image->setTexture(lua::require_string(L, idx));
}
}
static void p_set_value(UINode* node, int idx) {
static void p_set_value(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setValue(state->tonumber(idx));
bar->setValue(lua_tonumber(L, idx));
}
}
static void p_set_min(UINode* node, int idx) {
static void p_set_min(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setMin(state->tonumber(idx));
bar->setMin(lua_tonumber(L, idx));
}
}
static void p_set_max(UINode* node, int idx) {
static void p_set_max(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setMax(state->tonumber(idx));
bar->setMax(lua_tonumber(L, idx));
}
}
static void p_set_step(UINode* node, int idx) {
static void p_set_step(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setStep(state->tonumber(idx));
bar->setStep(lua_tonumber(L, idx));
}
}
static void p_set_track_width(UINode* node, int idx) {
static void p_set_track_width(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setTrackWidth(state->tointeger(idx));
bar->setTrackWidth(lua_tointeger(L, idx));
}
}
static void p_set_track_color(UINode* node, int idx) {
static void p_set_track_color(UINode* node, lua_State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setTrackColor(state->tocolor(idx));
bar->setTrackColor(lua::tocolor(L, idx));
}
}
static void p_set_checked(UINode* node, int idx) {
static void p_set_checked(UINode* node, lua_State* L, int idx) {
if (auto box = dynamic_cast<CheckBox*>(node)) {
box->setChecked(state->toboolean(idx));
box->setChecked(lua_toboolean(L, idx));
} else if (auto box = dynamic_cast<FullCheckBox*>(node)) {
box->setChecked(state->toboolean(idx));
box->setChecked(lua_toboolean(L, idx));
}
}
static void p_set_page(UINode* node, int idx) {
static void p_set_page(UINode* node, lua_State* L, int idx) {
if (auto menu = dynamic_cast<Menu*>(node)) {
menu->setPage(state->requireString(idx));
menu->setPage(lua::require_string(L, idx));
}
}
static void p_set_inventory(UINode* node, int idx) {
static void p_set_inventory(UINode* node, lua_State* L, int idx) {
if (auto view = dynamic_cast<InventoryView*>(node)) {
auto inventory = level->inventories->get(state->tointeger(idx));
auto inventory = level->inventories->get(lua_tointeger(L, idx));
if (inventory == nullptr) {
view->unbind();
} else {
@@ -494,8 +494,8 @@ static void p_set_inventory(UINode* node, int idx) {
}
}
}
static void p_set_focused(const std::shared_ptr<UINode> &node, int idx) {
if (state->toboolean(idx) && !node->isFocused()) {
static void p_set_focused(const std::shared_ptr<UINode> &node, lua_State* L, int idx) {
if (lua_toboolean(L, idx) && !node->isFocused()) {
scripting::engine->getGUI()->setFocus(node);
} else if (node->isFocused()){
node->defocus();
@@ -503,14 +503,14 @@ static void p_set_focused(const std::shared_ptr<UINode> &node, int idx) {
}
static int l_gui_setattr(lua_State* L) {
auto docname = state->requireString(1);
auto element = state->requireString(2);
auto attr = state->requireString(3);
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3);
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
static const std::unordered_map<std::string_view, std::function<void(UINode*,int)>> setters {
static const std::unordered_map<std::string_view, std::function<void(UINode*,lua_State*,int)>> setters {
{"color", p_set_color},
{"hoverColor", p_set_hover_color},
{"pressedColor", p_set_pressed_color},
@@ -539,20 +539,20 @@ static int l_gui_setattr(lua_State* L) {
};
auto func = setters.find(attr);
if (func != setters.end()) {
func->second(node.get(), 4);
func->second(node.get(), L, 4);
}
static const std::unordered_map<std::string_view, std::function<void(std::shared_ptr<UINode>,int)>> setters2 {
static const std::unordered_map<std::string_view, std::function<void(std::shared_ptr<UINode>,lua_State*,int)>> setters2 {
{"focused", p_set_focused},
};
auto func2 = setters2.find(attr);
if (func2 != setters2.end()) {
func2->second(node, 4);
func2->second(node, L, 4);
}
return 0;
}
static int l_gui_get_env(lua_State* L) {
auto name = state->requireString(1);
auto name = lua::require_string(L, 1);
auto doc = scripting::engine->getAssets()->getLayout(name);
if (doc == nullptr) {
throw std::runtime_error("document '"+std::string(name)+"' not found");
@@ -562,9 +562,9 @@ static int l_gui_get_env(lua_State* L) {
}
static int l_gui_str(lua_State* L) {
auto text = util::str2wstr_utf8(state->requireString(1));
auto text = lua::require_wstring(L, 1);
if (!lua_isnoneornil(L, 2)) {
auto context = util::str2wstr_utf8(state->requireString(2));
auto context = lua::require_wstring(L, 2);
lua_pushstring(L, util::wstr2str_utf8(langs::get(text, context)).c_str());
} else {
lua_pushstring(L, util::wstr2str_utf8(langs::get(text)).c_str());
@@ -573,7 +573,7 @@ static int l_gui_str(lua_State* L) {
}
static int l_gui_reindex(lua_State* L) {
auto name = state->requireString(1);
auto name = lua::require_string(L, 1);
auto doc = scripting::engine->getAssets()->getLayout(name);
if (doc == nullptr) {
throw std::runtime_error("document '"+std::string(name)+"' not found");