Rename to HUD Library [hudlib]

This commit is contained in:
octacian 2017-03-14 11:54:35 -07:00
parent 639bb08678
commit 27d812f563
6 changed files with 63 additions and 64 deletions

28
API.md
View File

@ -1,57 +1,57 @@
# API # API
HUD Plus introduces several API functions which can be used either in your own mod or when developing an official module. These functions make it easier and quicker to manage HUDs, using names rather than relying on IDs. This also allows for more advanced features like showing and hiding an HUD without actually fully destroying it. HUD Library introduces several API functions which can be used either in your own mod or when developing an official module. These functions make it easier and quicker to manage HUDs, using names rather than relying on IDs. This also allows for more advanced features like showing and hiding an HUD without actually fully destroying it.
#### `after` #### `after`
__Usage:__ `hudplus.after(<name (string)>, <time (integer)>, <func (function)>)` __Usage:__ `hudlib.after(<name (string)>, <time (integer)>, <func (function)>)`
This is a nearly drop-in replacement for `minetest.after`, except it can be considered to be slightly more advanced. It uses global steps instead, which are also considered more reliable. The most important change, is that things queued with `after` are placed in a queue table under the name specified. This means that if `after` is called specifying the same name, the time will be updated and the counter reset preventing HUDs and other things from disappearing too quickly in the case of `minetest.after` where calls would get stacked up. This is a nearly drop-in replacement for `minetest.after`, except it can be considered to be slightly more advanced. It uses global steps instead, which are also considered more reliable. The most important change, is that things queued with `after` are placed in a queue table under the name specified. This means that if `after` is called specifying the same name, the time will be updated and the counter reset preventing HUDs and other things from disappearing too quickly in the case of `minetest.after` where calls would get stacked up.
#### `after_remove` #### `after_remove`
__Usage:__ `hudplus.after_remove(<name (string)>)` __Usage:__ `hudlib.after_remove(<name (string)>)`
Remove an after call from the queue so that it will not be called when its time is up. After calls using HUD Plus' after function are queued by name. See `after` for more information. Remove an after call from the queue so that it will not be called when its time is up. After calls using HUD Library' after function are queued by name. See `after` for more information.
#### `hud_get` #### `hud_get`
__Usage:__ `hudplus.hud_get(<player (userdata or string)>, <hud name (string)>, <value to get (string, optional)>` __Usage:__ `hudlib.hud_get(<player (userdata or string)>, <hud name (string)>, <value to get (string, optional)>`
Allows retrieving information about an HUD (including the original definition). The third parameter allows you to specify which piece of information you want to get (e.g. `def`). If the third parameter is `nil` or invalid, the entire table containing all the information about the HUD will be returned. Allows retrieving information about an HUD (including the original definition). The third parameter allows you to specify which piece of information you want to get (e.g. `def`). If the third parameter is `nil` or invalid, the entire table containing all the information about the HUD will be returned.
#### `hud_set` #### `hud_set`
__Usage:__ `hudplus.hud_set(<player (userdata or string)>, <hud name (string)>, <key to set (string)>, <value>)` __Usage:__ `hudlib.hud_set(<player (userdata or string)>, <hud name (string)>, <key to set (string)>, <value>)`
Sets a piece of information for an HUD in the HUDs table where HUD Plus keeps track of registered HUDs. __Note:__ the final `value` parameter can be of any type (e.g. string, table, or boolean). Sets a piece of information for an HUD in the HUDs table where HUD Library keeps track of registered HUDs. __Note:__ the final `value` parameter can be of any type (e.g. string, table, or boolean).
#### `hud_add` #### `hud_add`
__Usage:__ `hudplus.hud_add(<player (userdata or string)>, <hud name (string)>, <definition (table)>)` __Usage:__ `hudlib.hud_add(<player (userdata or string)>, <hud name (string)>, <definition (table)>)`
Adds an HUD to the player. The HUD name should be a unique string (e.g. `game_time`), but typically not an ID as is used by `player:hud_add`. The definition accepts the same parameters as `player:hud_add`, however, `hud_elem_type` can be shortened to `type` and `position` can be shortened to `pos`. An HUD can be hidden by default by setting the `show` attribute to `false` (can be shown with `hud_show`). An HUD can be set to automatically hide itself after a certain number of seconds with `hide_after`. The callback attribute `on_hide` will be called whenever the HUD is hidden, and `on_show` whenever it is shown. `on_step` is called on every globalstep and is provided with the `player` and `dtime`. For further information, see the official developer wiki [documentation](http://dev.minetest.net/HUD). __Note:__ if an HUD with the name specified already exists, the new HUD will not be added. Adds an HUD to the player. The HUD name should be a unique string (e.g. `game_time`), but typically not an ID as is used by `player:hud_add`. The definition accepts the same parameters as `player:hud_add`, however, `hud_elem_type` can be shortened to `type` and `position` can be shortened to `pos`. An HUD can be hidden by default by setting the `show` attribute to `false` (can be shown with `hud_show`). An HUD can be set to automatically hide itself after a certain number of seconds with `hide_after`. The callback attribute `on_hide` will be called whenever the HUD is hidden, and `on_show` whenever it is shown. `on_step` is called on every globalstep and is provided with the `player` and `dtime`. For further information, see the official developer wiki [documentation](http://dev.minetest.net/HUD). __Note:__ if an HUD with the name specified already exists, the new HUD will not be added.
#### `hud_remove` #### `hud_remove`
__Usage:__ `hudplus.hud_remove(<player (userdata or string)>, <hud name (string)>)` __Usage:__ `hudlib.hud_remove(<player (userdata or string)>, <hud name (string)>)`
Removes an HUD from the player. If the HUD specified by the second parameter does not exist, nothing will be removed. Removes an HUD from the player. If the HUD specified by the second parameter does not exist, nothing will be removed.
#### `hud_change` #### `hud_change`
__Usage:__ `hudplus.hud_change(<player (userdata or string)>, <hud name (string)>, <attribute to change (string)>, <new value>)` __Usage:__ `hudlib.hud_change(<player (userdata or string)>, <hud name (string)>, <attribute to change (string)>, <new value>)`
Changes an HUD specified by the second parameter previously attached to the player. The third parameter specifies the name of the attribute to change, while the fourth specified the new value. `hud_elem_type` can be shortened to `type` and `position` can be shortened to `pos` (as with `hud_add`). Changes an HUD specified by the second parameter previously attached to the player. The third parameter specifies the name of the attribute to change, while the fourth specified the new value. `hud_elem_type` can be shortened to `type` and `position` can be shortened to `pos` (as with `hud_add`).
#### `hud_hide` #### `hud_hide`
__Usage:__ `hudplus.hud_hide(<player (userdata or string)>, <hud name (string)>)` __Usage:__ `hudlib.hud_hide(<player (userdata or string)>, <hud name (string)>)`
Non-destructively hides the HUD from the player. `nil` will be returned if the HUD is already hidden or does not exist. Can be shown again with `hud_show`. Non-destructively hides the HUD from the player. `nil` will be returned if the HUD is already hidden or does not exist. Can be shown again with `hud_show`.
#### `hud_show` #### `hud_show`
__Usage:__ `hudplus.hud_show(<player (userdata or string)>, <hud name (string)>)` __Usage:__ `hudlib.hud_show(<player (userdata or string)>, <hud name (string)>)`
Shows a previously hidden HUD to the player. `nil` will be returned if the HUD is already visible or does not exist. Can be hidden with `hud_hide`. Shows a previously hidden HUD to the player. `nil` will be returned if the HUD is already visible or does not exist. Can be hidden with `hud_hide`.
#### `register` #### `register`
__Usage:__ `hudplus.register(<hud name (string)>, <definition (table)>)` __Usage:__ `hudlib.register(<hud name (string)>, <definition (table)>)`
Registers an HUD to be shown to all players. Definition and name follow same standards as with `hud_add`. __Note:__ HUDs registered with this API function are only shown to a player when they join the game. If you wish to show an HUD to all players mid-game, set `show_on` to `now`. Registers an HUD to be shown to all players. Definition and name follow same standards as with `hud_add`. __Note:__ HUDs registered with this API function are only shown to a player when they join the game. If you wish to show an HUD to all players mid-game, set `show_on` to `now`.
#### `unregister` #### `unregister`
__Usage:__ `hudplus.unregister(<hud name(string)>)` __Usage:__ `hudlib.unregister(<hud name(string)>)`
Removes and HUD from all players at once. Useful if you need to temporarily disable an HUD for everybody (e.g. changing everyone's gamemode to creative). Removes and HUD from all players at once. Useful if you need to temporarily disable an HUD for everybody (e.g. changing everyone's gamemode to creative).

View File

@ -1,6 +1,6 @@
![Screenshot](.gh-screenshot.png) ![Screenshot](.gh-screenshot.png)
HUD Plus [hudplus] HUD Library [hudlib]
==================== ====================
This mod aims to create a better player HUD at the same time providing an extensive yet simplified API for modders (see API.md). Though this mod is small at the time, it can be expected to grow with new features including (but not limited to) hunger and armor statbars. This mod aims to create a better player HUD at the same time providing an extensive yet simplified API for modders (see API.md). Though this mod is small at the time, it can be expected to grow with new features including (but not limited to) hunger and armor statbars.

70
api.lua
View File

@ -1,11 +1,11 @@
-- hudplus/api.lua -- hudlib/api.lua
local huds = {} local huds = {}
local queue = {} local queue = {}
local times = {} local times = {}
-- [function] After -- [function] After
function hudplus.after(name, time, func) function hudlib.after(name, time, func)
queue[name] = { time = time, func = func, } queue[name] = { time = time, func = func, }
-- Update times table -- Update times table
@ -15,7 +15,7 @@ function hudplus.after(name, time, func)
end end
-- [function] Destroy after -- [function] Destroy after
function hudplus.after_remove(name) function hudlib.after_remove(name)
if queue[name] then if queue[name] then
queue[name] = nil queue[name] = nil
return true return true
@ -33,7 +33,7 @@ minetest.register_globalstep(function(dtime)
times[_] = times[_] + dtime times[_] = times[_] + dtime
if times[_] >= i.time then if times[_] >= i.time then
i.func() i.func()
hudplus.after_remove(_) hudlib.after_remove(_)
times[_] = nil times[_] = nil
end end
end end
@ -49,8 +49,8 @@ minetest.register_globalstep(function(dtime)
end) end)
-- [function] Get HUD -- [function] Get HUD
function hudplus.hud_get(name, hud_name, key) function hudlib.hud_get(name, hud_name, key)
assert(name and hud_name, "hudplus.hud_get: Invalid parameters") assert(name and hud_name, "hudlib.hud_get: Invalid parameters")
if type(name) == "userdata" then if type(name) == "userdata" then
name = name:get_player_name() name = name:get_player_name()
end end
@ -70,8 +70,8 @@ function hudplus.hud_get(name, hud_name, key)
end end
-- [function] Set HUD Value -- [function] Set HUD Value
function hudplus.hud_set(name, hud_name, key, value) function hudlib.hud_set(name, hud_name, key, value)
assert(name and hud_name and key, "hudplus.hud_set: Invalid parameters") assert(name and hud_name and key, "hudlib.hud_set: Invalid parameters")
if type(name) == "userdata" then if type(name) == "userdata" then
name = name:get_player_name() name = name:get_player_name()
end end
@ -87,8 +87,8 @@ function hudplus.hud_set(name, hud_name, key, value)
end end
-- [function] Add HUD -- [function] Add HUD
function hudplus.hud_add(player, hud_name, def) function hudlib.hud_add(player, hud_name, def)
assert(player and hud_name and def, "hudplus.hud_add: Invalid parameters") assert(player and hud_name and def, "hudlib.hud_add: Invalid parameters")
if type(player) == "string" then if type(player) == "string" then
player = minetest.get_player_by_name(player) player = minetest.get_player_by_name(player)
end end
@ -120,8 +120,8 @@ function hudplus.hud_add(player, hud_name, def)
} }
if def.hide_after then if def.hide_after then
hudplus.after("hide_"..hud_name, def.hide_after, function() hudlib.after("hide_"..hud_name, def.hide_after, function()
hudplus.hud_hide(player, hud_name) hudlib.hud_hide(player, hud_name)
if def.on_hide then if def.on_hide then
def.on_hide() def.on_hide()
@ -135,14 +135,14 @@ function hudplus.hud_add(player, hud_name, def)
end end
-- [function] Remove HUD -- [function] Remove HUD
function hudplus.hud_remove(player, hud_name) function hudlib.hud_remove(player, hud_name)
assert(player and hud_name, "hudplus.hud_remove: Invalid parameters") assert(player and hud_name, "hudlib.hud_remove: Invalid parameters")
if type(player) == "string" then if type(player) == "string" then
player = minetest.get_player_by_name(player) player = minetest.get_player_by_name(player)
end end
local name = player:get_player_name() local name = player:get_player_name()
local hud = hudplus.hud_get(name, hud_name) local hud = hudlib.hud_get(name, hud_name)
if hud then if hud then
player:hud_remove(hud.id) player:hud_remove(hud.id)
return true return true
@ -150,8 +150,8 @@ function hudplus.hud_remove(player, hud_name)
end end
-- [function] Change HUD -- [function] Change HUD
function hudplus.hud_change(player, hud_name, key, val) function hudlib.hud_change(player, hud_name, key, val)
assert(player and hud_name and key, "hudplus.hud_change: Invalid parameters") assert(player and hud_name and key, "hudlib.hud_change: Invalid parameters")
if type(player) == "string" then if type(player) == "string" then
player = minetest.get_player_by_name(player) player = minetest.get_player_by_name(player)
end end
@ -160,30 +160,30 @@ function hudplus.hud_change(player, hud_name, key, val)
if key == "pos" then key = "position" end if key == "pos" then key = "position" end
local name = player:get_player_name() local name = player:get_player_name()
local hud = hudplus.hud_get(name, hud_name) local hud = hudlib.hud_get(name, hud_name)
if hud then if hud then
player:hud_change(hud.id, key, val) player:hud_change(hud.id, key, val)
-- Update def in hud list -- Update def in hud list
hud.def[key] = val hud.def[key] = val
hudplus.hud_set(player, hud_name, "def", hud.def) hudlib.hud_set(player, hud_name, "def", hud.def)
return true return true
end end
end end
-- [function] Hide HUD -- [function] Hide HUD
function hudplus.hud_hide(player, hud_name) function hudlib.hud_hide(player, hud_name)
assert(player and hud_name, "hudplus.hud_hide: Invalid parameters") assert(player and hud_name, "hudlib.hud_hide: Invalid parameters")
if type(player) == "string" then if type(player) == "string" then
player = minetest.get_player_by_name(player) player = minetest.get_player_by_name(player)
end end
local name = player:get_player_name() local name = player:get_player_name()
local hud = hudplus.hud_get(name, hud_name) local hud = hudlib.hud_get(name, hud_name)
if hud then if hud then
if hud.show == true then if hud.show == true then
player:hud_remove(hud.id) player:hud_remove(hud.id)
hudplus.hud_set(player, hud_name, "show", false) hudlib.hud_set(player, hud_name, "show", false)
local def = hud.def local def = hud.def
if def.on_hide then if def.on_hide then
@ -196,23 +196,23 @@ function hudplus.hud_hide(player, hud_name)
end end
-- [function] Show HUD -- [function] Show HUD
function hudplus.hud_show(player, hud_name) function hudlib.hud_show(player, hud_name)
assert(player and hud_name, "hudplus.hud_hide: Invalid parameters") assert(player and hud_name, "hudlib.hud_hide: Invalid parameters")
if type(player) == "string" then if type(player) == "string" then
player = minetest.get_player_by_name(player) player = minetest.get_player_by_name(player)
end end
local name = player:get_player_name() local name = player:get_player_name()
local hud = hudplus.hud_get(name, hud_name) local hud = hudlib.hud_get(name, hud_name)
if hud then if hud then
if hud.show == false then if hud.show == false then
hudplus.hud_set(player, hud_name, "id", player:hud_add(hud.def)) hudlib.hud_set(player, hud_name, "id", player:hud_add(hud.def))
hudplus.hud_set(player, hud_name, "show", true) hudlib.hud_set(player, hud_name, "show", true)
local def = hud.def local def = hud.def
if def.hide_after then if def.hide_after then
hudplus.after("hide_"..hud_name, def.hide_after, function() hudlib.after("hide_"..hud_name, def.hide_after, function()
hudplus.hud_hide(player, hud_name) hudlib.hud_hide(player, hud_name)
if def.on_hide then if def.on_hide then
def.on_hide() def.on_hide()
@ -230,11 +230,11 @@ function hudplus.hud_show(player, hud_name)
end end
-- [function] Register HUD -- [function] Register HUD
function hudplus.register(hud_name, def) function hudlib.register(hud_name, def)
local when = def.show_on or "join" local when = def.show_on or "join"
if when == "join" then if when == "join" then
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
hudplus.hud_add(player, hud_name, def) hudlib.hud_add(player, hud_name, def)
if def.on_add then if def.on_add then
def.on_add(player) def.on_add(player)
@ -242,7 +242,7 @@ function hudplus.register(hud_name, def)
end) end)
elseif when == "now" then elseif when == "now" then
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(minetest.get_connected_players()) do
if hudplus.hud_add(player, hud_name, def) then if hudlib.hud_add(player, hud_name, def) then
if def.on_add then if def.on_add then
def.on_add(player) def.on_add(player)
end end
@ -252,8 +252,8 @@ function hudplus.register(hud_name, def)
end end
-- [function] Unregister HUD -- [function] Unregister HUD
function hudplus.unregister(hud_name) function hudlib.unregister(hud_name)
for player, huds in pairs(huds) do for player, huds in pairs(huds) do
hudplus.hud_remove(minetest.get_player_by_name(player), hud_name) hudlib.hud_remove(minetest.get_player_by_name(player), hud_name)
end end
end end

View File

@ -12,5 +12,4 @@ Planned
- API to register HUDs displayed to all players - API to register HUDs displayed to all players
- Statbar API - Statbar API
Forum: https://forum.minetest.net/viewtopic.php?f=9&t=16864&p=257211#p257211 GitHub: https://github.com/octacian/hudlib
GitHub: https://github.com/octacian/hudplus

View File

@ -1,28 +1,28 @@
-- hudplus/init.lua -- hudlib/init.lua
hudplus = {} hudlib = {}
hudplus.VERSION = 0.1 hudlib.VERSION = 0.1
hudplus.RELEASE = "alpha" hudlib.RELEASE = "alpha"
local modpath = minetest.get_modpath("hudplus") local modpath = minetest.get_modpath("hudlib")
-- [function] Log -- [function] Log
function hudplus.log(content, log_type) function hudlib.log(content, log_type)
if not content then return false end if not content then return false end
if log_type == nil then log_type = "action" end if log_type == nil then log_type = "action" end
minetest.log(log_type, "[HUD Plus] "..content) minetest.log(log_type, "[HUD Library] "..content)
end end
--------------------- ---------------------
---- CHATCOMMAND ---- ---- CHATCOMMAND ----
--------------------- ---------------------
-- [register cmd] /hudplus -- [register cmd] /hudlib
minetest.register_chatcommand("hudplus", { minetest.register_chatcommand("hudlib", {
description = "Manage HUD Plus", description = "Check HUD Library version",
func = function(name) func = function(name)
return true, "HUD Plus: Version "..tostring(hudplus.VERSION).." ("..hudplus.RELEASE..")" return true, "HUD Library: Version "..tostring(hudlib.VERSION).." ("..hudlib.RELEASE..")"
end, end,
}) })

View File

@ -1 +1 @@
name = hudplus name = hudlib