Update hud.lua

This commit is contained in:
Federico Cecchetto 2022-06-02 17:07:43 +02:00
parent 7ed826d244
commit a8a63efbee

View File

@ -4,157 +4,195 @@ local textheight = 13
local textoffsety = barheight / 2 - textheight / 2 local textoffsety = barheight / 2 - textheight / 2
function createinfobar() function createinfobar()
local infobar = LUI.UIElement.new({ local infobar = LUI.UIElement.new({
left = GameX.IsHardcoreMode() and 0 or 213, left = 213,
top = GameX.IsHardcoreMode() and 0 or -6, top = -6,
height = barheight, height = barheight,
width = 70, width = 70,
leftAnchor = true, leftAnchor = true,
topAnchor = true topAnchor = true
}) })
infobar:registerAnimationState("hud_on", { infobar:registerAnimationState("minimap_on", {
alpha = 1 left = 213,
}) top = -6,
height = barheight,
width = 70,
leftAnchor = true,
topAnchor = true
})
infobar:registerAnimationState("hud_off", { infobar:registerAnimationState("minimap_off", {
alpha = 0 left = 0,
}) top = 0,
height = barheight,
width = 70,
leftAnchor = true,
topAnchor = true
})
return infobar infobar:registerAnimationState("hud_on", {
alpha = 1
})
infobar:registerAnimationState("hud_off", {
alpha = 0
})
return infobar
end
function updateinfobarvisibility()
local root = Engine.GetLuiRoot()
local menus = root:AnyActiveMenusInStack()
local infobar = root.infobar
if (not infobar) then
return
end
if (menus or Game.InKillCam()) then
infobar:animateToState("hud_off")
else
infobar:animateToState("hud_on")
end
local validstates = {
"hud_on",
"active",
"nosignal",
"scrambled"
}
infobar:animateToState("minimap_off")
for i = 1, #validstates do
if (validstates[i] == root.hud.minimap.current_state) then
infobar:animateToState("minimap_on")
break
end
end
end end
function populateinfobar(infobar) function populateinfobar(infobar)
elementoffset = 0 elementoffset = 0
if (Engine.GetDvarBool("cg_infobar_fps")) then if (Engine.GetDvarBool("cg_infobar_fps")) then
infobar:addElement(infoelement({ infobar:addElement(infoelement({
label = "FPS: ", label = "FPS: ",
getvalue = function() getvalue = function()
return game:getfps() return game:getfps()
end, end,
width = 70, width = 70,
interval = 100 interval = 100
})) }))
end end
if (Engine.GetDvarBool("cg_infobar_ping")) then if (Engine.GetDvarBool("cg_infobar_ping")) then
infobar:addElement(infoelement({ infobar:addElement(infoelement({
label = "Latency: ", label = "Latency: ",
getvalue = function() getvalue = function()
return game:getping() .. " ms" return game:getping() .. " ms"
end, end,
width = 115, width = 115,
interval = 100 interval = 100
})) }))
end end
updateinfobarvisibility()
end end
function infoelement(data) function infoelement(data)
local container = LUI.UIElement.new({ local container = LUI.UIElement.new({
bottomAnchor = true, bottomAnchor = true,
leftAnchor = true, leftAnchor = true,
topAnchor = true, topAnchor = true,
width = data.width, width = data.width,
left = elementoffset left = elementoffset
}) })
elementoffset = elementoffset + data.width + 10 elementoffset = elementoffset + data.width + 10
local background = LUI.UIImage.new({ local background = LUI.UIImage.new({
bottomAnchor = true, bottomAnchor = true,
leftAnchor = true, leftAnchor = true,
topAnchor = true, topAnchor = true,
rightAnchor = true, rightAnchor = true,
material = luiglobals.RegisterMaterial("white"), material = luiglobals.RegisterMaterial("white"),
color = luiglobals.Colors.black, color = luiglobals.Colors.black,
alpha = 0.5 alpha = 0.5
}) })
local labelfont = CoD.TextSettings.FontBold110 local labelfont = CoD.TextSettings.FontBold110
local label = LUI.UIText.new({ local label = LUI.UIText.new({
left = 5, left = 5,
top = textoffsety, top = textoffsety,
font = labelfont.Font, font = labelfont.Font,
height = textheight, height = textheight,
leftAnchor = true, leftAnchor = true,
topAnchor = true, topAnchor = true,
color = { color = {
r = 0.8, r = 0.8,
g = 0.8, g = 0.8,
b = 0.8 b = 0.8
} }
}) })
label:setText(data.label) label:setText(data.label)
local _, _, left = luiglobals.GetTextDimensions(data.label, labelfont.Font, textheight) local _, _, left = luiglobals.GetTextDimensions(data.label, labelfont.Font, textheight)
local value = LUI.UIText.new({ local value = LUI.UIText.new({
left = left + 5, left = left + 5,
top = textoffsety, top = textoffsety,
font = labelfont.Font, font = labelfont.Font,
height = textheight, height = textheight,
leftAnchor = true, leftAnchor = true,
topAnchor = true, topAnchor = true,
color = { color = {
r = 0.6, r = 0.6,
g = 0.6, g = 0.6,
b = 0.6 b = 0.6
} }
}) })
value:addElement(LUI.UITimer.new(data.interval, "update")) value:addElement(LUI.UITimer.new(data.interval, "update"))
value:setText(data.getvalue()) value:setText(data.getvalue())
value:addEventHandler("update", function() value:addEventHandler("update", function()
value:setText(data.getvalue()) value:setText(data.getvalue())
end) end)
container:addElement(background) container:addElement(background)
container:addElement(label) container:addElement(label)
container:addElement(value) container:addElement(value)
return container return container
end end
local updatehudvisibility = mphud.updateHudVisibility local updatehudvisibility = mphud.updateHudVisibility
mphud.updateHudVisibility = function(a1, a2) mphud.updateHudVisibility = function(a1, a2)
updatehudvisibility(a1, a2) updatehudvisibility(a1, a2)
updateinfobarvisibility()
local root = Engine.GetLuiRoot()
local menus = root:AnyActiveMenusInStack()
local infobar = root.infobar
if (not infobar) then
return
end
if (menus) then
infobar:animateToState("hud_off")
else
infobar:animateToState("hud_on")
end
end end
LUI.onmenuopen("mp_hud", function(hud) LUI.onmenuopen("mp_hud", function(hud)
if (Engine.InFrontend()) then if (Engine.InFrontend()) then
return return
end end
local infobar = createinfobar() local infobar = createinfobar()
local root = Engine.GetLuiRoot() local root = Engine.GetLuiRoot()
root.infobar = infobar root.infobar = infobar
populateinfobar(infobar) root.hud = hud
populateinfobar(infobar)
root:registerEventHandler("update_hud_infobar_settings", function() root:registerEventHandler("update_hud_infobar_settings", function()
infobar:removeAllChildren() infobar:removeAllChildren()
populateinfobar(infobar) populateinfobar(infobar)
end) end)
root:processEvent({ root:processEvent({
name = "update_hud_infobar_settings" name = "update_hud_infobar_settings"
}) })
hud.static.scalable:addElement(infobar) hud.static.scalable:addElement(infobar)
end) end)