Compare commits
5 Commits
587da73726
...
e1ea505ca3
Author | SHA1 | Date |
---|---|---|
Zughy | e1ea505ca3 | |
Zughy | c0268247fe | |
Zughy | 0b63551489 | |
Zughy | 4efc15ccf7 | |
Zughy | ba06bf799b |
16
DOCS.md
16
DOCS.md
|
@ -6,20 +6,21 @@ A panel is a custom object made of 2 HUD elements, the title and the background,
|
|||
|
||||
### 1.1. Declaration
|
||||
To declare a new panel, simply do
|
||||
`local panel = Panel:new({parameters})`
|
||||
`local panel = Panel:new(name, {parameters})`
|
||||
|
||||
The parameters it takes are the following:
|
||||
* `name`: required. The panel name. Used to retrieve it via `panel_lib.get_panel(player_name, panel_name)`.
|
||||
* `player`: required. The player to assign the panel to
|
||||
* `position`: the panel position (as in any other HUD)
|
||||
* `alignment`: same
|
||||
* `offset`: same
|
||||
* `bg`: the picture to put in the background
|
||||
* `bg_scale`: its scaling
|
||||
* `title`: the default text
|
||||
* `title`: the default text. Default is empty (`""`)
|
||||
* `title_size`: (table) as a multiplier, where Y is not considered. Ie. `{ x = 2}` will double up the font size
|
||||
* `title_alignment`
|
||||
* `title_offset`
|
||||
* `title_offset`: if `offset` is declared already, it'll add/subtract the two values
|
||||
* `title_color`
|
||||
* `visible`: (bool) whether the panel is visible right after its creation. Default is `true`
|
||||
* `sub_img_elems`: (table) whatever image to add to the panel
|
||||
* `sub_txt_elems`: (table) whatever text to add to the panel
|
||||
|
||||
|
@ -39,9 +40,9 @@ Install it as any other mod `¯\_(ツ)_/¯`
|
|||
* `new({params})`: creates a new panel
|
||||
* `show()`: makes the panel appear
|
||||
* `hide()`: makes the panel disappear (but it's still assigned to the player)
|
||||
* `is_shown()`: whether the panel is currently displayed
|
||||
* `is_visible()`: whether the panel is currently displayed
|
||||
* `remove()`: deletes it
|
||||
* `update(panel_params, txt_elems, img_elems)`: updates only the mentioned parameters. For instance, calling
|
||||
* `update(panel_params, txt_elems, img_elems)`: updates only the mentioned parameters. Beware: `panel_params` only supports a few. They are `position`, `bg`, `title` and `title_color`.For instance, calling
|
||||
|
||||
```
|
||||
panel:update(nil, nil, {my_custom_img = {
|
||||
|
@ -50,6 +51,9 @@ panel:update(nil, nil, {my_custom_img = {
|
|||
```
|
||||
updates just the text of the sub-element `my_custom_img`.
|
||||
|
||||
* `add_sub_elem(type, name, HUD_elem)`: adds a sub-element at runtime. `type` must be either `text` or `image`. `HUD_elem` is the table representing the HUD
|
||||
* `remove_sub_elem(name)`: removes a sub-element at runtime
|
||||
|
||||
## 2.2 Getters
|
||||
* `panel_lib.get_panel(player_name, panel_name)`: obtains the panel associated with a player
|
||||
|
||||
|
|
72
api.lua
72
api.lua
|
@ -19,6 +19,7 @@ Panel = {
|
|||
position = { x = 1, y = 0.5 },
|
||||
scale = { x = 1, y = 1 },
|
||||
alignment = { x = -1, y = 0 },
|
||||
offset = {x = 0, y = 0},
|
||||
text = "panel_bg.png",
|
||||
},
|
||||
title_def = {
|
||||
|
@ -75,6 +76,11 @@ function Panel:new(name, def)
|
|||
panel.background_def.alignment = def.alignment
|
||||
end
|
||||
|
||||
if def.offset then
|
||||
panel.background_def.offset = def.offset
|
||||
panel.title_def.offset = def.offset
|
||||
end
|
||||
|
||||
if def.bg then
|
||||
panel.background_def.text = def.bg
|
||||
end
|
||||
|
@ -92,7 +98,11 @@ function Panel:new(name, def)
|
|||
end
|
||||
|
||||
if def.title_offset then
|
||||
panel.title_def.offset = def.title_offset
|
||||
if not def.offset then
|
||||
panel.title_def.offset = def.title_offset
|
||||
else
|
||||
panel.title_def.offset = {x = panel.title_def.offset.x + (def.title_offset.x or 0), y = panel.title_def.offset.y + (def.title_offset.y or 0)}
|
||||
end
|
||||
end
|
||||
|
||||
if def.title_color then
|
||||
|
@ -192,8 +202,55 @@ end
|
|||
function Panel:update(def, txt_elems, img_elems)
|
||||
|
||||
if def ~= nil then
|
||||
local player = minetest.get_player_by_name(self.player_name)
|
||||
|
||||
for k, v in pairs(def) do
|
||||
self[k] = v
|
||||
-- aggiorno eventuale posizione, che tratto a parte perché influisce sia su sfondo che titolo,
|
||||
-- influenzando anche tutti i sottoelementi del pannello
|
||||
if k == "position" then
|
||||
self.background_def.position = v
|
||||
self.title_def.position = v
|
||||
|
||||
for _, elem in pairs(self.sub_img_elems) do
|
||||
local HUD_ID = self.hud_id[elem]
|
||||
player:hud_change(HUD_ID, k, v)
|
||||
end
|
||||
|
||||
for _, elem in pairs(self.sub_txt_elems) do
|
||||
local HUD_ID = self.hud_id[elem]
|
||||
player:hud_change(HUD_ID, k, v)
|
||||
end
|
||||
|
||||
player:hud_change(self.hud_id.bg_hud_id, k, v)
|
||||
player:hud_change(self.hud_id.text_hud_id, k, v)
|
||||
|
||||
-- aggiorno eventuale sfondo
|
||||
elseif k == "bg" then
|
||||
self.hud_text.bg_hud_txt = v
|
||||
self.background_def.text = v
|
||||
|
||||
if self.is_visible then
|
||||
player:hud_change(self.hud_id.bg_hud_id, "text", v)
|
||||
end
|
||||
|
||||
-- aggiorno eventuali proprietà titolo
|
||||
elseif k == "title" or k == "title_color" then
|
||||
|
||||
if k == "title" then
|
||||
self.hud_text.text_hud_txt = v
|
||||
self.title_def.text = v
|
||||
if self.is_visible then
|
||||
player:hud_change(self.hud_id.text_hud_id, "text", v)
|
||||
end
|
||||
else
|
||||
self.title_def.number = v
|
||||
player:hud_change(self.hud_id.text_hud_id, "number", v)
|
||||
end
|
||||
|
||||
else
|
||||
error("[PANEL_LIB] Invalid or unsupported element type, check DOCS and spelling")
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -315,6 +372,11 @@ function add_sub_elem(panel, type, name, HUD_elem)
|
|||
-- l'utente a modificare gli offset se vuole spostare gli elementi
|
||||
panel[name].position = mould.position
|
||||
|
||||
-- se il pannello ha gli offset personalizzati e il sottoelemento pure, li sommo
|
||||
if HUD_elem.offset and (panel.background_def.offset.x ~= 0 or panel.background_def.offset.y ~= 0) then
|
||||
panel[name].offset = {x = panel.background_def.offset.x + (HUD_elem.offset.x or 0), y = panel.background_def.offset.y + (HUD_elem.offset.y or 0)}
|
||||
end
|
||||
|
||||
-- mostro l'elemento se il pannello era già visibile
|
||||
panel[name].text = panel:is_visible() and panel[name].text or ""
|
||||
|
||||
|
@ -330,7 +392,11 @@ function update_sub_elems(panel, elems)
|
|||
|
||||
for elem, _ in pairs(elems) do
|
||||
for k, v in pairs(elems[elem]) do
|
||||
panel[elem][k] = v
|
||||
if k == "offset" and (panel.background_def.offset.x ~= 0 or panel.background_def.offset.y ~= 0) then
|
||||
panel[elem][k] = { x = panel.background_def.offset.x + (v.x or 0), y = panel.background_def.offset.y + (v.y or 0)}
|
||||
else
|
||||
panel[elem][k] = v
|
||||
end
|
||||
|
||||
if k == "text" then
|
||||
panel.hud_text[elem] = v
|
||||
|
|
Loading…
Reference in New Issue