From d6fe4c3bbd16b7511223b2bedb787da25c664b34 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 11 Apr 2024 22:18:49 +0200 Subject: [PATCH] Make alignment behave like other elements --- builtin/game/hud.lua | 2 +- doc/lua_api.md | 4 +--- games/devtest/mods/testhud/init.lua | 10 ++++++---- src/client/hud.cpp | 10 +++++----- src/client/hud.h | 2 +- src/script/common/c_content.cpp | 5 ++++- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/builtin/game/hud.lua b/builtin/game/hud.lua index cfd321fc2..d75eef813 100644 --- a/builtin/game/hud.lua +++ b/builtin/game/hud.lua @@ -267,7 +267,7 @@ register_builtin_hud_element("hotbar", { type = "hotbar", position = {x = 0.5, y = 1}, direction = 0, - alignment = {x = -0.5, y = -1}, + alignment = {x = 0, y = -1}, offset = {x = 0, y = -4}, -- Extra padding below. }, show_elem = function(player, flags) diff --git a/doc/lua_api.md b/doc/lua_api.md index d3185416a..1509b66ab 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -1751,15 +1751,13 @@ Displays a horizontal bar made up of half-images with an optional background. * `item`: Position of item that is selected. * `direction`: Direction the list will be displayed in * `offset`: offset in pixels from position. -* `alignment`: The alignment of the inventory. - It starts at the top left corner and not at the center like most other types. +* `alignment`: The alignment of the inventory. Aligned at the top left corner if not specified. ### `hotbar` * `direction`: Direction the list will be displayed in * `offset`: offset in pixels from position. * `alignment`: The alignment of the inventory. - It starts at the top left corner and not at the center like the most other types. ### `waypoint` diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua index f5325932f..8c0cd9c89 100644 --- a/games/devtest/mods/testhud/init.lua +++ b/games/devtest/mods/testhud/init.lua @@ -213,25 +213,27 @@ local hud_hotbar_defs = { type = "hotbar", position = {x=0.2, y=0.5}, direction = 0, - alignment = {x=0, y=-1}, + alignment = {x=1, y=-1}, }, { type = "hotbar", position = {x=0.2, y=0.5}, direction = 2, + alignment = {x=1, y=1}, }, { type = "hotbar", position = {x=0.7, y=0.5}, direction = 0, - offset = {x=40, y=20}, + offset = {x=140, y=20}, + alignment = {x=-1, y=-1}, }, { type = "hotbar", position = {x=0.7, y=0.5}, direction = 2, - alignment = {x=0, y=-1}, - offset = {x=40, y=20}, + offset = {x=140, y=20}, + alignment = {x=-1, y=1}, }, } diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 91167e0ac..0b6cdb74a 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -236,7 +236,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect& rect, // NOTE: selectitem = 0 -> no selected; selectitem is 1-based // mainlist can be NULL, but draw the frame anyway. -void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount, v2f alignment, +void Hud::drawItems(v2s32 screen_pos, v2s32 screen_offset, s32 itemcount, v2f alignment, s32 inv_offset, InventoryList *mainlist, u16 selectitem, u16 direction, bool is_hotbar) { @@ -249,11 +249,11 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount, v2f width = tmp; } - // Position of upper left corner of bar + // Position: screen_pos + screen_offset + alignment v2s32 pos(screen_offset.X * m_scale_factor, screen_offset.Y * m_scale_factor); - pos += upperleftpos; - pos.X += alignment.X * width; - pos.Y += alignment.Y * height; + pos += screen_pos; + pos.X += (alignment.X - 1.0f) * (width * 0.5f); + pos.Y += (alignment.Y - 1.0f) * (height * 0.5f); // Store hotbar_image in member variable, used by drawItem() if (hotbar_image != player->hotbar_image) { diff --git a/src/client/hud.h b/src/client/hud.h index d1191464d..6bccf0b13 100644 --- a/src/client/hud.h +++ b/src/client/hud.h @@ -100,7 +100,7 @@ private: const std::string &texture, const std::string& bgtexture, s32 count, s32 maxcount, v2s32 offset, v2s32 size = v2s32()); - void drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount, v2f alignment, + void drawItems(v2s32 screen_pos, v2s32 screen_offset, s32 itemcount, v2f alignment, s32 inv_offset, InventoryList *mainlist, u16 selectitem, u16 direction, bool is_hotbar); diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index de4f2fe02..6f138d080 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -2303,7 +2303,10 @@ void read_hud_element(lua_State *L, HudElement *elem) elem->dir = getintfield_default(L, 2, "dir", 0); lua_getfield(L, 2, "alignment"); - elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f(); + if (lua_istable(L, -1)) + elem->align = read_v2f(L, -1); + else + elem->align = elem->type == HUD_ELEM_INVENTORY ? v2f(1.0f, 1.0f) : v2f(); lua_pop(L, 1); lua_getfield(L, 2, "offset");