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
2020-04-21 08:57:09 -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-21 08:57:09 -07:00
2020-04-23 05:54:16 -07:00
Panel = {
2020-08-02 16:09:18 -07:00
name = " " ,
2020-04-23 05:54:16 -07:00
-- player to show the panel to
2020-04-21 08:57:09 -07:00
player_name = " " ,
2020-04-21 14:01:14 -07:00
-- ids of the hud of the player
hud_id = { } ,
2020-11-28 08:54:21 -08:00
-- 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 " ,
2020-07-28 05:57:10 -07:00
position = { x = 1 , y = 0.5 } ,
2020-04-23 02:54:46 -07:00
alignment = { x = 0 , y = 0 } ,
2020-07-28 05:57:10 -07:00
offset = { x = 0 , y = 0 } ,
size = { x = 1 } ,
number = 0xFFFFFF ,
2020-11-28 04:36:18 -08:00
text = " "
2020-04-21 08:57:09 -07:00
} ,
2020-04-22 04:00:23 -07:00
sub_img_elems = { } ,
2020-07-27 17:31:58 -07:00
sub_txt_elems = { } ,
2020-11-28 12:41:00 -08:00
visible = true
2020-04-14 04:26:33 -07:00
}
2020-04-23 02:54:46 -07:00
2020-11-28 08:54:21 -08:00
function Panel : new ( name , def )
2020-04-23 05:54:16 -07:00
local panel = { }
2020-07-26 16:54:50 -07:00
local metapanel = clone_table ( Panel )
2020-04-14 04:26:33 -07:00
2020-07-26 16:54:50 -07:00
setmetatable ( panel , metapanel )
metapanel.__index = metapanel
2020-04-21 08:57:09 -07:00
2020-11-28 08:54:21 -08:00
--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 12:41:00 -08:00
2020-11-28 15:06:42 -08:00
if def.is_shown ~= nil then
2020-11-28 12:41:00 -08:00
minetest.log ( " warning " , " [PANEL_LIB] `is_shown` is deprecated. Use `visible` instead " )
def.visible = def.is_shown
end
2020-11-28 08:54:21 -08:00
--^------------------ LEGACY UPDATE, to remove in 3.0 -------------------^
if not name or type ( name ) ~= " string " then
2020-08-02 16:09:18 -07:00
minetest.log ( " warning " , " [PANEL_LIB] Can't create a panel without a proper name, aborting " )
return
else
2020-11-28 08:54:21 -08:00
panel.name = name
2020-08-02 16:09:18 -07:00
end
2020-04-21 08:57:09 -07:00
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
2020-04-21 08:57:09 -07:00
if def.bg_scale then
2020-04-23 05:54:16 -07:00
panel.background_def . scale = def.bg_scale
2020-04-21 08:57:09 -07:00
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-21 08:57:09 -07:00
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-21 08:57:09 -07:00
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
2020-04-21 08:57:09 -07:00
2020-07-28 05:57:10 -07:00
if def.title_size then
panel.title_def . size = def.title_size
end
2020-04-21 08:57:09 -07:00
if def.player then
2020-04-23 05:54:16 -07:00
panel.player_name = def.player
2020-04-21 08:57:09 -07:00
end
2020-11-28 15:06:42 -08:00
if def.visible ~= nil and type ( def.visible ) == " boolean " then
panel.visible = def.visible
end
2020-11-28 08:54:21 -08:00
panel.hud_text . bg_hud_txt = panel.background_def . text
panel.hud_text . text_hud_txt = panel.title_def . text
2020-11-28 12:41:00 -08:00
-- se il pannello non è mostrato di base, svuoto sfondo e titolo
2020-11-28 15:06:42 -08:00
if panel.visible == false then
2020-11-28 12:41:00 -08:00
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-11-28 08:54:21 -08:00
2020-04-23 02:54:46 -07:00
-- controllo sottoelementi
2020-04-22 04:00:23 -07:00
if def.sub_img_elems then
2020-11-28 12:41:00 -08:00
for subname , HUD_elem in pairs ( def.sub_img_elems ) do
add_sub_elem ( panel , " image " , subname , HUD_elem )
2020-04-21 08:57:09 -07:00
end
end
2020-04-22 04:00:23 -07:00
if def.sub_txt_elems then
2020-11-28 12:41:00 -08:00
for subname , HUD_elem in pairs ( def.sub_txt_elems ) do
add_sub_elem ( panel , " text " , subname , HUD_elem )
2020-04-21 08:57:09 -07:00
end
2020-04-14 04:26:33 -07:00
end
2020-04-21 08:57:09 -07:00
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
2020-11-28 08:54:21 -08:00
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-21 08:57:09 -07:00
2020-04-23 05:54:16 -07:00
function Panel : show ( )
2020-04-21 08:57:09 -07:00
local player = minetest.get_player_by_name ( self.player_name )
2020-11-28 08:54:21 -08:00
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
2020-04-22 04:00:23 -07:00
2020-04-21 08:57:09 -07:00
--check for custom elements
2020-04-22 04:00:23 -07:00
for _ , name in pairs ( self.sub_img_elems ) do
2020-11-28 08:54:21 -08:00
player : hud_change ( self.hud_id [ name ] , " text " , self.hud_text [ name ] )
2020-04-22 04:00:23 -07:00
end
2020-04-21 08:57:09 -07:00
for _ , name in pairs ( self.sub_txt_elems ) do
2020-11-28 08:54:21 -08:00
player : hud_change ( self.hud_id [ name ] , " text " , self.hud_text [ name ] )
2020-04-21 08:57:09 -07:00
end
2020-07-27 17:20:50 -07:00
2020-11-28 12:41:00 -08:00
self.visible = true
2020-04-14 04:26:33 -07:00
end
2020-04-21 08:57:09 -07:00
2020-04-23 05:54:16 -07:00
function Panel : hide ( )
2020-04-21 08:57:09 -07:00
if ( self.hud_id ) then
2020-04-23 02:54:46 -07:00
local player = minetest.get_player_by_name ( self.player_name )
2020-04-21 08:57:09 -07:00
for k , v in pairs ( self.hud_id ) do
2020-11-28 08:54:21 -08:00
player : hud_change ( self.hud_id [ k ] , " text " , " " )
2020-04-14 04:26:33 -07:00
end
2020-04-21 08:57:09 -07:00
end
2020-07-27 17:20:50 -07:00
2020-11-28 12:41:00 -08:00
self.visible = false
2020-04-21 08:57:09 -07:00
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 ( )
2020-08-02 16:09:18 -07:00
panel_lib.panels [ self.player_name ] [ self.name ] = nil
2020-11-28 08:54:21 -08:00
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
2020-11-28 12:41:00 -08: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
2020-11-28 08:54:21 -08:00
2020-11-28 12:41:00 -08:00
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
2020-11-28 08:54:21 -08:00
self.hud_id [ name ] = nil
self.hud_text [ name ] = nil
self [ name ] = nil
2020-07-27 17:31:58 -07:00
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-07-27 17:31:58 -07:00
2020-04-21 14:01:14 -07:00
----------------------------------------------
---------------FUNZIONI LOCALI----------------
----------------------------------------------
2020-04-21 08:57:09 -07:00
-- 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
2020-07-26 16:54:50 -07:00
copy [ clone_table ( orig_key ) ] = clone_table ( orig_value )
2020-04-23 02:54:46 -07:00
end
2020-07-26 16:54:50 -07:00
setmetatable ( copy , clone_table ( getmetatable ( orig ) ) )
2020-04-23 02:54:46 -07:00
else -- number, string, boolean, etc
copy = orig
end
return copy
2020-04-21 08:57:09 -07:00
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 " )
2020-11-28 08:54:21 -08:00
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
2020-11-28 08:54:21 -08:00
-- 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 " "
2020-11-28 08:54:21 -08:00
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 12:41:00 -08:00
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
2020-11-28 12:41:00 -08:00
----------------------------------------------
------------------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