Update HUD bars mod to 1.2.0

This commit is contained in:
Wuzzy 2015-05-21 16:48:29 +02:00
parent f7ac3ef019
commit 2ee0832640
6 changed files with 258 additions and 93 deletions

View File

@ -1,4 +1,4 @@
API documentation for the HUD bars mod 1.1.0 API documentation for the HUD bars mod 1.2.0
============================================ ============================================
## Introduction ## Introduction
@ -59,8 +59,9 @@ manually in a reliable way.
* `text_color`: A 3-octet number defining the color of the text. The octets denote, in this order red, green and blue and range from `0x00` (complete lack of this component) to `0xFF` (full intensity of this component). Example: `0xFFFFFF` for white. * `text_color`: A 3-octet number defining the color of the text. The octets denote, in this order red, green and blue and range from `0x00` (complete lack of this component) to `0xFF` (full intensity of this component). Example: `0xFFFFFF` for white.
* `label`: A string which is displayed on the HUD bar itself to describe the HUD bar. Try to keep this string short. * `label`: A string which is displayed on the HUD bar itself to describe the HUD bar. Try to keep this string short.
* `textures`: A table with the following fields: * `textures`: A table with the following fields:
* `bar`: The file name of the bar image (as string). * `bar`: The file name of the bar image (as string). This is only used for the `progress_bar` bar type (see `README.txt`, settings section).
* `icon`: The file name of the icon, as string. This field can be `nil`, in which case no icon will be used. * `icon`: The file name of the icon, as string. For the `progress_bar` type, it is shown as single image left of the bar, for the two statbar bar types, it is used as the statbar icon and will be repeated. This field can be `nil`, in which case no icon will be used, but this is not recommended, because the HUD bar will be invisible if the one of the statbar bar types is used.
* `bgicon`: The file name of the background icon, it is used as the background for the modern statbar mode only. This field can be `nil`, in which case no background icon will be displayed in this mode.
* `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_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 * `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
* `default_start_hidden`: The HUD bar will be initially start hidden by default when added to a player. Use `hb.unhide_hudbar` to unhide it. * `default_start_hidden`: The HUD bar will be initially start hidden by default when added to a player. Use `hb.unhide_hudbar` to unhide it.

View File

@ -1,6 +1,6 @@
Minetest mod: HUD bars Minetest mod: HUD bars
====================== ======================
Version: 1.1.0 Version: 1.2.0
This software uses semantic versioning, as defined by version 2.0.0 of the SemVer This software uses semantic versioning, as defined by version 2.0.0 of the SemVer
standard. <http://semver.org/> standard. <http://semver.org/>
@ -35,6 +35,57 @@ This mod can be configured by editing minetest.conf. Currently, the following se
the breath bar will be automatically hidden shortly after the breathbar has been filled up. If set the breath bar will be automatically hidden shortly after the breathbar has been filled up. If set
to “false”, the breath bar will always be displayed. The default value is “true”. to “false”, the breath bar will always be displayed. The default value is “true”.
- hudbars_sorting: This setting allows you to specify the “slot” positions of the HUD bars manually.
The setting has to be specified as a comma-seperated list of key=value pairs, where a key refers to the
identifier of a HUD bar and the value refers to the slot number of where the HUD bar should be placed.
The slot number must be an integer greater of equal to 0. The slot positions start (slot 0) at the
bottom (nearest to hotbar in default configuration) left side, the following slot 1 is at the right
side, slot `2` is on the right side again, but placed over the first HUD bar (slot 0), and it goes on,
in a zig-zag pattern.
All HUD bars to which no sorting rule has been applied will fill in all slots which have not been occupied
by the HUD bars specified in this setting, the slots will be filled in from the lowest slot number.
Note that the order of those remaining HUD bars is *not* fixed, it basically just boils down on which mod
“came” first. Don't worry, the mod will still work perfectly fine, this setting is entirely optional.
Be careful not to use slot indices twice, or else different HUD bars will be drawn over each other!
If this setting is not set, by default the health and breath bar are displayed at slot positions 0 and 1,
respectively (health bar at left bottom-most positoin, breath bar right from it). All other HUD bars are
placed automatically.
Example value:
breath=0, health=1
This places the breath bar at the left side, and the health bar to the right side.
- hudbars_bar_type: Specifies the style of bars. You can select between the default progress-bar-like bars and the good old statbars
like you know from vanilla Minetest. Note that the classic and modern statbars are still a little bit experimental.
These values are possible:
- progress_bar: A horizontal progress-bar-like bar with a label, showing numerical value (current, maximum), and an icon.
These bars usually convey the most information. This is the default and recommended value..
- statbar_classic: Classic statbar, like in vanilla Minetest. Made out of up to 20 half-symbols. Those bars represent the vague ratio between
the current value and the maximum value. 1 half-symbol stands for approximately 5% of the maximum value.
- statbar_modern: Like the classic statbar, but also supports background images, this kind of statbar may be considered to be more user-friendly
than the classic statbar. This bar type closely resembles the [hud] mod.
- hudbars_vmargin: The vertical distance between two HUD bars in pixels (default: 24)
- hudbars_tick: The number of seconds between two updates of the HUD bars. Increase this number if you have a slow server (default: 0.1)
Position settings:
With these settings you can configure the positions of the HUD bars. All settings must be specified as a number.
The pos settings are specified as a floating-point number between 0 to 1 each, the start_offset settings are
specified as whole numbers, they specify a number of pixels.
The left and right variants are used for the zig-zag mode. In the stack_up and stack_down modes, only the left variant is used for
the base position
- hudbars_pos_left_x, hudbars_pos_left_y: Screen position (x and y) of the left HUD bar in zigzag mode. 0 is left-most/top, 1 is right-most/bottom.
Defaults: 0.5 (x) and 1 (y)
- hudbars_pos_right_x, hudbars_pos_right_y: Same as above, but for the right one.
Defaults: 0.5 and 1.
- hudbars_start_offset_left_x, hudbars_start_offset_left_y: Offset in pixels from the basic screen position specified in hudbars_pos_left_x/hudbars_pos_left_y.
Defaults: -175 and -86
- hudbars_start_offset_right_x, hudbars_start_offset_right_y: Same as above, but for the right one.
Defaults: 15 and -86
API: API:
---- ----
@ -45,6 +96,7 @@ Documentation for the API of this mod can be found in API.md.
License of textures: License of textures:
-------------------- --------------------
hudbars_icon_health.png - celeron55 (CC BY-SA 3.0), modified by BlockMen hudbars_icon_health.png - celeron55 (CC BY-SA 3.0), modified by BlockMen
hudbars_bgicon_health.png - celeron55 (CC BY-SA 3.0), modified by BlockMen
hudbars_icon_breath.png - kaeza (WTFPL), modified by BlockMen hudbars_icon_breath.png - kaeza (WTFPL), modified by BlockMen
hudbars_bar_health.png - Wuzzy (WTFPL) hudbars_bar_health.png - Wuzzy (WTFPL)
hudbars_bar_breath.png - Wuzzy (WTFPL) hudbars_bar_breath.png - Wuzzy (WTFPL)

View File

@ -51,3 +51,12 @@ as of version 2.0.0 of the standard <http://semver.org/>.
1.1.0 1.1.0
----- -----
- Add boolean minetest.conf setting support (hudbars_autohide_breathbar) to control whether the breath bar is automatically hidden when full (default: yes) - Add boolean minetest.conf setting support (hudbars_autohide_breathbar) to control whether the breath bar is automatically hidden when full (default: yes)
1.2.0
-----
- New setting: hudbars_sorting. You can now manually sort all the HUD bars. Useful if you don't like automatic order
- New setting: hudbars_bar_type. You now can change the appearance of the HUD bars.
- New HUD bar types, slightly experimental: Classic statbars and modern [hud]-style statbars
- New experimental/unfinished setting: hudbars_alignment_pattern: You can now make the HUD bars stack vertically instead of the current zig-zag pattern. Note you probably need to change source code to productively use this feature
- Various position-related HUD bar settings (see README.txt)
- Remove hudbars.conf support and hudbars.conf.example (was never officially supported anyways)

View File

@ -1,24 +0,0 @@
-- HUD bars example config file
-------------------------------
-- Currently you can customize the starting position of the first bottom two HUD bars
-- and the vertical margin. That's all.
-- Remove the two dashes to activate a setting. Lua syntax is used.
-- The examples are all equal to the mod defaults
-------------------------------------------------------------------------------------------------------
-- Vertical space between two HUD bars
-- hb.settings.vmargin = 24
-- Pos of the first HUD bar the the left (“pos”, as in hud definition of hud_add of Minetest Lua API)
-- hb.settings.pos_left = { x=0.5, y=1 }
-- Pos of the first HUD bar the the right
-- hb.settings.pos_right= { x = 0.5, y = 1 }
-- Offset of the first HUD bar to the left (“offset”, as in HUD definition)
-- hb.settings.start_offset_left = { x = -175, y = -70 }
-- Offset of the first HUD bar to the right
-- hb.settings_start_offset_right = { x = 15, y = -70 }

View File

@ -5,24 +5,90 @@ hb.hudtables = {}
-- number of registered HUD bars -- number of registered HUD bars
hb.hudbars_count = 0 hb.hudbars_count = 0
-- table which records which HUD bar slots have been “registered” so far; used for automatic positioning
hb.registered_slots = {}
hb.settings = {} hb.settings = {}
-- default settings function hb.load_setting(sname, stype, defaultval, valid_values)
local sval
if stype == "string" then
sval = minetest.setting_get(sname)
elseif stype == "bool" then
sval = minetest.setting_getbool(sname)
elseif stype == "number" then
sval = tonumber(minetest.setting_get(sname))
end
if sval ~= nil then
if valid_values ~= nil then
local valid = false
for i=1,#valid_values do
if sval == valid_values[i] then
valid = true
end
end
if not valid then
minetest.log("error", "[hudbars] Invalid value for "..sname.."! Using default value ("..tostring(defaultval)..").")
return defaultval
else
return sval
end
else
return sval
end
else
return defaultval
end
end
-- (hardcoded) default settings
hb.settings.max_bar_length = 160 hb.settings.max_bar_length = 160
hb.settings.statbar_length = 20
-- statbar positions -- statbar positions
hb.settings.pos_left = { x=0.5, y=1 } hb.settings.pos_left = {}
hb.settings.pos_right= { x = 0.5, y = 1 } hb.settings.pos_right = {}
hb.settings.start_offset_left = { x = -175, y = -86 } hb.settings.start_offset_left = {}
hb.settings.start_offset_right = { x = 15, y = -86 } hb.settings.start_offset_right= {}
hb.settings.pos_left.x = hb.load_setting("hudbars_pos_left_x", "number", 0.5)
hb.settings.pos_left.y = hb.load_setting("hudbars_pos_left_y", "number", 1)
hb.settings.pos_right.x = hb.load_setting("hudbars_pos_right_x", "number", 0.5)
hb.settings.pos_right.y = hb.load_setting("hudbars_pos_right_y", "number", 1)
hb.settings.start_offset_left.x = hb.load_setting("hudbars_start_offset_left_x", "number", -175)
hb.settings.start_offset_left.y = hb.load_setting("hudbars_start_offset_left_y", "number", -86)
hb.settings.start_offset_right.x = hb.load_setting("hudbars_start_offset_right_x", "number", 15)
hb.settings.start_offset_right.y = hb.load_setting("hudbars_start_offset_right_y", "number", -86)
hb.settings.vmargin = 24 hb.settings.vmargin = hb.load_setting("hudbars_tick", "number", 24)
hb.settings.tick = 0.1 hb.settings.tick = hb.load_setting("hudbars_tick", "number", 0.1)
hb.settings.autohide_breath = true --[[
local autohide_breath = minetest.setting_getbool("hudbars_autohide_breath") - hudbars_alignment_pattern: This setting changes the way the HUD bars are ordered on the display. You can choose
if autohide_breath ~= nil then between a zig-zag pattern or a vertically stacked pattern.
hb.settings.autohide_breath = autohide_breath The following values are allowed:
zigzag: Starting from the left bottom, the next is right from the first,
the next is above the first, the next is right of the third, etc.
This is the default.
stack_up: The HUD bars are stacked vertically, going upwards.
stack_down: The HUD bars are stacked vertically, going downwards.
]]
-- Misc. settings
hb.settings.alignment_pattern = hb.load_setting("hudbars_alignment_pattern", "string", "zigzag", {"zigzag", "stack_up", "stack_down"})
hb.settings.bar_type = hb.load_setting("hudbars_bar_type", "string", "progress_bar", {"progress_bar", "statbar_classic", "statbar_modern"})
hb.settings.autohide_breath = hb.load_setting("hudbars_autohide_breath", "bool", true)
local sorting = minetest.setting_get("hudbars_sorting")
if sorting ~= nil then
hb.settings.sorting = {}
hb.settings.sorting_reverse = {}
for k,v in string.gmatch(sorting, "(%w+)=(%w+)") do
hb.settings.sorting[k] = tonumber(v)
hb.settings.sorting_reverse[tonumber(v)] = k
end
else
hb.settings.sorting = { ["health"] = 0, ["breath"] = 1 }
hb.settings.sorting_reverse = { [0] = "health", [1] = "breath" }
end end
-- Table which contains all players with active default HUD bars (only for internal use) -- Table which contains all players with active default HUD bars (only for internal use)
@ -32,7 +98,17 @@ function hb.value_to_barlength(value, max)
if max == 0 then if max == 0 then
return 0 return 0
else else
return math.ceil((value/max) * hb.settings.max_bar_length) if hb.settings.bar_type == "progress_bar" then
local x
if value < 0 then x=-0.5 else x = 0.5 end
local ret = math.modf((value/max) * hb.settings.max_bar_length + x)
return ret
else
local x
if value < 0 then x=-0.5 else x = 0.5 end
local ret = math.modf((value/max) * hb.settings.statbar_length + x)
return ret
end
end end
end end
@ -40,22 +116,52 @@ function hb.get_hudtable(identifier)
return hb.hudtables[identifier] return hb.hudtables[identifier]
end end
function hb.get_hudbar_position_index(identifier)
if hb.settings.sorting[identifier] ~= nil then
return hb.settings.sorting[identifier]
else
local i = 0
while true do
if hb.registered_slots[i] ~= true and hb.settings.sorting_reverse[i] == nil then
return i
end
i = i + 1
end
end
end
function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string) function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string)
local hudtable = {} local hudtable = {}
local pos, offset local pos, offset
if hb.hudbars_count % 2 == 0 then local index = math.floor(hb.get_hudbar_position_index(identifier))
hb.registered_slots[index] = true
if hb.settings.alignment_pattern == "stack_up" then
pos = hb.settings.pos_left pos = hb.settings.pos_left
offset = { offset = {
x = hb.settings.start_offset_left.x, x = hb.settings.start_offset_left.x,
y = hb.settings.start_offset_left.y - hb.settings.vmargin * math.floor(hb.hudbars_count/2) y = hb.settings.start_offset_left.y - hb.settings.vmargin * index
}
elseif hb.settings.alignment_pattern == "stack_down" then
pos = hb.settings.pos_left
offset = {
x = hb.settings.start_offset_left.x,
y = hb.settings.start_offset_left.y + hb.settings.vmargin * index
}
else
if index % 2 == 0 then
pos = hb.settings.pos_left
offset = {
x = hb.settings.start_offset_left.x,
y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2)
} }
else else
pos = hb.settings.pos_right pos = hb.settings.pos_right
offset = { offset = {
x = hb.settings.start_offset_right.x, x = hb.settings.start_offset_right.x,
y = hb.settings.start_offset_right.y - hb.settings.vmargin * math.floor((hb.hudbars_count-1)/2) y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2)
} }
end end
end
if format_string == nil then if format_string == nil then
format_string = "%s: %d/%d" format_string = "%s: %d/%d"
end end
@ -82,6 +188,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
barnumber = hb.value_to_barlength(start_value, start_max) barnumber = hb.value_to_barlength(start_value, start_max)
text = string.format(format_string, label, start_value, start_max) text = string.format(format_string, label, start_value, start_max)
end end
if hb.settings.bar_type == "progress_bar" then
ids.bg = player:hud_add({ ids.bg = player:hud_add({
hud_elem_type = "image", hud_elem_type = "image",
position = pos, position = pos,
@ -100,14 +207,34 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
offset = { x = offset.x - 3, y = offset.y }, offset = { x = offset.x - 3, y = offset.y },
}) })
end end
elseif hb.settings.bar_type == "statbar_modern" then
if textures.bgicon ~= nil then
ids.bg = player:hud_add({
hud_elem_type = "statbar",
position = pos,
scale = bgscale,
text = textures.bgicon,
number = hb.settings.statbar_length,
alignment = {x=-1,y=-1},
offset = { x = offset.x, y = offset.y },
})
end
end
local bar_image
if hb.settings.bar_type == "progress_bar" then
bar_image = textures.bar
elseif hb.settings.bar_type == "statbar_classic" or hb.settings.bar_type == "statbar_modern" then
bar_image = textures.icon
end
ids.bar = player:hud_add({ ids.bar = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = pos, position = pos,
text = textures.bar, text = bar_image,
number = barnumber, number = barnumber,
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = offset, offset = offset,
}) })
if hb.settings.bar_type == "progress_bar" then
ids.text = player:hud_add({ ids.text = player:hud_add({
hud_elem_type = "text", hud_elem_type = "text",
position = pos, position = pos,
@ -117,6 +244,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
direction = 0, direction = 0,
offset = { x = offset.x + 2, y = offset.y }, offset = { x = offset.x + 2, y = offset.y },
}) })
end
-- Do not forget to update hb.get_hudbar_state if you add new fields to the state table -- Do not forget to update hb.get_hudbar_state if you add new fields to the state table
state.hidden = start_hidden state.hidden = start_hidden
state.value = start_value state.value = start_value
@ -199,7 +327,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
end end
if hudtable.hudstate[name].hidden == false then if hudtable.hudstate[name].hidden == false then
if max_changed then if max_changed and hb.settings.bar_type == "progress_bar" then
if hudtable.hudstate[name].max == 0 then if hudtable.hudstate[name].max == 0 then
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0}) player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
else else
@ -214,6 +342,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
hudtable.hudstate[name].barlength = new_barlength hudtable.hudstate[name].barlength = new_barlength
end end
if hb.settings.bar_type == "progress_bar" then
local new_text = string.format(hudtable.format_string, hudtable.label, new_value, new_max_value) local new_text = string.format(hudtable.format_string, hudtable.label, new_value, new_max_value)
if new_text ~= hudtable.hudstate[name].text then if new_text ~= hudtable.hudstate[name].text then
player:hud_change(hudtable.hudids[name].text, "text", new_text) player:hud_change(hudtable.hudids[name].text, "text", new_text)
@ -222,17 +351,20 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
end end
end end
end end
end
function hb.hide_hudbar(player, identifier) function hb.hide_hudbar(player, identifier)
local name = player:get_player_name() local name = player:get_player_name()
local hudtable = hb.get_hudtable(identifier) local hudtable = hb.get_hudtable(identifier)
if(hudtable.hudstate[name].hidden == false) then if(hudtable.hudstate[name].hidden == false) then
if hb.settings.bar_type == "progress_bar" then
if hudtable.hudids[name].icon ~= nil then if hudtable.hudids[name].icon ~= nil then
player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0}) player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0})
end end
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0}) player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
player:hud_change(hudtable.hudids[name].bar, "number", 0)
player:hud_change(hudtable.hudids[name].text, "text", "") player:hud_change(hudtable.hudids[name].text, "text", "")
end
player:hud_change(hudtable.hudids[name].bar, "number", 0)
hudtable.hudstate[name].hidden = true hudtable.hudstate[name].hidden = true
end end
end end
@ -244,14 +376,16 @@ function hb.unhide_hudbar(player, identifier)
local name = player:get_player_name() local name = player:get_player_name()
local value = hudtable.hudstate[name].value local value = hudtable.hudstate[name].value
local max = hudtable.hudstate[name].max local max = hudtable.hudstate[name].max
if hb.settings.bar_type == "progress_bar" then
if hudtable.hudids[name].icon ~= nil then if hudtable.hudids[name].icon ~= nil then
player:hud_change(hudtable.hudids[name].icon, "scale", {x=1,y=1}) player:hud_change(hudtable.hudids[name].icon, "scale", {x=1,y=1})
end end
if hudtable.hudstate[name].max ~= 0 then if hudtable.hudstate[name].max ~= 0 then
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1}) player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
end end
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))) player:hud_change(hudtable.hudids[name].text, "text", tostring(string.format(hudtable.format_string, hudtable.label, value, max)))
end
player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max))
hudtable.hudstate[name].hidden = false hudtable.hudstate[name].hidden = false
end end
end end
@ -271,17 +405,10 @@ end
--register built-in HUD bars --register built-in HUD bars
if minetest.setting_getbool("enable_damage") then if minetest.setting_getbool("enable_damage") then
hb.register_hudbar("health", 0xFFFFFF, "Health", { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png" }, 20, 20, false) hb.register_hudbar("health", 0xFFFFFF, "Health", { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, 20, 20, false)
hb.register_hudbar("breath", 0xFFFFFF, "Breath", { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png" }, 10, 10, true) hb.register_hudbar("breath", 0xFFFFFF, "Breath", { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png" }, 10, 10, true)
end end
--load custom settings
local set = io.open(minetest.get_modpath("hudbars").."/hudbars.conf", "r")
if set then
dofile(minetest.get_modpath("hudbars").."/hudbars.conf")
set:close()
end
local function hide_builtin(player) local function hide_builtin(player)
local flags = player:hud_get_flags() local flags = player:hud_get_flags()
flags.healthbar = false flags.healthbar = false

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B