mirror of
https://repo.or.cz/minetest_hudbars.git
synced 2024-08-20 13:16:07 +02:00
Rename main table from “hud” to “hb”
This commit is contained in:
parent
b36645816f
commit
041e76692b
38
API.md
38
API.md
@ -12,10 +12,10 @@ This API allows you to add, change, hide and unhide custom HUD bars for this mod
|
||||
To give you a *very* brief overview over this API, here is the basic workflow on how to add your own custom HUD bar:
|
||||
|
||||
* Create images for your HUD bar
|
||||
* Call `hud.register_hudbar` to make the definition of the HUD bar known to this mod
|
||||
* Call `hud.init_hudbar` for each player for which you want to use previously defined HUD bar
|
||||
* Use `hud.change_hudbar` whenever you need to change the values of a HUD bar of a certain player
|
||||
* If you need it: Use `hud.hide_hudbar` and `hud.unhide_hudbar` to hide or unhide HUD bars of a certain player
|
||||
* Call `hb.register_hudbar` to make the definition of the HUD bar known to this mod
|
||||
* Call `hb.init_hudbar` for each player for which you want to use previously defined HUD bar
|
||||
* Use `hb.change_hudbar` whenever you need to change the values of a HUD bar of a certain player
|
||||
* If you need it: Use `hb.hide_hudbar` and `hb.unhide_hudbar` to hide or unhide HUD bars of a certain player
|
||||
|
||||
## The basic rules
|
||||
In order to use this API, you should be aware of a few basic rules in order to understand it:
|
||||
@ -35,7 +35,7 @@ To make a new HUD bar known to this mod, you need …
|
||||
|
||||
* … an image of size 2×16 for the bar
|
||||
* … an icon of size 16×16 (optional)
|
||||
* … to register it with `hud.register_hudbar`
|
||||
* … to register it with `hb.register_hudbar`
|
||||
|
||||
### Bar image
|
||||
The image for the bar will be repeated horizontally to denote the “value” of the HUD bar.
|
||||
@ -49,7 +49,7 @@ a vertical gradient.
|
||||
### Icon
|
||||
A 16×16 image shown left of the HUD bar. This is optional.
|
||||
|
||||
### `hud.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, start_hide, format_string)`
|
||||
### `hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, start_hide, format_string)`
|
||||
This function adds a new custom HUD
|
||||
Note this does not yet display the HUD bar.
|
||||
|
||||
@ -64,7 +64,7 @@ There is currently no reliable way to force a certain order at which the custom
|
||||
* `icon`: The file name of the icon, as string. This field can be `nil`, in which case no icon will be used.
|
||||
* `default_start_value`: If this HUD bar is added to a player, and no initial value is specified, this value will be used as initial current value
|
||||
* `default_max_value`: If this HUD bar is added to a player, and no initial maximum value is specified, this value will be used as initial maximum value
|
||||
* `start_hide`: The HUD bar will be initially start hidden when added to a player. Use `hud.unhide_hudbar` to unhide it.
|
||||
* `start_hide`: The HUD bar will be initially start hidden when added to a player. Use `hb.unhide_hudbar` to unhide it.
|
||||
* `format_string`: This is optional; You can specify an alternative format string display the final text on the HUD bar. The default format string is “`%s: %d/%d`” (in this order: Label, current value, maximum value). See also the Lua documentation of `string.format`.
|
||||
|
||||
#### Return value
|
||||
@ -77,18 +77,18 @@ explicitly initialized on a per-player basis.
|
||||
|
||||
You probably want to do this in the `minetest.register_on_joinplayer`.
|
||||
|
||||
### `hud.init_hudbar(player, identifier, start_value, start_max)`
|
||||
### `hb.init_hudbar(player, identifier, start_value, start_max)`
|
||||
This function initialzes and activates a previously registered HUD bar and assigns it to a
|
||||
certain client/player. This has only to be done once per player and after that, you can change
|
||||
the values using `hud.change_hudbar`.
|
||||
the values using `hb.change_hudbar`.
|
||||
|
||||
However, if `start_hide` was set to `true` for the HUD bar (in `hud.register_hudbar`), the HUD bar
|
||||
However, if `start_hide` was set to `true` for the HUD bar (in `hb.register_hudbar`), the HUD bar
|
||||
will initially be hidden, but the HUD elements are still sent to the client. Otherwise,
|
||||
the HUD bar will be initially be shown to the player.
|
||||
|
||||
#### Parameters
|
||||
* `player`: `ObjectRef` of the player to which the new HUD bar should be displayed to.
|
||||
* `identifier`: The identifier of the HUD bar type, as specified in `hud.register_hudbar`.
|
||||
* `identifier`: The identifier of the HUD bar type, as specified in `hb.register_hudbar`.
|
||||
* `start_value`: The initial current value of the HUD bar. This is optional, `default_start_value` of the registration function will be used, if this is `nil`.
|
||||
* `start_max`: The initial maximum value of the HUD bar. This is optional, `default_start_max` of the registration function will be used, if this is `nil`
|
||||
|
||||
@ -99,16 +99,16 @@ Always `nil`.
|
||||
|
||||
## Modifying a HUD bar
|
||||
After a HUD bar has been added, you can change the current and maximum value on a per-player basis.
|
||||
You use the function `hud.change_hudbar` for this.
|
||||
You use the function `hb.change_hudbar` for this.
|
||||
|
||||
### `hud.change_hudbar(player, identifier, new_value, new_max_value)`
|
||||
### `hb.change_hudbar(player, identifier, new_value, new_max_value)`
|
||||
Changes the values of an initialized HUD bar for a certain player. `new_value` and `new_max_value`
|
||||
can be `nil`; if one of them is `nil`, that means the value is unchanged. If both values
|
||||
are `nil`, this function is a no-op.
|
||||
|
||||
#### Parameters
|
||||
* `player`: `ObjectRef` of the player to which the HUD bar belongs to
|
||||
* `identifier`: The identifier of the HUD bar type to change, as specified in `hud.register_hudbar`.
|
||||
* `identifier`: The identifier of the HUD bar type to change, as specified in `hb.register_hudbar`.
|
||||
* `new_value`: The new current value of the HUD bar
|
||||
* `new_max_value`: The new maximum value of the HUD bar
|
||||
|
||||
@ -118,29 +118,29 @@ Always `nil`.
|
||||
|
||||
## Hiding and unhiding a HUD bar
|
||||
You can also hide custom HUD bars, meaning they will not be displayed for a certain player. You can still
|
||||
use `hud.change_hudbar` on a hidden HUD bar, the new values will be correctly displayed after the HUD bar
|
||||
use `hb.change_hudbar` on a hidden HUD bar, the new values will be correctly displayed after the HUD bar
|
||||
has been unhidden.
|
||||
|
||||
Note that the hidden state of a HUD bar will *not* be saved by this mod on server shutdown, so you may need
|
||||
to write your own routines for this.
|
||||
|
||||
### `hud.hide_hudbar(player, identifier)`
|
||||
### `hb.hide_hudbar(player, identifier)`
|
||||
Hides the specified HUD bar from the screen of the specified player.
|
||||
|
||||
#### Parameters
|
||||
* `player`: `ObjectRef` of the player to which the HUD bar belongs to
|
||||
* `identifier`: The identifier of the HUD bar type to hide, as specified in `hud.register_hudbar`.
|
||||
* `identifier`: The identifier of the HUD bar type to hide, as specified in `hb.register_hudbar`.
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
|
||||
|
||||
### `hud.hide_hudbar(player, identifier)`
|
||||
### `hb.hide_hudbar(player, identifier)`
|
||||
Makes a previously hidden HUD bar visible again to a player.
|
||||
|
||||
#### Parameters
|
||||
* `player`: `ObjectRef` of the player to which the HUD bar belongs to
|
||||
* `identifier`: The identifier of the HUD bar type to unhide, as specified in `hud.register_hudbar`.
|
||||
* `identifier`: The identifier of the HUD bar type to unhide, as specified in `hb.register_hudbar`.
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
|
74
init.lua
74
init.lua
@ -1,13 +1,13 @@
|
||||
hud = {}
|
||||
hb = {}
|
||||
|
||||
-- HUD statbar values
|
||||
hud.health = {}
|
||||
hud.air = {}
|
||||
hb.health = {}
|
||||
hb.air = {}
|
||||
|
||||
hud.hudtables = {}
|
||||
hb.hudtables = {}
|
||||
|
||||
-- number of registered HUD bars
|
||||
hud.hudbars_count = 0
|
||||
hb.hudbars_count = 0
|
||||
|
||||
-- HUD item ids
|
||||
local health_hud = {}
|
||||
@ -53,7 +53,7 @@ end
|
||||
|
||||
HUD_TICK = 0.1
|
||||
|
||||
function hud.value_to_barlength(value, max)
|
||||
function hb.value_to_barlength(value, max)
|
||||
if max == 0 then
|
||||
return 0
|
||||
else
|
||||
@ -61,24 +61,24 @@ function hud.value_to_barlength(value, max)
|
||||
end
|
||||
end
|
||||
|
||||
function hud.get_hudtable(identifier)
|
||||
return hud.hudtables[identifier]
|
||||
function hb.get_hudtable(identifier)
|
||||
return hb.hudtables[identifier]
|
||||
end
|
||||
|
||||
function hud.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, start_hide, format_string)
|
||||
function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, start_hide, format_string)
|
||||
local hudtable = {}
|
||||
local pos, offset
|
||||
if hud.hudbars_count % 2 == 0 then
|
||||
if hb.hudbars_count % 2 == 0 then
|
||||
pos = HUD_CUSTOM_POS_LEFT
|
||||
offset = {
|
||||
x = HUD_CUSTOM_START_OFFSET_LEFT.x,
|
||||
y = HUD_CUSTOM_START_OFFSET_LEFT.y - HUD_CUSTOM_VMARGIN * math.floor(hud.hudbars_count/2)
|
||||
y = HUD_CUSTOM_START_OFFSET_LEFT.y - HUD_CUSTOM_VMARGIN * math.floor(hb.hudbars_count/2)
|
||||
}
|
||||
else
|
||||
pos = HUD_CUSTOM_POS_RIGHT
|
||||
offset = {
|
||||
x = HUD_CUSTOM_START_OFFSET_RIGHT.x,
|
||||
y = HUD_CUSTOM_START_OFFSET_RIGHT.y - HUD_CUSTOM_VMARGIN * math.floor((hud.hudbars_count-1)/2)
|
||||
y = HUD_CUSTOM_START_OFFSET_RIGHT.y - HUD_CUSTOM_VMARGIN * math.floor((hb.hudbars_count-1)/2)
|
||||
}
|
||||
end
|
||||
if format_string == nil then
|
||||
@ -119,7 +119,7 @@ function hud.register_hudbar(identifier, text_color, label, textures, default_st
|
||||
hud_elem_type = "statbar",
|
||||
position = pos,
|
||||
text = textures.bar,
|
||||
number = hud.value_to_barlength(start_value, start_max),
|
||||
number = hb.value_to_barlength(start_value, start_max),
|
||||
alignment = {x=-1,y=-1},
|
||||
offset = offset,
|
||||
})
|
||||
@ -136,8 +136,8 @@ function hud.register_hudbar(identifier, text_color, label, textures, default_st
|
||||
state.value = start_value
|
||||
state.max = start_max
|
||||
|
||||
hud.hudtables[identifier].hudids[name] = ids
|
||||
hud.hudtables[identifier].hudstate[name] = state
|
||||
hb.hudtables[identifier].hudids[name] = ids
|
||||
hb.hudtables[identifier].hudstate[name] = state
|
||||
end
|
||||
|
||||
hudtable.identifier = identifier
|
||||
@ -146,22 +146,22 @@ function hud.register_hudbar(identifier, text_color, label, textures, default_st
|
||||
hudtable.hudids = {}
|
||||
hudtable.hudstate = {}
|
||||
|
||||
hud.hudbars_count= hud.hudbars_count + 1
|
||||
hb.hudbars_count= hb.hudbars_count + 1
|
||||
|
||||
hud.hudtables[identifier] = hudtable
|
||||
hb.hudtables[identifier] = hudtable
|
||||
end
|
||||
|
||||
function hud.init_hudbar(player, identifier, start_value, start_max)
|
||||
hud.hudtables[identifier].add_all(player, start_value, start_max)
|
||||
function hb.init_hudbar(player, identifier, start_value, start_max)
|
||||
hb.hudtables[identifier].add_all(player, start_value, start_max)
|
||||
end
|
||||
|
||||
function hud.change_hudbar(player, identifier, new_value, new_max_value)
|
||||
function hb.change_hudbar(player, identifier, new_value, new_max_value)
|
||||
if new_value == nil and new_max_value == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hud.get_hudtable(identifier)
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
|
||||
if new_value ~= nil then
|
||||
hudtable.hudstate[name].value = new_value
|
||||
@ -180,16 +180,16 @@ function hud.change_hudbar(player, identifier, new_value, new_max_value)
|
||||
else
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
|
||||
end
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", hud.value_to_barlength(new_value, new_max_value))
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value))
|
||||
player:hud_change(hudtable.hudids[name].text, "text",
|
||||
tostring(string.format(hudtable.format_string, hudtable.label, new_value, new_max_value))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function hud.hide_hudbar(player, identifier)
|
||||
function hb.hide_hudbar(player, identifier)
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hud.get_hudtable(identifier)
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
if(hudtable.hudstate[name].hidden == false) then
|
||||
player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0})
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
|
||||
@ -199,9 +199,9 @@ function hud.hide_hudbar(player, identifier)
|
||||
end
|
||||
end
|
||||
|
||||
function hud.unhide_hudbar(player, identifier)
|
||||
function hb.unhide_hudbar(player, identifier)
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hud.get_hudtable(identifier)
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
if(hudtable.hudstate[name].hidden) then
|
||||
local name = player:get_player_name()
|
||||
local value = hudtable.hudstate[name].value
|
||||
@ -210,7 +210,7 @@ function hud.unhide_hudbar(player, identifier)
|
||||
if hudtable.hudstate[name].max ~= 0 then
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
|
||||
end
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", hud.value_to_barlength(value, max))
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max))
|
||||
player:hud_change(hudtable.hudids[name].text, "text", tostring(string.format(hudtable.format_string, hudtable.label, value, max)))
|
||||
hudtable.hudstate[name].hidden = false
|
||||
end
|
||||
@ -254,7 +254,7 @@ local function custom_hud(player)
|
||||
hud_elem_type = "statbar",
|
||||
position = HUD_HEALTH_POS,
|
||||
text = "hudbars_bar_health.png",
|
||||
number = hud.value_to_barlength(player:get_hp(), 20),
|
||||
number = hb.value_to_barlength(player:get_hp(), 20),
|
||||
alignment = {x=-1,y=-1},
|
||||
offset = HUD_HEALTH_OFFSET,
|
||||
})
|
||||
@ -276,7 +276,7 @@ local function custom_hud(player)
|
||||
airtext = ""
|
||||
airscale = {x=0, y=0}
|
||||
else
|
||||
airnumber = hud.value_to_barlength(math.min(air, 10), 10)
|
||||
airnumber = hb.value_to_barlength(math.min(air, 10), 10)
|
||||
airtext = tostring(string.format("Breath: %d/%d", math.min(air, 10), 10))
|
||||
airscale = {x=1, y=1}
|
||||
end
|
||||
@ -322,17 +322,17 @@ end
|
||||
local function update_hud(player)
|
||||
local name = player:get_player_name()
|
||||
--air
|
||||
local air = tonumber(hud.air[name])
|
||||
local air = tonumber(hb.air[name])
|
||||
if player:get_breath() ~= air then
|
||||
air = player:get_breath()
|
||||
hud.air[name] = air
|
||||
hb.air[name] = air
|
||||
local airnumber, airtext, airscale
|
||||
if air == 11 then
|
||||
airnumber = 0
|
||||
airtext = ""
|
||||
airscale = {x=0, y=0}
|
||||
else
|
||||
airnumber = hud.value_to_barlength(math.min(air, 10), 10)
|
||||
airnumber = hb.value_to_barlength(math.min(air, 10), 10)
|
||||
airtext = tostring(string.format("Breath: %d/%d", math.min(player:get_breath(), 10), 10))
|
||||
airscale = {x=1, y=1}
|
||||
end
|
||||
@ -342,11 +342,11 @@ local function update_hud(player)
|
||||
player:hud_change(air_hud_bg[name], "scale", airscale)
|
||||
end
|
||||
--health
|
||||
local hp = tonumber(hud.health[name])
|
||||
local hp = tonumber(hb.health[name])
|
||||
if player:get_hp() ~= hp then
|
||||
hp = player:get_hp()
|
||||
hud.health[name] = hp
|
||||
player:hud_change(health_hud[name], "number", hud.value_to_barlength(hp, 20))
|
||||
hb.health[name] = hp
|
||||
player:hud_change(health_hud[name], "number", hb.value_to_barlength(hp, 20))
|
||||
player:hud_change(health_hud_text[name], "text",
|
||||
tostring(string.format("Health: %d/%d", hp, 20))
|
||||
)
|
||||
@ -357,9 +357,9 @@ end
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
local inv = player:get_inventory()
|
||||
hud.health[name] = player:get_hp()
|
||||
hb.health[name] = player:get_hp()
|
||||
local air = player:get_breath()
|
||||
hud.air[name] = air
|
||||
hb.air[name] = air
|
||||
minetest.after(0.5, function()
|
||||
hide_builtin(player)
|
||||
custom_hud(player)
|
||||
|
Loading…
Reference in New Issue
Block a user