Massive refactoring

master
Marco 2020-04-23 14:54:16 +02:00
parent 30cec02a97
commit c7e2c5dd14
7 changed files with 60 additions and 61 deletions

36
DOCS.md
View File

@ -1,16 +1,16 @@
# Scoreboard_lib docs # Panel_lib docs
## 1. Scoreboard structure ## 1. Panel structure
A scoreboard is a custom object made of 2 HUD elements, the title and the background, plus a group of two sub-elements: the text and the images. Picture the background as the container of all the things you want to showcase in your HUD. Those things go in the sub-elements. A panel is a custom object made of 2 HUD elements, the title and the background, plus a group of two sub-elements: the text and the images. Picture the background as the container of all the things you want to showcase in your HUD. Those things go in the sub-elements.
### 1.1. Declaration ### 1.1. Declaration
To declare a new scoreboard, simply do To declare a new panel, simply do
`local scoreboard = Scoreboard:new({parameters})` `local panel = Panel:new({parameters})`
The parameters it takes are the following: The parameters it takes are the following:
* `player`: the player to assign the scoreboard to * `player`: the player to assign the panel to
* `position`: the scoreboard position (as in any other HUD) * `position`: the panel position (as in any other HUD)
* `alignment`: same * `alignment`: same
* `bg`: the picture to put in the background * `bg`: the picture to put in the background
* `bg_scale`: its scaling * `bg_scale`: its scaling
@ -18,15 +18,15 @@ The parameters it takes are the following:
* `title_alignment` * `title_alignment`
* `title_offset` * `title_offset`
* `title_color` * `title_color`
* `sub_img_elems`: (table) whatever image to add to the scoreboard * `sub_img_elems`: (table) whatever image to add to the panel
* `sub_txt_elems`: (table) whatever text to add to the scoreboard * `sub_txt_elems`: (table) whatever text to add to the panel
### 1.2. Sub-elements ### 1.2. Sub-elements
Sub-elements are HUDs added on top of the main container, sharing the same position. This means two things: Sub-elements are HUDs added on top of the main container, sharing the same position. This means two things:
1. As a HUD, they take the same parameters a HUD takes. They are clones respectively of the background and the title, so if you skip some parameter, they'll keep their reference default value. 1. As a HUD, they take the same parameters a HUD takes. They are clones respectively of the background and the title, so if you skip some parameter, they'll keep their reference default value.
2. If you wanna move them around, you must tweak the offset, **NOT** the position. If you change the position, they may look fine in a screen resolution, but not in another. In order to prevent it, `scoreboard_lib` automatically overrides the position with the scoreboard's. 2. If you wanna move them around, you must tweak the offset, **NOT** the position. If you change the position, they may look fine in a screen resolution, but not in another. In order to prevent it, `panel_lib` automatically overrides the position with the panel's.
Have a look at [the example file](https://gitlab.com/zughy-friends-minetest/scoreboard_lib/-/blob/master/scoreboard.example) to see a complete scoreboard declaration. Have a look at [the example file](https://gitlab.com/zughy-friends-minetest/panel_lib/-/blob/master/panel.example) to see a complete panel declaration.
## 2. Configuration ## 2. Configuration
@ -34,21 +34,21 @@ Install it as any other mod `¯\_(ツ)_/¯`
## 2.1. Functions ## 2.1. Functions
* `new({params})`: creates a new scoreboard * `new({params})`: creates a new panel
* `show()`: makes the scoreboard appear * `show()`: makes the panel appear
* `hide()`: makes the scoreboard disappear (but it's still assigned to the player) * `hide()`: makes the panel disappear (but it's still assigned to the player)
* `remove()`: deletes it * `remove()`: deletes it
* `update(scoreboard_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. For instance, calling
``` ```
scoreboard:update(nil, nil, {my_custom_img = { panel:update(nil, nil, {my_custom_img = {
text = "pic2.png" text = "pic2.png"
}) })
``` ```
updates just the text of the sub-element `my_custom_img`. updates just the text of the sub-element `my_custom_img`.
There is also a getter, to obtain the scoreboard associated with a player There is also a getter, to obtain the panel associated with a player
* `scoreboard_lib.get_scoreboard(player_name)` * `panel_lib.get_panel(player_name)`
## 3. Collaborating ## 3. Collaborating
Something's wrong? Feel free to open an issue, go for a merge request and whatnot. I'd really appreciate it :) Something's wrong? Feel free to open an issue, go for a merge request and whatnot. I'd really appreciate it :)

View File

@ -1,6 +1,6 @@
# Scoreboard_lib # Panel_lib
Scoreboard_lib is a library for Minetest allowing modders to create complex scoreboards in an easy way Panel_lib is a library for Minetest allowing modders to create complex HUDs in an easy way
<a href="https://liberapay.com/EticaDigitale/donate"><img src="https://i.imgur.com/4B2PxjP.png" alt="Support my work"/></a> <a href="https://liberapay.com/EticaDigitale/donate"><img src="https://i.imgur.com/4B2PxjP.png" alt="Support my work"/></a>
@ -17,4 +17,4 @@ Have a read at the [DOCS](https://gitlab.com/zughy-friends-minetest/scoreboard_l
(none) (none)
#### Mods relying on scoreboard_lib #### Mods relying on scoreboard_lib
[Quake](https://gitlab.com/zughy-friends-minetest/minetest-quake) [Quake](https://gitlab.com/zughy-friends-minetest/minetest-quake)

72
api.lua
View File

@ -1,20 +1,20 @@
scoreboard_lib.scoreboards = {} panel_lib.panels = {}
local function clone_table() end local function clone_table() end
Scoreboard = { Panel = {
-- player to show the scoreboard to -- player to show the panel to
player_name = "", player_name = "",
-- ids of the hud of the player -- ids of the hud of the player
hud_id = {}, hud_id = {},
-- because the scoreboard is composed by a background and a text we need to -- because the panel is composed by a background and a text we need to
-- define the two HUD to use later -- define the two HUD to use later
background_def = { background_def = {
hud_elem_type = "image", hud_elem_type = "image",
position = { x = 1, y = 0.5 }, position = { x = 1, y = 0.5 },
scale = { x = 1, y = 1 }, scale = { x = 1, y = 1 },
alignment = { x = -1, y = 0 }, alignment = { x = -1, y = 0 },
text = "scoreboard_bg.png", text = "panel_bg.png",
}, },
title_def = { title_def = {
hud_elem_type = "text", hud_elem_type = "text",
@ -32,63 +32,63 @@ Scoreboard = {
function Scoreboard:new(def) function Panel:new(def)
local scoreboard = {} local panel = {}
setmetatable(scoreboard, self) setmetatable(panel, self)
self.__index = self self.__index = self
if def.position then if def.position then
scoreboard.background_def.position = def.position panel.background_def.position = def.position
scoreboard.title_def.position = def.position panel.title_def.position = def.position
end end
if def.alignment then if def.alignment then
scoreboard.background_def.alignment = def.alignment panel.background_def.alignment = def.alignment
end end
if def.bg then if def.bg then
scoreboard.background_def.text = def.bg panel.background_def.text = def.bg
end end
if def.bg_scale then if def.bg_scale then
scoreboard.background_def.scale = def.bg_scale panel.background_def.scale = def.bg_scale
end end
if def.title then if def.title then
scoreboard.title_def.text = def.title panel.title_def.text = def.title
end end
if def.title_alignment then if def.title_alignment then
scoreboard.title_def.alignment = def.title_alignment panel.title_def.alignment = def.title_alignment
end end
if def.title_offset then if def.title_offset then
scoreboard.title_def.offset = def.title_offset panel.title_def.offset = def.title_offset
end end
if def.title_color then if def.title_color then
scoreboard.title_def.number = def.title_color panel.title_def.number = def.title_color
end end
if def.player then if def.player then
scoreboard.player_name = def.player panel.player_name = def.player
end end
-- controllo sottoelementi -- controllo sottoelementi
if def.sub_img_elems then if def.sub_img_elems then
local i = 1 local i = 1
for name, elem in pairs(def.sub_img_elems) do for name, elem in pairs(def.sub_img_elems) do
scoreboard.sub_img_elems[i] = name panel.sub_img_elems[i] = name
scoreboard[name] = clone_table(scoreboard.background_def) panel[name] = clone_table(panel.background_def)
for param, v in pairs(elem) do for param, v in pairs(elem) do
scoreboard[name][param] = v panel[name][param] = v
end end
-- mantengo la stessa posizione del corpo della scoreboard, costringendo -- mantengo la stessa posizione del corpo della panel, costringendo
-- l'utente a modificare gli offset se vuole spostare gli elementi -- l'utente a modificare gli offset se vuole spostare gli elementi
scoreboard[name].position = def.position panel[name].position = def.position
i = i +1 i = i +1
end end
end end
@ -96,27 +96,27 @@ function Scoreboard:new(def)
if def.sub_txt_elems then if def.sub_txt_elems then
local i = 1 local i = 1
for name, elem in pairs(def.sub_txt_elems) do for name, elem in pairs(def.sub_txt_elems) do
scoreboard.sub_txt_elems[i] = name panel.sub_txt_elems[i] = name
scoreboard[name] = clone_table(scoreboard.title_def) panel[name] = clone_table(panel.title_def)
for param, v in pairs(elem) do for param, v in pairs(elem) do
scoreboard[name][param] = v panel[name][param] = v
end end
-- idem come sopra -- idem come sopra
scoreboard[name].position = def.position panel[name].position = def.position
i = i +1 i = i +1
end end
end end
-- salvo in memoria -- salvo in memoria
scoreboard_lib.scoreboards[def.player] = scoreboard panel_lib.panels[def.player] = panel
return scoreboard return panel
end end
function Scoreboard:show() function Panel:show()
local player = minetest.get_player_by_name(self.player_name) local player = minetest.get_player_by_name(self.player_name)
self.hud_id.bg_hud_id = player:hud_add(self.background_def) self.hud_id.bg_hud_id = player:hud_add(self.background_def)
self.hud_id.text_hud_id = player:hud_add(self.title_def) self.hud_id.text_hud_id = player:hud_add(self.title_def)
@ -133,7 +133,7 @@ end
function Scoreboard:hide() function Panel:hide()
if (self.hud_id) then if (self.hud_id) then
local player = minetest.get_player_by_name(self.player_name) local player = minetest.get_player_by_name(self.player_name)
@ -146,7 +146,7 @@ end
function Scoreboard:update(def, txt_elems, img_elems) function Panel:update(def, txt_elems, img_elems)
if def ~= nil then if def ~= nil then
for k, v in pairs(def) do for k, v in pairs(def) do
@ -177,8 +177,8 @@ end
function Scoreboard:remove() function Panel:remove()
scoreboard_lib.scoreboards[self.player_name] = nil panel_lib.panels[self.player_name] = nil
self:hide() self:hide()
self = nil self = nil
end end
@ -189,8 +189,8 @@ end
-----------------GETTERS---------------------- -----------------GETTERS----------------------
---------------------------------------------- ----------------------------------------------
function scoreboard_lib.get_scoreboard(p_name) function panel_lib.get_panel(p_name)
return scoreboard_lib.scoreboards[p_name] return panel_lib.panels[p_name]
end end

View File

@ -1,3 +1,3 @@
scoreboard_lib = {} panel_lib = {}
dofile(minetest.get_modpath("scoreboard_lib") .. "/api.lua") dofile(minetest.get_modpath("panel_lib") .. "/api.lua")

View File

@ -1 +0,0 @@
name = scoreboard_lib

View File

@ -2,7 +2,7 @@
player = "singleplayer", player = "singleplayer",
position = { x = 1, y = 0.5 }, position = { x = 1, y = 0.5 },
alignment = { x = -1, y = 0 }, alignment = { x = -1, y = 0 },
bg = "scoreboard_bg.png" bg = "panel_bg.png"
bg_scale = { x = 45, y = 28 }, bg_scale = { x = 45, y = 28 },
title = "Default", title = "Default",
title_alignment = { x = 0, y = 0 }, title_alignment = { x = 0, y = 0 },

View File

Before

Width:  |  Height:  |  Size: 99 B

After

Width:  |  Height:  |  Size: 99 B