From 1a2a1276b357c0884bac3d97ab0c314edee28114 Mon Sep 17 00:00:00 2001 From: WillConker Date: Tue, 11 Jun 2024 20:53:01 +0100 Subject: [PATCH] Made hudbars automatically reposition --- mods/HUD/hudbars/default_settings.lua | 3 +- mods/HUD/hudbars/init.lua | 150 ++++++++++++++++++++------ mods/HUD/hudbars/settingtypes.txt | 9 -- 3 files changed, 120 insertions(+), 42 deletions(-) diff --git a/mods/HUD/hudbars/default_settings.lua b/mods/HUD/hudbars/default_settings.lua index 865a7cb6a..fcb265cab 100644 --- a/mods/HUD/hudbars/default_settings.lua +++ b/mods/HUD/hudbars/default_settings.lua @@ -13,7 +13,7 @@ hb.settings.pos_left.y = hb.load_setting("hudbars_pos_left_y", "number", 1) hb.settings.pos_right.x = hb.load_setting("hudbars_pos_right_x", "number", 0.5) hb.settings.pos_right.y = hb.load_setting("hudbars_pos_right_y", "number", 1) -- Modified in MCL2! -hb.settings.bar_type = hb.load_setting("hudbars_bar_type", "string", "statbar_modern", {"progress_bar", "statbar_classic", "statbar_modern"}) +hb.settings.bar_type = hb.load_setting("hudbars_bar_type", "string", "progress_bar", {"progress_bar", "statbar_classic", "statbar_modern"}) if hb.settings.bar_type == "progress_bar" then hb.settings.start_offset_left.x = hb.load_setting("hudbars_start_offset_left_x", "number", -175) hb.settings.start_offset_left.y = hb.load_setting("hudbars_start_offset_left_y", "number", -86) @@ -33,7 +33,6 @@ hb.settings.tick = hb.load_setting("hudbars_tick", "number", 0.1) hb.settings.forceload_default_hudbars = hb.load_setting("hudbars_forceload_default_hudbars", "bool", true) -- Misc. settings -hb.settings.alignment_pattern = hb.load_setting("hudbars_alignment_pattern", "string", "zigzag", {"zigzag", "stack_up", "stack_down"}) hb.settings.autohide_breath = hb.load_setting("hudbars_autohide_breath", "bool", true) local sorting = minetest.settings:get("hudbars_sorting") diff --git a/mods/HUD/hudbars/init.lua b/mods/HUD/hudbars/init.lua index 7f86a959d..13f29d374 100644 --- a/mods/HUD/hudbars/init.lua +++ b/mods/HUD/hudbars/init.lua @@ -13,6 +13,7 @@ hb = { hudbars_count = 0, -- table which records which HUD bar slots have been “registered” so far; used for automatic positioning registered_slots = {}, + registered_slots_reversed = {}, settings = {}, -- Table which contains all players with active default HUD bars (only for internal use) players = {}, @@ -127,41 +128,104 @@ function hb.get_hudbar_position_index(identifier) end end -function hb.register_hudbar(identifier, text_color, label, textures, direction, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config) +function hb.get_hudbar_height(identifier, player) + local hudtable = hb.hudtables[identifier] + if hudtable ~= nil then + local hudstate = hudtable.hudstate[player:get_player_name()] + if (hudstate == nil or hudstate.hidden == nil) and hudtable.start_hidden == true or (hudstate and hudstate.hidden == true) then + minetest.debug("hudbar height of", identifier, "hidden") + return 0 + end + if hudtable.resizeable == true and hb.settings.bar_type ~= "progress_bar" then + return math.ceil(hudstate.max / hb.settings.statbar_length) * hb.settings.vmargin + else + return hb.settings.vmargin + end + else + return 0 + end +end + +function hb.get_hudbar_y_offset(identifier, player) + local index = hb.registered_slots_reversed[identifier] + if index % 2 == 0 then + local y_offset = 0 + for i=0,index-1,2 do + if hb.registered_slots[i] then + minetest.debug() + y_offset = y_offset + hb.get_hudbar_height(hb.registered_slots[i], player) + minetest.debug("Get hudbar y offset for", identifier, "y_offset now", y_offset, "after checking", hb.registered_slots[i]) + end + end + return y_offset + else + local y_offset = 0 + for i=1,index-1,2 do + if hb.registered_slots[i] then + y_offset = y_offset + hb.get_hudbar_height(hb.registered_slots[i], player) + end + end + return y_offset + end +end + +function hb.is_hudbar_on_right(identifier) + local index = hb.registered_slots_reversed[identifier] + minetest.debug("Hudbar", identifier, "on right",index % 2 == 1) + return index % 2 == 1 +end + +function hb.update_hudbar_y_offset(identifier, player) + local hudtable = hb.get_hudtable(identifier) + local name = player:get_player_name() + if hudtable.hudids[name] ~= nil then + local barid = hudtable.hudids[name].bar + local new_y_offset = hb.get_hudbar_y_offset(identifier, player) + local old_offset = player:hud_get(barid).offset + minetest.debug("Update hudbar y offset for", identifier, "new offset", new_y_offset, "old x offset", old_offset.x) + if hb.is_hudbar_on_right(identifier) then + new_y_offset = hb.settings.start_offset_right.y - new_y_offset + else + new_y_offset = hb.settings.start_offset_left.y - new_y_offset + end + player:hud_change(barid, "offset", {x=old_offset.x, y=new_y_offset}) + if hb.settings.bar_type == "progress_bar" then + if hudtable.hudids[name].icon then + player:hud_change(hudtable.hudids[name].icon, "offset", {x=old_offset.x - 3, y=new_y_offset}) + end + player:hud_change(hudtable.hudids[name].bg, "offset", {x=old_offset.x - 1, y=new_y_offset - 1}) + player:hud_change(hudtable.hudids[name].text, "offset", {x=old_offset.x + 2, y=new_y_offset - 1}) + end + end +end + +function hb.update_hudbar_y_offsets(identifier, player) + local index = hb.registered_slots_reversed[identifier] + minetest.debug("Update hudbar y offsets", identifier, index) + if index % 2 == 0 then + for i=0,#hb.registered_slots,2 do + if hb.registered_slots[i] then + hb.update_hudbar_y_offset(hb.registered_slots[i], player) + end + end + else + for i=1,#hb.registered_slots,2 do + if hb.registered_slots[i] then + hb.update_hudbar_y_offset(hb.registered_slots[i], player) + end + end + end +end + +function hb.register_hudbar(identifier, text_color, label, textures, direction, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config, resizeable) minetest.log("action", "hb.register_hudbar: "..tostring(identifier)) local hudtable = {} local pos, offset local index = math.floor(hb.get_hudbar_position_index(identifier)) - hb.registered_slots[index] = true - if hb.settings.alignment_pattern == "stack_up" then - pos = hb.settings.pos_left - offset = { - x = direction == 0 and hb.settings.start_offset_left.x or -hb.settings.start_offset_right.x, - y = hb.settings.start_offset_left.y - hb.settings.vmargin * index - } - elseif hb.settings.alignment_pattern == "stack_down" then - pos = hb.settings.pos_left - offset = { - x = direction == 0 and hb.settings.start_offset_right.x or -hb.settings.start_offset_left.x, - y = hb.settings.start_offset_left.y + hb.settings.vmargin * index - } - else -- zigzag - if index % 2 == 0 then - pos = hb.settings.pos_left - offset = { - -- -(24+18) = -42. using linear eq, -42 = -258m - 24. - x = direction == 0 and hb.settings.start_offset_left.x or (-42+24)/(-258.0) * hb.settings.start_offset_left.x - 24, - y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2) - } - else - pos = hb.settings.pos_right - offset = { - -- 24*10+30 - 24 = 234. using linear eq, 234 = 16m - 24. - x = direction == 0 and hb.settings.start_offset_right.x or (234+24)/(16) * hb.settings.start_offset_right.x - 24, - y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2) - } - end - end + hb.registered_slots[index] = identifier + hb.registered_slots_reversed[identifier] = index + + if format_string == nil then format_string = N("@1: @2/@3") @@ -188,6 +252,25 @@ function hb.register_hudbar(identifier, text_color, label, textures, direction, local name = player:get_player_name() local bgscale, iconscale, text, barnumber, bgiconnumber + -- zigzag + if index % 2 == 0 then + pos = hb.settings.pos_left + offset = { + -- -(24+18) = -42. using linear eq, -42 = -258m - 24. + x = direction == 0 and hb.settings.start_offset_left.x or (-42+24)/(-258.0) * hb.settings.start_offset_left.x - 24, + y = hb.settings.start_offset_left.y - hb.get_hudbar_y_offset(identifier, player)--hb.settings.vmargin * (index/2) + } + minetest.debug(identifier, hb.get_hudbar_y_offset(identifier, player)) + else + pos = hb.settings.pos_right + offset = { + -- 24*10+30 - 24 = 234. using linear eq, 234 = 16m - 24. + x = direction == 0 and hb.settings.start_offset_right.x or (234+24)/(16) * hb.settings.start_offset_right.x - 24, + y = hb.settings.start_offset_right.y - hb.get_hudbar_y_offset(identifier, player)--hb.settings.vmargin * ((index-1)/2) + } + minetest.debug(identifier, hb.get_hudbar_y_offset(identifier, player)) + end + if start_max == 0 or start_hidden then bgscale = { x=0, y=0 } else @@ -317,6 +400,7 @@ function hb.init_hudbar(player, identifier, start_value, start_max, start_hidden if not player_exists(player) then return false end local hudtable = hb.get_hudtable(identifier) hb.hudtables[identifier].add_all(player, hudtable, start_value, start_max, start_hidden) + hb.update_hudbar_y_offsets(identifier, player) return true end @@ -417,6 +501,8 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon end end end + + hb.update_hudbar_y_offsets(identifier, player) return true end @@ -436,6 +522,7 @@ function hb.hide_hudbar(player, identifier) player:hud_change(hudtable.hudids[name].bar, "number", 0) player:hud_change(hudtable.hudids[name].bar, "item", 0) hudtable.hudstate[name].hidden = true + hb.update_hudbar_y_offsets(identifier, player) return true end @@ -461,6 +548,7 @@ function hb.unhide_hudbar(player, identifier) player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max)) player:hud_change(hudtable.hudids[name].bar, "item", hb.value_to_barlength(max, max)) hudtable.hudstate[name].hidden = false + hb.update_hudbar_y_offsets(identifier, player) return true end diff --git a/mods/HUD/hudbars/settingtypes.txt b/mods/HUD/hudbars/settingtypes.txt index 3e4390e20..16997c4ae 100644 --- a/mods/HUD/hudbars/settingtypes.txt +++ b/mods/HUD/hudbars/settingtypes.txt @@ -21,15 +21,6 @@ hudbars_bar_type (HUD bars style) enum progress_bar progress_bar,statbar_classic # after the breath has been filled up. Otherwise, the breath will always be displayed. hudbars_autohide_breath (Automatically hide breath indicators) bool true -# This setting changes the way the HUD bars are ordered on the display. You can choose -# between a zig-zag pattern (default) or a vertically stacked pattern. -# The following values are allowed: -# - zigzag: Starting from the left bottom, the next is right from the first, -# the next is above the first, the next is right of the third, etc. -# - stack_up: The HUD bars are stacked vertically, going upwards. -# - stack_down: The HUD bars are stacked vertically, going downwards. -hudbars_alignment_pattern (HUD bars alignment pattern) enum zigzag zigzag,stack_up,stack_down - # This setting allows you to specify the order of the HUD bars explicitly. If left empty # (the default), the health and breath indicators come first, additional indicators # may appear in any order. This setting is quite technical and normal users probably do not