Improve the HUD API
This commit is contained in:
parent
27ceddc136
commit
87586ae912
@ -33,7 +33,7 @@ end
|
|||||||
|
|
||||||
function hud.register(name, def)
|
function hud.register(name, def)
|
||||||
if not name or not def then
|
if not name or not def then
|
||||||
throw_error("not enough parameters given")
|
throw_error("Not enough parameters given")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -43,8 +43,8 @@ function hud.register(name, def)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- actually register
|
-- actually register
|
||||||
-- add background first since draworder is based on id :\
|
-- add background first since draworder is based on id
|
||||||
if --[[def.hud_elem_type == "statbar" and]] def.background ~= nil then
|
if def.hud_elem_type == "statbar" and def.background ~= nil then
|
||||||
sb_bg[name] = table.copy(def)
|
sb_bg[name] = table.copy(def)
|
||||||
sb_bg[name].text = def.background
|
sb_bg[name].text = def.background
|
||||||
if not def.autohide_bg and def.max then
|
if not def.autohide_bg and def.max then
|
||||||
@ -54,7 +54,6 @@ function hud.register(name, def)
|
|||||||
-- add item itself
|
-- add item itself
|
||||||
items[name] = def
|
items[name] = def
|
||||||
|
|
||||||
-- no error so far, return success
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -63,63 +62,44 @@ function hud.change_item(player, name, def)
|
|||||||
throw_error("Not enough parameters given to change HUD item")
|
throw_error("Not enough parameters given to change HUD item")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local i_name = player:get_player_name().."_"..name
|
|
||||||
|
local i_name = player:get_player_name() .. "_" .. name
|
||||||
local elem = hud_id[i_name]
|
local elem = hud_id[i_name]
|
||||||
|
|
||||||
if not elem then
|
if not elem then
|
||||||
throw_error("Given HUD element " .. dump(name) .. " does not exist")
|
throw_error("Given HUD element " .. dump(name) .. " does not exist")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Only update if values supported and value actually changed
|
if def.number then
|
||||||
-- update supported values (currently number and text only)
|
if not def.hud_elem_type == "statbar" then
|
||||||
if def.number and elem.number then
|
throw_error("Attempted to change an statbar HUD parameter for text HUD element " .. dump(name))
|
||||||
if def.number ~= elem.number then
|
return false
|
||||||
if elem.max and def.number > elem.max and not def.max then
|
|
||||||
def.number = elem.max
|
|
||||||
end
|
|
||||||
if def.max then
|
|
||||||
elem.max = def.max
|
|
||||||
end
|
|
||||||
player:hud_change(elem.id, "number", def.number)
|
|
||||||
elem.number = def.number
|
|
||||||
-- hide background when set
|
|
||||||
local bg = hud_id[i_name.."_bg"]
|
|
||||||
if elem.autohide_bg then
|
|
||||||
if def.number < 1 then
|
|
||||||
player:hud_change(bg.id, "number", 0)
|
|
||||||
else
|
|
||||||
local num = bg.number
|
|
||||||
if bg.max then
|
|
||||||
num = bg.max
|
|
||||||
end
|
|
||||||
player:hud_change(bg.id, "number", num)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if bg and bg.max and bg.max < 1 and def.max and def.max > bg.max then
|
|
||||||
player:hud_change(bg.id, "number", def.max)
|
|
||||||
bg.max = def.max
|
|
||||||
bg.number = def.max
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if def.text and elem.text then
|
|
||||||
if def.text ~= elem.text then
|
|
||||||
player:hud_change(elem.id, "text", def.text)
|
|
||||||
elem.text = def.text
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if def.offset and elem.offset then
|
if elem.max and def.number > elem.max then
|
||||||
if def.item_name and def.offset == "item" then
|
def.number = elem.max
|
||||||
-- for legacy reasons
|
|
||||||
--[[if def.item_name then
|
|
||||||
hud.swap_statbar(player, name, def.item_name)
|
|
||||||
end]]
|
|
||||||
else
|
|
||||||
player:hud_change(elem.id, "offset", def.offset)
|
|
||||||
elem.offset = def.offset
|
|
||||||
end
|
end
|
||||||
|
player:hud_change(elem.id, "number", def.number)
|
||||||
|
|
||||||
|
-- hide background when set
|
||||||
|
if elem.autohide_bg then
|
||||||
|
local bg = hud_id[i_name .. "_bg"]
|
||||||
|
if not bg then
|
||||||
|
throw_error("Given HUD element " .. dump(name) .. "_bg does not exist")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local num = bg.max or bg.number
|
||||||
|
if def.number == 0 then
|
||||||
|
num = 0
|
||||||
|
end
|
||||||
|
player:hud_change(bg.id, "number", num)
|
||||||
|
end
|
||||||
|
elseif def.text then
|
||||||
|
player:hud_change(elem.id, "text", def.text)
|
||||||
|
elseif def.offset then
|
||||||
|
player:hud_change(elem.id, "offset", def.offset)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -130,11 +110,13 @@ function hud.remove_item(player, name)
|
|||||||
throw_error("Not enough parameters given")
|
throw_error("Not enough parameters given")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local i_name = player:get_player_name().."_"..name
|
|
||||||
|
local i_name = player:get_player_name() .. "_" .. name
|
||||||
if hud_id[i_name] == nil then
|
if hud_id[i_name] == nil then
|
||||||
throw_error("Given HUD element " .. dump(name) .. " does not exist")
|
throw_error("Given HUD element " .. dump(name) .. " does not exist")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
player:hud_remove(hud_id[i_name].id)
|
player:hud_remove(hud_id[i_name].id)
|
||||||
hud_id[i_name] = nil
|
hud_id[i_name] = nil
|
||||||
|
|
||||||
@ -148,21 +130,22 @@ end
|
|||||||
-- Following code is placed here to keep HUD ids internal
|
-- Following code is placed here to keep HUD ids internal
|
||||||
local function add_hud_item(player, name, def)
|
local function add_hud_item(player, name, def)
|
||||||
if not player or not name or not def then
|
if not player or not name or not def then
|
||||||
throw_error("not enough parameters given")
|
throw_error("Not enough parameters given")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local i_name = player:get_player_name().."_"..name
|
|
||||||
|
local i_name = player:get_player_name() .. "_" .. name
|
||||||
hud_id[i_name] = def
|
hud_id[i_name] = def
|
||||||
hud_id[i_name].id = player:hud_add(def)
|
hud_id[i_name].id = player:hud_add(def)
|
||||||
end
|
end
|
||||||
|
|
||||||
core.register_on_joinplayer(function(player)
|
core.register_on_joinplayer(function(player)
|
||||||
-- add the backgrounds for statbars
|
-- add the backgrounds for statbars
|
||||||
for _,item in pairs(sb_bg) do
|
for _, item in pairs(sb_bg) do
|
||||||
add_hud_item(player, _.."_bg", item)
|
add_hud_item(player, _ .. "_bg", item)
|
||||||
end
|
end
|
||||||
-- and finally the actual HUD items
|
-- and finally the actual HUD items
|
||||||
for _,item in pairs(items) do
|
for _, item in pairs(items) do
|
||||||
add_hud_item(player, _, item)
|
add_hud_item(player, _, item)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user