Add window_position, window_padding, and window_anchor

Now flow can actually be used for HUDs
This commit is contained in:
luk3yx 2025-02-21 18:38:25 +13:00
parent e3dc6d6750
commit 8f3c3b1deb
2 changed files with 85 additions and 30 deletions

View File

@ -542,7 +542,31 @@ end)
You can set `bgcolor = "#123"`, `fbgcolor = "#123"`, and
`bg_fullscreen = true` on the root element to set a background colour. The
values for these correspond to the [`bgcolor` formspec element](https://minetest.gitlab.io/minetest/formspec/#bgcolorbgcolorfullscreenfbgcolor).
values for these correspond to the [`bgcolor` formspec element](https://api.luanti.org/formspec/#bgcolorbgcolorfullscreenfbgcolor).
</details><details>
<summary><b><code>position[]</code> etc</b></summary>
These values allow the position of the displayed form to be moved around.
See [the formspec documentation](https://api.luanti.org/formspec/#positionxy)
for more information. They are especially useful when creating HUDs using flow.
Example:
```lua
local my_gui = flow.make_gui(function(player, ctx)
return gui.VBox{
-- 0 is the start/top, 1 is the bottom/end
window_position = {x = 0, y = 0},
window_anchor = {x = 1, y = 1},
-- Equivalent to padding[0.1,0.2]
window_padding = {x = 0.1, y = 0.2},
gui.Label{label = "Hello world"},
}
end)
```
</details><details>
<summary><b>Rendering to a formspec</b></summary>

View File

@ -46,38 +46,64 @@ local function render_ast(node, embedded)
{type = "size", w = w, h = h},
}
-- TODO: Consider a nicer place to put these parameters
if node.no_prepend and not embedded then
res[#res + 1] = {type = "no_prepend"}
end
if node.fbgcolor or node.bgcolor or node.bg_fullscreen ~= nil then
-- Hack to prevent breaking mods that rely on the old (broken)
-- behaviour of fbgcolor
if node.fbgcolor == "#08080880" and node.bgcolor == nil and
node.bg_fullscreen == nil then
node.bg_fullscreen = true
node.fbgcolor = nil
if not embedded then
if node.window_position then
res[#res + 1] = {
type = "position",
x = node.window_position.x,
y = node.window_position.y,
}
node.window_position = nil
end
if node.window_anchor then
res[#res + 1] = {
type = "anchor",
x = node.window_anchor.x,
y = node.window_anchor.y,
}
node.window_anchor = nil
end
if node.window_padding then
res[#res + 1] = {
type = "padding",
x = node.window_padding.x,
y = node.window_padding.y,
}
node.window_padding = nil
end
if node.no_prepend then
res[#res + 1] = {type = "no_prepend"}
end
res[#res + 1] = {
type = "bgcolor",
bgcolor = node.bgcolor,
fbgcolor = node.fbgcolor,
fullscreen = node.bg_fullscreen
}
node.bgcolor = nil
node.fbgcolor = nil
node.bg_fullscreen = nil
end
if node.fbgcolor or node.bgcolor or node.bg_fullscreen ~= nil then
-- Hack to prevent breaking mods that rely on the old (broken)
-- behaviour of fbgcolor
if node.fbgcolor == "#08080880" and node.bgcolor == nil and
node.bg_fullscreen == nil then
node.bg_fullscreen = true
node.fbgcolor = nil
end
-- Add the root element's background image as a fullscreen one
if node.bgimg and not embedded then
res[#res + 1] = {
type = node.bgimg_middle and "background9" or "background",
texture_name = node.bgimg, middle_x = node.bgimg_middle,
x = 0, y = 0, w = 0, h = 0, auto_clip = true,
}
node.bgimg = nil
res[#res + 1] = {
type = "bgcolor",
bgcolor = node.bgcolor,
fbgcolor = node.fbgcolor,
fullscreen = node.bg_fullscreen
}
node.bgcolor = nil
node.fbgcolor = nil
node.bg_fullscreen = nil
end
-- Add the root element's background image as a fullscreen one
if node.bgimg then
res[#res + 1] = {
type = node.bgimg_middle and "background9" or "background",
texture_name = node.bgimg, middle_x = node.bgimg_middle,
x = 0, y = 0, w = 0, h = 0, auto_clip = true,
}
node.bgimg = nil
end
end
res[#res + 1] = node
@ -329,6 +355,11 @@ function Form:show(player, ctx)
end
function Form:show_hud(player, ctx)
if not core.global_exists("hud_fs") then
error("[flow] Form:show_hud() requires the hud_fs mod to be " ..
"installed!", 2)
end
local info = core.get_player_information(player:get_player_name())
local tree = self:_render(player, ctx or {}, nil, nil, nil,
info and info.lang_code)