auto document variable in ui-document lua script
This commit is contained in:
parent
05589b721e
commit
ef0d4d69e4
@ -1,6 +1,3 @@
|
||||
local Document = require("core:document")
|
||||
document = Document.new(DOC_NAME)
|
||||
|
||||
function on_open(invid)
|
||||
print("OPEN", invid)
|
||||
end
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
local Element = {}
|
||||
function Element.new(docname, name)
|
||||
return setmetatable({docname=docname, name=name}, {
|
||||
__index=function(self, k)
|
||||
return gui.getattr(self.docname, self.name, k)
|
||||
end,
|
||||
__newindex=function(self, k, v)
|
||||
gui.setattr(self.docname, self.name, k, v)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
local Document = {}
|
||||
function Document.new(docname)
|
||||
return setmetatable({name=docname}, {
|
||||
__index=function(self, k)
|
||||
return Element.new(self.name, k)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
return Document
|
||||
@ -110,4 +110,27 @@ end
|
||||
color_mt = {}
|
||||
function color_mt.__tostring(self)
|
||||
return "rgba("..self[1]..", "..self[2]..", "..self[3]..", "..self[4]..")"
|
||||
end
|
||||
end
|
||||
|
||||
-- class designed for simple UI-nodes access via properties syntax
|
||||
local Element = {}
|
||||
function Element.new(docname, name)
|
||||
return setmetatable({docname=docname, name=name}, {
|
||||
__index=function(self, k)
|
||||
return gui.getattr(self.docname, self.name, k)
|
||||
end,
|
||||
__newindex=function(self, k, v)
|
||||
gui.setattr(self.docname, self.name, k, v)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
-- the engine automatically creates an instance for every ui document (layout)
|
||||
Document = {}
|
||||
function Document.new(docname)
|
||||
return setmetatable({name=docname}, {
|
||||
__index=function(self, k)
|
||||
return Element.new(self.name, k)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user