Add alternative statbar display modes

For those nostalgists. ;-)
This commit is contained in:
Wuzzy 2015-05-19 23:35:47 +02:00
parent 897d40e47b
commit 4ac3b25539
3 changed files with 93 additions and 41 deletions

View File

@ -58,7 +58,15 @@ This mod can be configured by editing minetest.conf. Currently, the following se
breath=0, health=1 breath=0, health=1
This places the breath bar at the left side, and the health bar to the right side. 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.
API: API:
---- ----
The API is used to add your own custom HUD bars. The API is used to add your own custom HUD bars.
@ -68,6 +76,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

@ -41,6 +41,17 @@ if alignment_pattern ~= nil then
end end
end end
hb.settings.bar_type = "progress_bar"
local bar_type = minetest.setting_getbool("hudbars_bar_type")
if bar_type ~= nil then
hb.settings.bar_type = bar_type
if bar_type ~= "progress_bar" and bar_type ~= "statbar_classic" and bar_type ~= "statbar_modern" then
minetest.log("error", "[hudbars] Invalid value for hudbars_alignment_pattern! Using default (progress_bar).")
end
end
hb.settings.autohide_breath = true hb.settings.autohide_breath = true
local autohide_breath = minetest.setting_getbool("hudbars_autohide_breath") local autohide_breath = minetest.setting_getbool("hudbars_autohide_breath")
if autohide_breath ~= nil then if autohide_breath ~= nil then
@ -67,7 +78,11 @@ function hb.value_to_barlength(value, max)
if max == 0 then if max == 0 then
return 0 return 0
else else
if hb.settings.bar_type == "progress_bar" then
return math.ceil((value/max) * hb.settings.max_bar_length) return math.ceil((value/max) * hb.settings.max_bar_length)
else
return math.ceil((value/max) * 20)
end
end end
end end
@ -147,6 +162,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,
@ -165,14 +181,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 = 20,
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,
@ -182,6 +218,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
@ -264,7 +301,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
@ -279,6 +316,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)
@ -287,17 +325,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
@ -309,14 +350,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
@ -336,7 +379,7 @@ 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B