Make alignment behave like other elements

This commit is contained in:
cx384 2024-04-11 22:18:49 +02:00
parent 1d9c5292c5
commit d6fe4c3bbd
6 changed files with 18 additions and 15 deletions

View File

@ -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)

View File

@ -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`

View File

@ -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},
},
}

View File

@ -236,7 +236,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& 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) {

View File

@ -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);

View File

@ -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");