Add inventory element API
Implements an API specifically for the purpose of manipulating inventory HUD elements.
This commit is contained in:
parent
04c8af7669
commit
a3520ea320
27
doc/Inventory.md
Normal file
27
doc/Inventory.md
Normal file
@ -0,0 +1,27 @@
|
||||
# Inventory Elements
|
||||
The API for inventory HUD elements is based off of the main API, however, it makes things even easier when it comes to manipulating the inventory HUDs. The main reason why this sub-API makes things easier, is that it presents a table for each inventory element, suited with several functions to manipulate it without requiring that the identification of the element be provided each time you make a change.
|
||||
|
||||
#### `list_inv`
|
||||
__Usage:__ `hudlib.list_inv(<player (userdata or string)>)`
|
||||
|
||||
Lists all inventory elements attached to a player.
|
||||
|
||||
#### `get_inv`
|
||||
__Usage:__ `hudlib.get_inv(<player (userdata or string)>, <hud name (string)>)`
|
||||
|
||||
Returns a table containing all of the helpers which allow manipulation of the inventory element. Helpers can be called in this method: `hudlib.get_inv(...).remove()` (see below for all available helpers).
|
||||
|
||||
__Helpers:__
|
||||
- `remove()` - removes the inventory HUD element
|
||||
- `hide()` - hides the inventory
|
||||
- `show()` - shows the inventory (if it is hidden)
|
||||
- `set_pos(x, y)` - sets the position of the inventory
|
||||
- `set_name(name)` - changes the inventory shown
|
||||
- `set_number(number)` - sets number of items shown
|
||||
- `set_item(number)` - sets selected item
|
||||
- `set_dir(number)` - sets direction in which inventory is drawn
|
||||
|
||||
#### `add_inv`
|
||||
__Usage:__ `hudlib.add_inv(<player (userdata or string)>, <hud name (string)>, <definition (table)>)`
|
||||
|
||||
Adds an inventory HUD element and returns the helper table described in the `get_inv` documentation above. Automatically sets the type of the element. The inventory name may be specified by the `name` rather than `text` attribute. See documentation for `add` in `HUD.md` for information on other custom attributes.
|
2
init.lua
2
init.lua
@ -39,3 +39,5 @@ dofile(modpath.."/hud.lua")
|
||||
dofile(modpath.."/image.lua")
|
||||
-- Load Text API
|
||||
dofile(modpath.."/text.lua")
|
||||
-- Load Inventory API
|
||||
dofile(modpath.."/inventory.lua")
|
||||
|
63
inventory.lua
Normal file
63
inventory.lua
Normal file
@ -0,0 +1,63 @@
|
||||
-- hudlib/inventory.lua
|
||||
|
||||
-- [local function] Generate methods
|
||||
local function gen(name, hud_name)
|
||||
return {
|
||||
remove = function()
|
||||
hudlib.remove(name, hud_name)
|
||||
end,
|
||||
hide = function()
|
||||
hudlib.hide(name, hud_name)
|
||||
end,
|
||||
show = function()
|
||||
hudlib.show(name, hud_name)
|
||||
end,
|
||||
set_pos = function(x, y)
|
||||
hudlib.change(name, hud_name, "position", {x = x, y = y})
|
||||
end,
|
||||
set_name = function(name)
|
||||
hudlib.change(name, hud_name, "text", name)
|
||||
end,
|
||||
set_number = function(num)
|
||||
hudlib.change(name, hud_name, "number", num)
|
||||
end,
|
||||
set_item = function(item)
|
||||
hudlib.change(name, hud_name, "item", item)
|
||||
end,
|
||||
set_dir = function(dir)
|
||||
hudlib.change(name, hud_name, "direction", dir)
|
||||
end,
|
||||
set_offset = function(x, y)
|
||||
hudlib.change(name, hud_name, "offset", {x = x, y = y})
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
-- [function] List inventory elements
|
||||
function hudlib.list_inv(name)
|
||||
local huds = hudlib.list(name)
|
||||
local elems = {}
|
||||
|
||||
for _, hud in pairs(huds) do
|
||||
if hud.def.hud_elem_type == "inventory" then
|
||||
elems[#elems + 1] = _
|
||||
end
|
||||
end
|
||||
|
||||
return elems
|
||||
end
|
||||
|
||||
-- [function] Get inventory element
|
||||
function hudlib.get_inv(name, hud_name)
|
||||
return gen(name, hud_name)
|
||||
end
|
||||
|
||||
-- [function] Add inventory element
|
||||
function hudlib.add_inv(name, hud_name, def)
|
||||
def.type = "inventory"
|
||||
def.text = def.name or def.text
|
||||
|
||||
if hudlib.add(name, hud_name, def) then
|
||||
return gen(name, hud_name)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user