gui: more lua support + 'coord' renamed to 'pos'

This commit is contained in:
MihailRis 2024-02-18 22:08:08 +03:00
parent 6f88502cae
commit ed6a377e90
5 changed files with 23 additions and 11 deletions

View File

@ -1,4 +1,4 @@
<inventory color="#1F1F1FE0">
<slots-grid coord="0, 164" rows="1" count="10" sharefunc="inventory_share_func"/>
<slots-grid pos="0, 164" rows="1" count="10" sharefunc="inventory_share_func"/>
<slots-grid rows="3" start-index="10" count="30" sharefunc="inventory_share_func"/>
</inventory>

View File

@ -25,8 +25,8 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
if (element->has("id")) {
node.setId(element->attr("id").getText());
}
if (element->has("coord")) {
node.setCoord(element->attr("coord").asVec2());
if (element->has("pos")) {
node.setCoord(element->attr("pos").asVec2());
}
if (element->has("size")) {
node.setSize(element->attr("size").asVec2());

View File

@ -571,7 +571,7 @@ void Hud::draw(const GfxContext& ctx){
glm::vec2 invSize = inventoryView->getSize();
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
}

View File

@ -71,7 +71,7 @@ int l_gui_getattr(lua_State* L) {
if (attr == "color") {
return lua::pushcolor_arr(L, node->getColor());
} else if (attr == "coord") {
} else if (attr == "pos") {
return lua::pushvec2_arr(L, node->getCoord());
} else if (attr == "size") {
return lua::pushvec2_arr(L, node->getSize());
@ -96,11 +96,15 @@ int l_gui_setattr(lua_State* L) {
const std::string attr = lua_tostring(L, 3);
auto node = getDocumentNode(L, docname, element);
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
return 0;
if (attr == "pos") {
node->setCoord(lua::tovec2(L, 1));
} else if (attr == "size") {
node->setSize(lua::tovec2(L, 1));
} else {
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
return 0;
}
return 0;
}

View File

@ -85,6 +85,14 @@ namespace lua {
lua_rawseti(L, -2, 4);
return 1;
}
inline glm::vec2 tovec2(lua_State* L, int idx) {
lua_rawgeti(L, idx, 1);
lua::luanumber x = lua_tonumber(L, -1); lua_pop(L, -1);
lua_rawgeti(L, idx, 1);
lua::luanumber y = lua_tonumber(L, -1); lua_pop(L, -1);
return glm::vec2(x, y);
}
}
#endif // LOGIC_SCRIPTING_LUA_UTIL_H_