Feature #152 and potential fix #320

- Added Lua scripts hud.open, inventory.create and inventory.remove
- Window now resizes even if it is not focused
This commit is contained in:
opchik98
2024-11-07 18:38:24 +02:00
parent 5faca685ec
commit 8e549fe806
11 changed files with 117 additions and 13 deletions
+33
View File
@@ -388,6 +388,39 @@ void Hud::openInventory() {
add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false));
}
void Hud::openInventory(
UiDocument* doc,
std::shared_ptr<Inventory> inv,
bool playerInventory
) {
if (inv == nullptr) {
// why try to open nox-existent inventory??
return;
}
if (isInventoryOpen()) {
closeInventory();
}
auto level = frontend->getLevel();
auto content = level->content;
secondInvView = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (secondInvView == nullptr) {
throw std::runtime_error("secondary UI root element must be 'inventory'");
}
secondUI = secondInvView;
if (playerInventory) {
openInventory();
} else {
inventoryOpen = true;
}
if (inv == nullptr) {
inv = level->inventories->createVirtual(secondInvView->getSlotsCount());
}
secondInvView->bind(inv, content);
add(HudElement(hud_element_mode::inventory_bound, doc, secondUI, false));
}
void Hud::openInventory(
glm::ivec3 block,
UiDocument* doc,
+12
View File
@@ -102,6 +102,8 @@ class Hud : public util::ObjectsKeeper {
std::shared_ptr<gui::InventoryView> inventoryView = nullptr;
/// @brief Block inventory view
std::shared_ptr<gui::InventoryView> blockUI = nullptr;
/// @brief Secondary inventory view
std::shared_ptr<gui::InventoryView> secondInvView = nullptr;
/// @brief Position of the block open
glm::ivec3 blockPos {};
/// @brief Id of the block open (used to detect block destruction or replacement)
@@ -145,6 +147,16 @@ public:
/// @brief Show player inventory in inventory-mode
void openInventory();
/// @brief Show inventory in inventory-mode
/// @param doc ui layout
/// @param inv inventory
/// @param playerInventory show player inventory too
void openInventory(
UiDocument* doc,
std::shared_ptr<Inventory> inv,
bool playerInventory
);
/// @brief Show block inventory in inventory-mode
/// @param block block position
+21
View File
@@ -36,6 +36,26 @@ static int l_close_inventory(lua::State*) {
return 0;
}
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 assets = engine->getAssets();
auto layout = assets->get<UiDocument>(layoutid);
if (layout == nullptr) {
throw std::runtime_error("there is no ui layout " + util::quote(layoutid));
}
hud->openInventory(
layout,
level->inventories->get(invid),
playerInventory
);
return 0;
}
static int l_open_block(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
@@ -154,6 +174,7 @@ static int l_set_debug_cheats(lua::State* L) {
const luaL_Reg hudlib[] = {
{"open_inventory", lua::wrap<l_open_inventory>},
{"close_inventory", lua::wrap<l_close_inventory>},
{"open", lua::wrap<l_open>},
{"open_block", lua::wrap<l_open_block>},
{"open_permanent", lua::wrap<l_open_permanent>},
{"show_overlay", lua::wrap<l_show_overlay>},
@@ -109,6 +109,26 @@ static int l_inventory_unbind_block(lua::State* L) {
return 0;
}
static int l_inventory_create(lua::State* L) {
auto invsize = lua::tointeger(L, 1);
auto inv = level->inventories->create(invsize);
if (inv == nullptr) {
return lua::pushinteger(L, 0);
}
return lua::pushinteger(L, inv->getId());
}
static int l_inventory_remove(lua::State* L) {
auto invid = lua::tointeger(L, 1);
auto inv = get_inventory(invid);
if (inv == nullptr) {
return 0;
}
level->inventories->remove(invid);
return 0;
}
static int l_inventory_clone(lua::State* L) {
auto id = lua::tointeger(L, 1);
auto clone = level->inventories->clone(id);
@@ -145,5 +165,7 @@ const luaL_Reg inventorylib[] = {
{"get_block", lua::wrap<l_inventory_get_block>},
{"bind_block", lua::wrap<l_inventory_bind_block>},
{"unbind_block", lua::wrap<l_inventory_unbind_block>},
{"create", lua::wrap<l_inventory_create>},
{"remove", lua::wrap<l_inventory_remove>},
{"clone", lua::wrap<l_inventory_clone>},
{NULL, NULL}};
+1 -1
View File
@@ -13,7 +13,7 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) {
top = CHUNK_H;
}
bool Chunk::isEmpty() {
bool Chunk::isEmpty() const {
int id = -1;
for (uint i = 0; i < CHUNK_VOL; i++) {
if (voxels[i].id != id) {
+1 -1
View File
@@ -44,7 +44,7 @@ public:
Chunk(int x, int z);
bool isEmpty();
bool isEmpty() const;
void updateHeights();
+3 -5
View File
@@ -72,11 +72,9 @@ bool Window::isFocused() {
void window_size_callback(GLFWwindow*, int width, int height) {
if (width && height) {
if (Window::isFocused()) {
glViewport(0, 0, width, height);
Window::width = width;
Window::height = height;
}
glViewport(0, 0, width, height);
Window::width = width;
Window::height = height;
if (!Window::isFullscreen() && !Window::isMaximized()) {
Window::getSettings()->width.set(width);