From 467d3a8c62aa39a9eda7095935de1a56c96fb708 Mon Sep 17 00:00:00 2001 From: cx384 Date: Fri, 29 Dec 2023 21:51:02 +0100 Subject: [PATCH] Rename `hud_elem_type` to `type` (#14065) --- builtin/game/features.lua | 1 + builtin/game/statbars.lua | 6 ++--- clientmods/preview/init.lua | 2 +- doc/client_lua_api.md | 4 +++- doc/lua_api.md | 11 +++++++-- .../mods/testentities/selectionbox.lua | 2 +- games/devtest/mods/testhud/init.lua | 10 ++++---- src/script/common/c_content.cpp | 24 +++++++++++++++++-- 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/builtin/game/features.lua b/builtin/game/features.lua index c76566ed3..f438743b7 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -29,6 +29,7 @@ core.features = { compress_zstd = true, sound_params_start_time = true, physics_overrides_v2 = true, + hud_def_type_field = true, } function core.has_feature(arg) diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index 78d1d2728..72b711bdc 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -3,7 +3,7 @@ local enable_damage = core.settings:get_bool("enable_damage") local bar_definitions = { hp = { - hud_elem_type = "statbar", + type = "statbar", position = {x = 0.5, y = 1}, text = "heart.png", text2 = "heart_gone.png", @@ -14,7 +14,7 @@ local bar_definitions = { offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)}, }, breath = { - hud_elem_type = "statbar", + type = "statbar", position = {x = 0.5, y = 1}, text = "bubble.png", text2 = "bubble_gone.png", @@ -139,7 +139,7 @@ end function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or - definition.hud_elem_type ~= "statbar" then + (definition.type or definition.hud_elem_type) ~= "statbar" then return false end diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index 46d59ec9a..f8d3a006b 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -33,7 +33,7 @@ end) core.after(1, function() print("armor: " .. dump(core.localplayer:get_armor_groups())) id = core.localplayer:hud_add({ - hud_elem_type = "text", + type = "text", name = "example", number = 0xff0000, position = {x=0, y=1}, diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 30c6d1f82..ef0d9e1d3 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -1332,8 +1332,10 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or ```lua { - hud_elem_type = "image", -- see HUD element types, default "text" + type = "image", -- see HUD element types, default "text" -- ^ type of HUD element, can be either of "image", "text", "statbar", or "inventory" + hud_elem_type = "image", +-- ^ Deprecated, same as `type`. In case both are specified `type` will be used. position = {x=0.5, y=0.5}, -- ^ Left corner position of element, default `{x=0,y=0}`. name = "", -- default "" diff --git a/doc/lua_api.md b/doc/lua_api.md index 4ba2e32da..7461127c2 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5278,6 +5278,8 @@ Utilities -- liquid_fluidity, liquid_fluidity_smooth, liquid_sink, -- acceleration_default, acceleration_air (5.8.0) physics_overrides_v2 = true, + -- In HUD definitions the field `type` is used and `hud_elem_type` is deprecated (5.9.0) + hud_def_type_field = true, } ``` @@ -7790,7 +7792,7 @@ child will follow movement and rotation of that bone. * `hud_change(id, stat, value)`: change a value of a previously added HUD element. * `stat` supports the same keys as in the hud definition table except for - `"hud_elem_type"`. + `"type"` (or the deprecated `"hud_elem_type"`). * `hud_get(id)`: gets the HUD element definition structure of the specified ID * `hud_set_flags(flags)`: sets specified HUD flags of player. * `flags`: A table with the following fields set to boolean values @@ -10050,9 +10052,14 @@ Used by `ObjectRef:hud_add`. Returned by `ObjectRef:hud_get`. ```lua { - hud_elem_type = "image", + type = "image", -- Type of element, can be "image", "text", "statbar", "inventory", -- "waypoint", "image_waypoint", "compass" or "minimap" + -- If undefined "text" will be used. + + hud_elem_type = "image", + -- Deprecated, same as `type`. + -- In case both are specified `type` will be used. position = {x=0.5, y=0.5}, -- Top left corner position of element diff --git a/games/devtest/mods/testentities/selectionbox.lua b/games/devtest/mods/testentities/selectionbox.lua index 36a8daa41..a455fd459 100644 --- a/games/devtest/mods/testentities/selectionbox.lua +++ b/games/devtest/mods/testentities/selectionbox.lua @@ -66,7 +66,7 @@ minetest.register_globalstep(function() local ent = pointed_thing.ref:get_luaentity() if ent and ent.name == "testentities:selectionbox" then hud_ids[pname] = hud_id or player:hud_add({ - hud_elem_type = "text", -- See HUD element types + type = "text", -- See HUD element types position = {x=0.5, y=0.5}, text = "X", number = 0xFF0000, diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua index 6b55b924b..da07f17e4 100644 --- a/games/devtest/mods/testhud/init.lua +++ b/games/devtest/mods/testhud/init.lua @@ -12,7 +12,7 @@ local font_states = { local font_default_def = { - hud_elem_type = "text", + type = "text", position = {x = 0.5, y = 0.5}, scale = {x = 2, y = 2}, alignment = { x = 0, y = 0 }, @@ -102,14 +102,14 @@ minetest.register_chatcommand("hudwaypoints", { return false end local regular = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "regular waypoint", text = "m", number = 0xFFFFFF, world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0}) } local reduced_precision = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "imprecise waypoint", text = "m (0.1 steps, precision = 10)", precision = 10, @@ -117,7 +117,7 @@ minetest.register_chatcommand("hudwaypoints", { world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0}) } local hidden_distance = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "waypoint with hidden distance", text = "this text is hidden as well (precision = 0)", precision = 0, @@ -149,7 +149,7 @@ minetest.register_chatcommand("hudwaypoints", { minetest.after(0.5, change, player) end local image_waypoint = player:hud_add { - hud_elem_type = "image_waypoint", + type = "image_waypoint", text = "testhud_waypoint.png", world_pos = player:get_pos(), scale = {x = 3, y = 3}, diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 1756c49c5..97b651ce8 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1959,8 +1959,28 @@ void push_objectRef(lua_State *L, const u16 id) void read_hud_element(lua_State *L, HudElement *elem) { - elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type", - es_HudElementType, HUD_ELEM_TEXT); + std::string type_string; + bool has_type = getstringfield(L, 2, "type", type_string); + + // Handle deprecated hud_elem_type + std::string deprecated_type_string; + if (getstringfield(L, 2, "hud_elem_type", deprecated_type_string)) { + if (has_type && deprecated_type_string != type_string) { + log_deprecated(L, "Ambiguous HUD element fields \"type\" and \"hud_elem_type\", " + "\"type\" will be used.", 1, true); + } else { + has_type = true; + type_string = deprecated_type_string; + log_deprecated(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", + 1, true); + } + } + + int type_enum; + if (has_type && string_to_enum(es_HudElementType, type_enum, type_string)) + elem->type = static_cast(type_enum); + else + elem->type = HUD_ELEM_TEXT; lua_getfield(L, 2, "position"); elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();