From f095267be0bd6f80d16f53a7e4097adf5ff12096 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Mon, 31 May 2021 11:21:50 +1200 Subject: [PATCH] Allow using regular (non-formspec) HUD elements --- README.md | 23 +++++++++++++++++++++++ init.lua | 10 ++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dbef965..65a69d1 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,26 @@ The following elements are supported: All valid formspec elements not listed above are ignored. +### Using normal HUD element definitions + +If you want features can't be implemented using formspecs, you can use a list +of HUD elements (the tables sent to `hud_add`) instead. Example: + +```lua +hud_fs.show_hud(player, "waypoints", { + { + hud_elem_type = "waypoint", + world_pos = {x = 0, y = 0, z = 0}, + name = "Waypoint 1" + }, + { + hud_elem_type = "waypoint", + world_pos = {x = 1, y = 2, z = 3}, + name = "Waypoint 2" + } +}) +``` + ### Advanced API - `hud_fs.set_scale(formname, scale)`: Sets the scale of the HUD. @@ -92,6 +112,9 @@ Then don't use this mod. There are plenty of other HUD library mods around such as [hudlib](https://github.com/octacian/hudlib) and [panel_lib](https://gitlab.com/zughy-friends-minetest/panel_lib). +Alternatively, the API provided by this mod accepts a list of HUD elements in +place of a formspec. + ## Performance If this mod becomes a performance bottleneck you can try the following things: diff --git a/init.lua b/init.lua index abc1739..becac2f 100644 --- a/init.lua +++ b/init.lua @@ -251,9 +251,11 @@ local function render(tree, proto_ver, scale, z_index) end offset_x = -node.x * size_w offset_y = -node.y * size_h - -- return render_error('anchor[] not implemented') elseif nodes[node_type] then add_node(node_type, node) + elseif node_type == nil and node.hud_elem_type then + -- Pass through plain HUD elements + hud_elems[#hud_elems + 1] = node end end @@ -280,9 +282,9 @@ local function compare_elems(old_elem, new_elem) for k, v in pairs(old_elem) do local v2 = new_elem[k] if type(v) == "table" and type(v2) == "table" then - -- Tables are guaranteed to be 2-dimensional vectors at the moment, - -- don't bother checking anything else to improve performance. - if v.x ~= v2.x or v.y ~= v2.y then + -- Tables are guaranteed to be vectors at the moment, don't bother + -- checking anything else to improve performance. + if v.x ~= v2.x or v.y ~= v2.y or v.z ~= v2.z then differences[#differences + 1] = k end elseif v ~= v2 then