auto document variable in ui-document lua script

This commit is contained in:
MihailRis
2024-02-13 20:54:33 +03:00
parent 05589b721e
commit ef0d4d69e4
5 changed files with 58 additions and 27 deletions
+24
View File
@@ -33,6 +33,16 @@ static bool getattr(lua_State* L, gui::Button* button, const std::string& attr)
return false;
}
static bool getattr(lua_State* L, gui::Label* label, const std::string& attr) {
if (label == nullptr)
return false;
if (attr == "text") {
lua_pushstring(L, util::wstr2str_utf8(label->getText()).c_str());
return true;
}
return false;
}
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
if (button == nullptr)
return false;
@@ -43,6 +53,16 @@ static bool setattr(lua_State* L, gui::Button* button, const std::string& attr)
return false;
}
static bool setattr(lua_State* L, gui::Label* label, const std::string& attr) {
if (label == nullptr)
return false;
if (attr == "text") {
label->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
return true;
}
return false;
}
int l_gui_getattr(lua_State* L) {
auto docname = lua_tostring(L, 1);
auto element = lua_tostring(L, 2);
@@ -59,6 +79,8 @@ int l_gui_getattr(lua_State* L) {
if (getattr(L, dynamic_cast<gui::Button*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::Label*>(node), attr))
return 1;
return 0;
}
@@ -72,6 +94,8 @@ int l_gui_setattr(lua_State* L) {
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
return 0;
return 0;
}
+10 -1
View File
@@ -130,6 +130,16 @@ std::unique_ptr<Environment> scripting::create_doc_environment(int parent, const
state->setfield("DOC_ENV");
state->pushstring(name.c_str());
state->setfield("DOC_NAME");
if (state->getglobal("Document")) {
if (state->getfield("new")) {
state->pushstring(name.c_str());
if (state->callNoThrow(1)) {
state->setfield("document", -3);
}
}
state->pop();
}
state->pop();
return std::make_unique<Environment>(id);
}
@@ -246,7 +256,6 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
}
void scripting::on_ui_open(UiDocument* layout, Inventory* inventory) {
timeutil::ScopeLogTimer log(555);
std::string name = layout->getId()+".open";
if (state->getglobal(name)) {
state->pushinteger(inventory->getId());