diff --git a/doc/en/scripting/builtins/libhud.md b/doc/en/scripting/builtins/libhud.md index 963e428c..72b1a4ed 100644 --- a/doc/en/scripting/builtins/libhud.md +++ b/doc/en/scripting/builtins/libhud.md @@ -8,8 +8,17 @@ hud.open_inventory() hud.close_inventory() -- Open UI and inventory. --- Throws an exception if has no UI layout. -hud.open(invid: int, layoutid: str) +-- Throws an exception if has UI layout does not exists. +-- If invid is not specified, a virtual (temporary) inventory is created. +-- Returns the invid or id of the virtual inventory. +hud.open( + -- UI layout name + layoutid: str, + -- Don't open player inventory + [optional] disablePlayerInventory: bool, + -- Inventory that UI layout will be bound to + [optional] invid: int +) -> int -- Open block UI and inventory. -- Throws an exception if block has no UI layout. diff --git a/doc/ru/scripting/builtins/libhud.md b/doc/ru/scripting/builtins/libhud.md index 4bf14d99..d43867ac 100644 --- a/doc/ru/scripting/builtins/libhud.md +++ b/doc/ru/scripting/builtins/libhud.md @@ -8,8 +8,18 @@ hud.open_inventory() hud.close_inventory() -- Открывает инвентарь и UI. --- Если не имеет макета UI - бросается исключение. -hud.open(invid: int, layoutid: str) +-- Если макет UI не существует - бросается исключение. +-- Если invid не указан, создаётся виртуальный (временный) инвентарь. +-- Возвращает invid или id виртуального инвентаря. +hud.open( + -- Макет UI + layoutid: str, + -- Не открывать инвентарь игрока + [опционально] disablePlayerInventory: bool, + -- Инвентарь, к которому будет привязан UI макет + [опционально] invid: int +) -> int + -- Открывает инвентарь и UI блока. -- Если блок не имеет макета UI - бросается исключение. diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index cc9c54d0..30659969 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -382,16 +382,11 @@ void Hud::openInventory() { add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false)); } -void Hud::openInventory( +std::shared_ptr Hud::openInventory( UiDocument* doc, std::shared_ptr inv, bool playerInventory ) { - if (inv == nullptr) { - // why try to open nox-existent inventory?? - return; - } - if (isInventoryOpen()) { closeInventory(); } @@ -413,6 +408,7 @@ void Hud::openInventory( } secondInvView->bind(inv, content); add(HudElement(hud_element_mode::inventory_bound, doc, secondUI, false)); + return inv; } void Hud::openInventory( diff --git a/src/frontend/hud.hpp b/src/frontend/hud.hpp index 4a2ecfd5..580ab54d 100644 --- a/src/frontend/hud.hpp +++ b/src/frontend/hud.hpp @@ -153,7 +153,7 @@ public: /// @param doc ui layout /// @param inv inventory /// @param playerInventory show player inventory too - void openInventory( + std::shared_ptr openInventory( UiDocument* doc, std::shared_ptr inv, bool playerInventory diff --git a/src/logic/scripting/lua/libs/libhud.cpp b/src/logic/scripting/lua/libs/libhud.cpp index 460cf66b..7f5ec46d 100644 --- a/src/logic/scripting/lua/libs/libhud.cpp +++ b/src/logic/scripting/lua/libs/libhud.cpp @@ -37,23 +37,20 @@ static int l_close_inventory(lua::State*) { } static int l_open(lua::State* L) { - auto invid = lua::tointeger(L, 1); - auto layoutid = lua::require_string(L, 2); - bool playerInventory = !lua::toboolean(L, 3); + auto layoutid = lua::require_string(L, 1); + bool playerInventory = !lua::toboolean(L, 2); + auto invid = lua::tointeger(L, 3); auto assets = engine->getAssets(); auto layout = assets->get(layoutid); if (layout == nullptr) { throw std::runtime_error("there is no ui layout " + util::quote(layoutid)); } - - hud->openInventory( + return lua::pushinteger(L, hud->openInventory( layout, level->inventories->get(invid), playerInventory - ); - - return 0; + )->getId()); } static int l_open_block(lua::State* L) {