scoreboard_lib/api.lua

361 lines
8.5 KiB
Lua
Raw Normal View History

2020-07-27 17:20:50 -07:00
panel_lib.panels = {} -- KEY: p_name; VALUE: {{"panel name" = panel}, {"panel name 2" = panel2}, ...}
2020-04-14 04:26:33 -07:00
local function clone_table() end
2020-11-28 04:33:59 -08:00
local function add_sub_elem() end
2020-11-28 15:06:42 -08:00
local function update_sub_elems() end
2020-04-23 05:54:16 -07:00
Panel = {
name = "",
2020-04-23 05:54:16 -07:00
-- player to show the panel to
player_name = "",
2020-04-21 14:01:14 -07:00
-- ids of the hud of the player
hud_id = {},
-- text values of the huds, to retrieve on :show()
hud_text = {},
2020-04-23 05:54:16 -07:00
-- because the panel is composed by a background and a text we need to
2020-04-14 04:26:33 -07:00
-- define the two HUD to use later
background_def = {
hud_elem_type = "image",
position = { x = 1, y = 0.5 },
scale = { x = 1, y = 1 },
alignment = { x = -1, y = 0 },
2020-04-23 05:54:16 -07:00
text = "panel_bg.png",
2020-04-14 04:26:33 -07:00
},
2020-04-23 02:54:46 -07:00
title_def = {
2020-04-14 04:26:33 -07:00
hud_elem_type = "text",
position = {x = 1, y = 0.5},
2020-04-23 02:54:46 -07:00
alignment = {x = 0, y = 0},
offset = {x = 0, y = 0},
size = { x = 1 },
number = 0xFFFFFF,
2020-11-28 04:36:18 -08:00
text = ""
},
sub_img_elems = {},
sub_txt_elems = {},
visible = true
2020-04-14 04:26:33 -07:00
}
2020-04-23 02:54:46 -07:00
function Panel:new(name, def)
2020-04-23 05:54:16 -07:00
local panel = {}
local metapanel = clone_table(Panel)
2020-04-14 04:26:33 -07:00
setmetatable(panel, metapanel)
metapanel.__index = metapanel
--v------------------ LEGACY UPDATE, to remove in 3.0 -------------------v
if type(name) == "table" and def == nil then
minetest.log("warning", "[PANEL_LIB] Panel declaration deprecated. Please put the panel name outside of the def table as it follows -> Panel:new(name, def)")
def = clone_table(name)
name = def.name
end
2020-11-28 15:06:42 -08:00
if def.is_shown ~= nil then
minetest.log("warning", "[PANEL_LIB] `is_shown` is deprecated. Use `visible` instead")
def.visible = def.is_shown
end
--^------------------ LEGACY UPDATE, to remove in 3.0 -------------------^
if not name or type(name) ~= "string" then
minetest.log("warning", "[PANEL_LIB] Can't create a panel without a proper name, aborting")
return
else
panel.name = name
end
if def.position then
2020-04-23 05:54:16 -07:00
panel.background_def.position = def.position
panel.title_def.position = def.position
2020-04-23 02:54:46 -07:00
end
if def.alignment then
2020-04-23 05:54:16 -07:00
panel.background_def.alignment = def.alignment
2020-04-23 02:54:46 -07:00
end
if def.bg then
2020-04-23 05:54:16 -07:00
panel.background_def.text = def.bg
2020-04-14 04:26:33 -07:00
end
if def.bg_scale then
2020-04-23 05:54:16 -07:00
panel.background_def.scale = def.bg_scale
end
2020-04-23 02:54:46 -07:00
if def.title then
2020-04-23 05:54:16 -07:00
panel.title_def.text = def.title
2020-04-14 04:26:33 -07:00
end
2020-04-23 02:54:46 -07:00
if def.title_alignment then
2020-04-23 05:54:16 -07:00
panel.title_def.alignment = def.title_alignment
2020-04-14 04:26:33 -07:00
end
2020-04-23 02:54:46 -07:00
if def.title_offset then
2020-04-23 05:54:16 -07:00
panel.title_def.offset = def.title_offset
2020-04-23 02:54:46 -07:00
end
if def.title_color then
2020-04-23 05:54:16 -07:00
panel.title_def.number = def.title_color
2020-04-14 04:26:33 -07:00
end
if def.title_size then
panel.title_def.size = def.title_size
end
if def.player then
2020-04-23 05:54:16 -07:00
panel.player_name = def.player
end
2020-11-28 15:06:42 -08:00
if def.visible ~= nil and type(def.visible) == "boolean" then
panel.visible = def.visible
end
panel.hud_text.bg_hud_txt = panel.background_def.text
panel.hud_text.text_hud_txt = panel.title_def.text
-- se il pannello non è mostrato di base, svuoto sfondo e titolo
2020-11-28 15:06:42 -08:00
if panel.visible == false then
panel.background_def.text = ""
panel.title_def.text = ""
end
local player = minetest.get_player_by_name(def.player)
-- assegno sfondo e titolo
panel.hud_id.bg_hud_id = player:hud_add(panel.background_def)
panel.hud_id.text_hud_id = player:hud_add(panel.title_def)
2020-04-23 02:54:46 -07:00
-- controllo sottoelementi
if def.sub_img_elems then
for subname, HUD_elem in pairs(def.sub_img_elems) do
add_sub_elem(panel, "image", subname, HUD_elem)
end
end
if def.sub_txt_elems then
for subname, HUD_elem in pairs(def.sub_txt_elems) do
add_sub_elem(panel, "text", subname, HUD_elem)
end
2020-04-14 04:26:33 -07:00
end
2020-04-23 02:54:46 -07:00
-- salvo in memoria
2020-07-27 17:20:50 -07:00
if not panel_lib.panels[def.player] then
panel_lib.panels[def.player] = {}
end
panel_lib.panels[def.player][name] = panel
2020-07-27 17:20:50 -07:00
2020-04-23 05:54:16 -07:00
return panel
2020-04-14 04:26:33 -07:00
end
2020-04-23 05:54:16 -07:00
function Panel:show()
local player = minetest.get_player_by_name(self.player_name)
if self.hud_text.bg_hud_txt ~= "" then
player:hud_change(self.hud_id.bg_hud_id, "text", self.hud_text.bg_hud_txt)
end
if self.hud_text.text_hud_txt ~= "" then
player:hud_change(self.hud_id.text_hud_id, "text", self.hud_text.text_hud_txt)
end
--check for custom elements
for _, name in pairs(self.sub_img_elems) do
player:hud_change(self.hud_id[name], "text", self.hud_text[name])
end
for _, name in pairs(self.sub_txt_elems) do
player:hud_change(self.hud_id[name], "text", self.hud_text[name])
end
2020-07-27 17:20:50 -07:00
self.visible = true
2020-04-14 04:26:33 -07:00
end
2020-04-23 05:54:16 -07:00
function Panel:hide()
if (self.hud_id) then
2020-04-23 02:54:46 -07:00
local player = minetest.get_player_by_name(self.player_name)
for k, v in pairs(self.hud_id) do
player:hud_change(self.hud_id[k], "text", "")
2020-04-14 04:26:33 -07:00
end
end
2020-07-27 17:20:50 -07:00
self.visible = false
end
2020-04-23 05:54:16 -07:00
function Panel:update(def, txt_elems, img_elems)
2020-04-21 14:01:14 -07:00
if def ~= nil then
for k, v in pairs(def) do
self[k] = v
end
end
if txt_elems ~= nil then
2020-11-28 15:06:42 -08:00
update_sub_elems(self, txt_elems)
2020-04-21 14:01:14 -07:00
end
2020-04-22 07:06:02 -07:00
if img_elems ~= nil then
2020-11-28 15:06:42 -08:00
update_sub_elems(self, img_elems)
2020-04-22 07:06:02 -07:00
end
2020-04-21 14:01:14 -07:00
end
2020-04-23 05:54:16 -07:00
function Panel:remove()
panel_lib.panels[self.player_name][self.name] = nil
local player = minetest.get_player_by_name(self.player_name)
for k, v in pairs(self.hud_id) do
player:hud_remove(self.hud_id[k])
end
2020-04-23 02:54:46 -07:00
self = nil
end
2020-04-21 14:01:14 -07:00
function Panel:is_visible()
return self.visible
2020-11-28 04:33:59 -08:00
end
function Panel:add_sub_elem(type, name, HUD_elem)
add_sub_elem(self, type, name, HUD_elem)
end
function Panel:remove_sub_elem(name)
self.sub_txt_elems[name] = nil
self.sub_img_elems[name] = nil
if self.visible then
2020-11-28 04:33:59 -08:00
minetest.get_player_by_name(self.player_name):hud_remove(self.hud_id[name])
end
self.hud_id[name] = nil
self.hud_text[name] = nil
self[name] = nil
end
2020-04-21 14:01:14 -07:00
----------------------------------------------
-----------------GETTERS----------------------
----------------------------------------------
2020-07-27 17:20:50 -07:00
function panel_lib.get_panel(p_name, panel_name)
return panel_lib.panels[p_name][panel_name]
2020-04-21 14:01:14 -07:00
end
2020-04-21 14:01:14 -07:00
----------------------------------------------
---------------FUNZIONI LOCALI----------------
----------------------------------------------
-- code from => http://lua-users.org/wiki/CopyTable
function clone_table(orig)
2020-04-23 02:54:46 -07:00
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[clone_table(orig_key)] = clone_table(orig_value)
2020-04-23 02:54:46 -07:00
end
setmetatable(copy, clone_table(getmetatable(orig)))
2020-04-23 02:54:46 -07:00
else -- number, string, boolean, etc
copy = orig
end
return copy
end
2020-11-28 04:33:59 -08:00
function add_sub_elem(panel, type, name, HUD_elem)
local sub_elems
local mould
if type == "text" then
sub_elems = panel.sub_txt_elems
mould = panel.title_def
elseif type == "image" then
sub_elems = panel.sub_img_elems
mould = panel.background_def
else
error("[PANEL_LIB] type must be either 'text' or 'image'!")
end
if panel[name] then
minetest.log("error", "[PANEL_LIB] Attempt to create a sub element with the same name of another. Aborting")
return end
2020-11-28 04:33:59 -08:00
sub_elems[#sub_elems +1] = name
panel[name] = clone_table(mould)
for k, v in pairs(HUD_elem) do
panel[name][k] = v
end
-- mantengo la stessa posizione del corpo del panel, costringendo
-- l'utente a modificare gli offset se vuole spostare gli elementi
panel[name].position = mould.position
-- mostro l'elemento se il pannello era già visibile
2020-11-28 15:06:42 -08:00
panel[name].text = panel:is_visible() and panel[name].text or ""
panel.hud_id[name] = minetest.get_player_by_name(panel.player_name):hud_add(panel[name])
panel.hud_text[name] = HUD_elem.text
2020-11-28 04:33:59 -08:00
end
2020-11-28 15:06:42 -08:00
function update_sub_elems(panel, elems)
local player = minetest.get_player_by_name(panel.player_name)
for elem, _ in pairs(elems) do
for k, v in pairs(elems[elem]) do
panel[elem][k] = v
if k == "text" then
panel.hud_text[elem] = v
-- aggiorno il testo solo se visibile
if panel:is_visible() then
player:hud_change(panel.hud_id[elem], k, v)
end
else
player:hud_change(panel.hud_id[elem], k, v)
end
end
end
end
----------------------------------------------
------------------DEPRECATED------------------
----------------------------------------------
-- to remove in 3.0
function Panel:is_shown()
minetest.log("warning", "[PANEL_LIB] `is_shown()` is deprecated. Use `is_visible()` instead")
return self:is_visible()
end