fix: Debug HUD toggle is now LIVE — flipping the checkbox mid-session recreates the player HUD on the spot (NumericUI recreate_hud pattern, pcall-guarded, 0.5s debounce); UIHud:init hook also strips the element when setting is off so recreate removes it; inject log now reports ADDED
This commit is contained in:
@@ -18,33 +18,82 @@ mod:add_require_path(ELEMENT_FILENAME)
|
|||||||
|
|
||||||
mod:hook("UIHud", "init",
|
mod:hook("UIHud", "init",
|
||||||
function(func, self, elements, visibility_groups, params)
|
function(func, self, elements, visibility_groups, params)
|
||||||
if mod_settings.debug_hud and elements then
|
if elements then
|
||||||
local already = false
|
local idx = nil
|
||||||
for i = 1, #elements do
|
for i = 1, #elements do
|
||||||
if elements[i].class_name == ELEMENT_CLASS_NAME then
|
if elements[i].class_name == ELEMENT_CLASS_NAME then
|
||||||
already = true
|
idx = i
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not already then
|
if mod_settings.debug_hud and not idx then
|
||||||
elements[#elements + 1] = {
|
elements[#elements + 1] = {
|
||||||
class_name = ELEMENT_CLASS_NAME,
|
class_name = ELEMENT_CLASS_NAME,
|
||||||
filename = ELEMENT_FILENAME,
|
filename = ELEMENT_FILENAME,
|
||||||
use_hud_scale = true,
|
use_hud_scale = true,
|
||||||
visibility_groups = { "alive", "dead" },
|
visibility_groups = { "alive", "dead" },
|
||||||
}
|
}
|
||||||
end
|
|
||||||
if not mod.debug_hud_inject_logged then
|
if not mod.debug_hud_inject_logged then
|
||||||
mod.debug_hud_inject_logged = true
|
mod.debug_hud_inject_logged = true
|
||||||
mod:diag("OMNI-HUD inject: setting=on already=", tostring(already))
|
mod:diag("OMNI-HUD inject: ADDED")
|
||||||
|
end
|
||||||
|
elseif not mod_settings.debug_hud and idx then
|
||||||
|
table.remove(elements, idx)
|
||||||
end
|
end
|
||||||
elseif not mod.debug_hud_inject_logged then
|
|
||||||
mod.debug_hud_inject_logged = true
|
|
||||||
mod:diag("OMNI-HUD inject: setting=off, skipped")
|
|
||||||
end
|
end
|
||||||
return func(self, elements, visibility_groups, params)
|
return func(self, elements, visibility_groups, params)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- live toggling: UIHud init only runs on HUD creation, so when the setting flips we
|
||||||
|
-- rebuild the HUD on the spot (pattern borrowed from NumericUI's recreate_hud)
|
||||||
|
local recreate_timer = 0
|
||||||
|
local last_debug_hud = nil
|
||||||
|
|
||||||
|
mod.update = function (dt)
|
||||||
|
local want = mod_settings.debug_hud and true or false
|
||||||
|
if last_debug_hud == nil then
|
||||||
|
last_debug_hud = want
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if want ~= last_debug_hud then
|
||||||
|
last_debug_hud = want
|
||||||
|
recreate_timer = 0.5
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if recreate_timer <= 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
recreate_timer = recreate_timer - dt
|
||||||
|
if recreate_timer > 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, err = pcall(function()
|
||||||
|
local ui_manager = Managers.ui
|
||||||
|
local hud = ui_manager and ui_manager._hud
|
||||||
|
if not hud or not hud._element_definitions or not hud._visibility_groups then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local player = Managers.player and Managers.player:local_player(1)
|
||||||
|
if not player then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local elements = hud._element_definitions
|
||||||
|
local visibility_groups = hud._visibility_groups
|
||||||
|
hud:destroy()
|
||||||
|
ui_manager:create_player_hud(player:peer_id(), player:local_player_id(), elements, visibility_groups)
|
||||||
|
mod:diag("OMNI-HUD: live recreate, debug_hud ->", tostring(want))
|
||||||
|
end)
|
||||||
|
if not ok then
|
||||||
|
mod:diag("OMNI-HUD: live recreate FAILED:", tostring(err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function unit_breed_name(unit)
|
local function unit_breed_name(unit)
|
||||||
if not unit then
|
if not unit then
|
||||||
return "-"
|
return "-"
|
||||||
|
|||||||
Reference in New Issue
Block a user