mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 17:39:46 +00:00
make hotbar size and layout configurable
Add hotbar-size, hotbar-layout and inventory-layout to config/defaults.toml, player.get_hotbar_size / player.set_hotbar_size and hud.is_hotbar_visible / hud.set_hotbar_visible.
This commit is contained in:
parent
2e2233a489
commit
aa670f3c9d
16 changed files with 188 additions and 20 deletions
|
|
@ -143,9 +143,11 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
|
|||
auto inventory = player.getInventory();
|
||||
auto& content = frontend.getLevel().content;
|
||||
|
||||
int size = player.getHotbarSize();
|
||||
|
||||
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr);
|
||||
InventoryBuilder builder(gui);
|
||||
builder.addGrid(10, 10, glm::vec2(), glm::vec4(4), true, slotLayout);
|
||||
builder.addGrid(size, size, glm::vec2(), glm::vec4(4), true, slotLayout);
|
||||
auto view = builder.build();
|
||||
view->setId("hud.hotbar");
|
||||
view->setOrigin(glm::vec2(view->getSize().x/2, 0));
|
||||
|
|
@ -177,7 +179,25 @@ Hud::Hud(Engine& engine, LevelFrontend& frontend, Player& player)
|
|||
contentAccessPanel->setScrollable(true);
|
||||
contentAccessPanel->setId("hud.content-access-panel");
|
||||
|
||||
hotbarView = createHotbar();
|
||||
auto hotbarLayout =
|
||||
frontend.getLevel().content.getDefaults()["hotbar-layout"].asString("");
|
||||
if (hotbarLayout.empty()) {
|
||||
hotbarView = createHotbar();
|
||||
} else {
|
||||
hotbarDocument = &assets.require<UiDocument>(hotbarLayout);
|
||||
hotbarView =
|
||||
std::dynamic_pointer_cast<InventoryView>(hotbarDocument->getRoot());
|
||||
if (hotbarView == nullptr) {
|
||||
throw std::runtime_error(
|
||||
"hotbar UI root element must be 'inventory'"
|
||||
);
|
||||
}
|
||||
hotbarView->bind(player.getInventory(), &frontend.getLevel().content);
|
||||
hotbarView->setInteractive(false);
|
||||
if (hotbarView->getPositionFunc() == nullptr) {
|
||||
hotbarView->setOrigin(glm::vec2(hotbarView->getSize().x / 2, 0));
|
||||
}
|
||||
}
|
||||
darkOverlay = guiutil::create(
|
||||
gui,
|
||||
"<container size='4000' color='#00000080' z-index='-1' visible='false'/>"
|
||||
|
|
@ -229,6 +249,7 @@ Hud::~Hud() {
|
|||
for (auto& element : elements) {
|
||||
onRemove(element);
|
||||
}
|
||||
hotbarView->unbind();
|
||||
gui.remove(hotbarView);
|
||||
gui.remove(darkOverlay);
|
||||
gui.remove(inventoryDropArea);
|
||||
|
|
@ -263,12 +284,13 @@ void Hud::processInput(bool visible) {
|
|||
}
|
||||
|
||||
void Hud::updateHotbarControl() {
|
||||
int size = player.getHotbarSize();
|
||||
int scroll = input.getScroll();
|
||||
if (!inventoryOpen && scroll) {
|
||||
int slot = player.getChosenSlot();
|
||||
slot = (slot - scroll) % 10;
|
||||
slot = (slot - scroll) % size;
|
||||
if (slot < 0) {
|
||||
slot += 10;
|
||||
slot += size;
|
||||
}
|
||||
player.setChosenSlot(slot);
|
||||
}
|
||||
|
|
@ -277,11 +299,12 @@ void Hud::updateHotbarControl() {
|
|||
i <= static_cast<int>(Keycode::NUM_9);
|
||||
i++
|
||||
) {
|
||||
if (input.jpressed(static_cast<Keycode>(i))) {
|
||||
player.setChosenSlot(i - static_cast<int>(Keycode::NUM_1));
|
||||
int slot = i - static_cast<int>(Keycode::NUM_1);
|
||||
if (slot < size && input.jpressed(static_cast<Keycode>(i))) {
|
||||
player.setChosenSlot(slot);
|
||||
}
|
||||
}
|
||||
if (input.jpressed(Keycode::NUM_0)) {
|
||||
if (size > 9 && input.jpressed(Keycode::NUM_0)) {
|
||||
player.setChosenSlot(9);
|
||||
}
|
||||
}
|
||||
|
|
@ -375,7 +398,16 @@ void Hud::update(bool visible) {
|
|||
contentAccessPanel->setVisible(inventoryView != nullptr && showContentPanel);
|
||||
contentAccessPanel->setSize(glm::vec2(caSize.x, windowSize.y));
|
||||
contentAccess->setMinSize(glm::vec2(1, windowSize.y));
|
||||
hotbarView->setVisible(visible && !(secondUI && !inventoryView));
|
||||
if (hotbarDocument == nullptr &&
|
||||
static_cast<int>(hotbarView->getSlotsCount()) !=
|
||||
player.getHotbarSize()) {
|
||||
gui.remove(hotbarView);
|
||||
hotbarView = createHotbar();
|
||||
gui.add(hotbarView);
|
||||
}
|
||||
hotbarView->setVisible(
|
||||
visible && hotbarVisible && !(secondUI && !inventoryView)
|
||||
);
|
||||
darkOverlay->setVisible(isMenuOpen);
|
||||
inventoryDropArea->setVisible(inventoryOpen);
|
||||
menu.setVisible(isMenuOpen);
|
||||
|
|
@ -404,8 +436,16 @@ void Hud::openInventory(bool playerInventory) {
|
|||
if (playerInventory) {
|
||||
auto& content = frontend.getLevel().content;
|
||||
auto inventory = player.getInventory();
|
||||
auto inventoryDocument = assets.get<UiDocument>("core:inventory");
|
||||
auto layout = content.getDefaults()["inventory-layout"].asString(
|
||||
"core:inventory"
|
||||
);
|
||||
auto inventoryDocument = &assets.require<UiDocument>(layout);
|
||||
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
|
||||
if (inventoryView == nullptr) {
|
||||
throw std::runtime_error(
|
||||
"player inventory UI root element must be 'inventory'"
|
||||
);
|
||||
}
|
||||
inventoryView->bind(inventory, &content);
|
||||
add(HudElement(HudElementMode::INVENTORY, inventoryDocument, inventoryView, false));
|
||||
}
|
||||
|
|
@ -700,7 +740,9 @@ void Hud::updateElementsPosition(const glm::uvec2& viewport) {
|
|||
if (exchangeSlot != nullptr) {
|
||||
exchangeSlot->setPos(input.getCursor().pos);
|
||||
}
|
||||
hotbarView->setPos(glm::vec2(viewport.x / 2, viewport.y - 65));
|
||||
if (hotbarView->getPositionFunc() == nullptr) {
|
||||
hotbarView->setPos(glm::vec2(viewport.x / 2, viewport.y - 65));
|
||||
}
|
||||
hotbarView->setSelected(player.getChosenSlot());
|
||||
}
|
||||
|
||||
|
|
@ -790,6 +832,14 @@ void Hud::setAllowPause(bool flag) {
|
|||
allowPause = flag;
|
||||
}
|
||||
|
||||
void Hud::setHotbarVisible(bool flag) {
|
||||
hotbarVisible = flag;
|
||||
}
|
||||
|
||||
bool Hud::isHotbarVisible() const {
|
||||
return hotbarVisible;
|
||||
}
|
||||
|
||||
bool Hud::isOpen(const std::string& layoutid) const {
|
||||
for (const auto& element : elements) {
|
||||
auto doc = element.getDocument();
|
||||
|
|
|
|||
|
|
@ -89,6 +89,10 @@ class Hud : public util::ObjectsKeeper {
|
|||
std::shared_ptr<gui::InventoryView> contentAccess;
|
||||
/// @brief Player inventory hotbar
|
||||
std::shared_ptr<gui::InventoryView> hotbarView;
|
||||
/// @brief Hotbar layout set by 'hotbar-layout', nullptr for built-in
|
||||
UiDocument* hotbarDocument = nullptr;
|
||||
/// @brief Hotbar is not hidden by scripts
|
||||
bool hotbarVisible = true;
|
||||
/// @brief Debug info and control panel (F3 key)
|
||||
std::shared_ptr<gui::UINode> debugPanel;
|
||||
/// @brief Overlay used in pause mode
|
||||
|
|
@ -219,6 +223,10 @@ public:
|
|||
|
||||
void setAllowPause(bool flag);
|
||||
|
||||
void setHotbarVisible(bool flag);
|
||||
|
||||
bool isHotbarVisible() const;
|
||||
|
||||
bool isOpen(const std::string& layoutid) const;
|
||||
|
||||
static bool showGeneratorMinimap;
|
||||
|
|
|
|||
|
|
@ -194,6 +194,15 @@ static int l_set_allow_pause(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int l_is_hotbar_visible(lua::State* L) {
|
||||
return lua::pushboolean(L, hud->isHotbarVisible());
|
||||
}
|
||||
|
||||
static int l_set_hotbar_visible(lua::State* L) {
|
||||
hud->setHotbarVisible(lua::toboolean(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_reload_script(lua::State* L) {
|
||||
auto packid = lua::require_string(L, 1);
|
||||
if (content == nullptr) {
|
||||
|
|
@ -237,6 +246,8 @@ const luaL_Reg hudlib[] = {
|
|||
{"_set_content_access", wrap_hud<l_set_content_access>},
|
||||
{"_set_debug_cheats", wrap_hud<l_set_debug_cheats>},
|
||||
{"set_allow_pause", wrap_hud<l_set_allow_pause>},
|
||||
{"is_hotbar_visible", wrap_hud<l_is_hotbar_visible>},
|
||||
{"set_hotbar_visible", wrap_hud<l_set_hotbar_visible>},
|
||||
{"reload_script", wrap_hud<l_reload_script>},
|
||||
{"is_open", wrap_hud<l_is_open>},
|
||||
{nullptr, nullptr}
|
||||
|
|
|
|||
|
|
@ -121,7 +121,21 @@ static int l_get_inv(lua::State* L) {
|
|||
|
||||
static int l_set_selected_slot(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
player->setChosenSlot(lua::tointeger(L, 2) % 10);
|
||||
player->setChosenSlot(lua::tointeger(L, 2) % player->getHotbarSize());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_hotbar_size(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushinteger(L, player->getHotbarSize());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_hotbar_size(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
player->setHotbarSize(lua::tointeger(L, 2));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -401,6 +415,8 @@ const luaL_Reg playerlib[] = {
|
|||
{"get_interaction_distance", lua::wrap<l_get_interaction_distance>},
|
||||
{"set_interaction_distance", lua::wrap<l_set_interaction_distance>},
|
||||
{"set_selected_slot", lua::wrap<l_set_selected_slot>},
|
||||
{"get_hotbar_size", lua::wrap<l_get_hotbar_size>},
|
||||
{"set_hotbar_size", lua::wrap<l_set_hotbar_size>},
|
||||
{"get_selected_block", lua::wrap<l_get_selected_block>},
|
||||
{"get_selected_entity", lua::wrap<l_get_selected_entity>},
|
||||
{"set_spawnpoint", lua::wrap<l_set_spawnpoint>},
|
||||
|
|
|
|||
|
|
@ -41,6 +41,12 @@ Player::Player(
|
|||
name(name),
|
||||
speed(speed),
|
||||
chosenSlot(0),
|
||||
hotbarSize(glm::max(
|
||||
1,
|
||||
static_cast<int>(
|
||||
level.content.getDefaults()["hotbar-size"].asInteger(10)
|
||||
)
|
||||
)),
|
||||
position(position),
|
||||
inventory(std::move(inv)),
|
||||
eid(eid),
|
||||
|
|
@ -153,13 +159,22 @@ void Player::attemptToChooseSpawnpoint() {
|
|||
}
|
||||
|
||||
void Player::setChosenSlot(int index) {
|
||||
chosenSlot = index;
|
||||
chosenSlot = glm::clamp(index, 0, hotbarSize - 1);
|
||||
}
|
||||
|
||||
int Player::getChosenSlot() const {
|
||||
return chosenSlot;
|
||||
}
|
||||
|
||||
void Player::setHotbarSize(int size) {
|
||||
hotbarSize = glm::max(1, size);
|
||||
setChosenSlot(chosenSlot);
|
||||
}
|
||||
|
||||
int Player::getHotbarSize() const {
|
||||
return hotbarSize;
|
||||
}
|
||||
|
||||
float Player::getSpeed() const {
|
||||
return speed;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ class Player : public Serializable {
|
|||
float speed;
|
||||
|
||||
int chosenSlot;
|
||||
int hotbarSize;
|
||||
glm::vec3 position;
|
||||
glm::vec3 spawnpoint {std::nanf(""), std::nanf(""), std::nanf("")};
|
||||
std::shared_ptr<Inventory> inventory;
|
||||
|
|
@ -96,6 +97,10 @@ public:
|
|||
|
||||
int getChosenSlot() const;
|
||||
|
||||
void setHotbarSize(int size);
|
||||
|
||||
int getHotbarSize() const;
|
||||
|
||||
float getSpeed() const;
|
||||
|
||||
bool isSuspended() const;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue