block inventory auto-position

This commit is contained in:
MihailRis 2024-02-18 23:05:58 +03:00
parent 96032b60ea
commit b06142abb9
4 changed files with 23 additions and 7 deletions

View File

@ -438,6 +438,7 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm
void InventoryView::createReaders(gui::UiXmlReader& reader) {
reader.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
auto view = std::make_shared<InventoryView>();
view->setColor(glm::vec4(0.122f, 0.122f, 0.122f, 0.878f)); // todo: fixme
reader.addIgnore("slots-grid");
reader.readUINode(reader, element, *view);

View File

@ -420,6 +420,9 @@ void Hud::openInventory() {
* In case of nullptr a new virtual inventory will be created
*/
void Hud::openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockinv) {
if (isInventoryOpen()) {
closeInventory();
}
auto level = frontend->getLevel();
blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (blockUI == nullptr) {
@ -570,10 +573,24 @@ void Hud::draw(const GfxContext& ctx){
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
glm::vec2 invSize = inventoryView->getSize();
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
if (blockUI == nullptr) {
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
} else {
glm::vec2 blockInvSize = blockUI->getSize();
int interval = 5;
float totalHeight = invSize.y + blockInvSize.y + interval;
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2+totalHeight/2-invSize.y
));
blockUI->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-totalHeight/2
));
}
}
grabbedItemView->setCoord(glm::vec2(Events::cursor));
batch->render();

View File

@ -433,9 +433,7 @@ int l_set_block_rotation(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
lua::luaint value = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
return 0;

View File

@ -60,7 +60,7 @@ int l_hud_open_block(lua_State* L) {
x, y, z
);
}
scripting::hud->openInventory(
glm::ivec3(x, y, z), layout, scripting::level->inventories->get(id)
);