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
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:
----
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:
--------------------
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_bar_health.png - Wuzzy (WTFPL)
hudbars_bar_breath.png - Wuzzy (WTFPL)

View File

@ -41,6 +41,17 @@ if alignment_pattern ~= nil then
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
local autohide_breath = minetest.setting_getbool("hudbars_autohide_breath")
if autohide_breath ~= nil then
@ -67,7 +78,11 @@ function hb.value_to_barlength(value, max)
if max == 0 then
return 0
else
if hb.settings.bar_type == "progress_bar" then
return math.ceil((value/max) * hb.settings.max_bar_length)
else
return math.ceil((value/max) * 20)
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)
text = string.format(format_string, label, start_value, start_max)
end
if hb.settings.bar_type == "progress_bar" then
ids.bg = player:hud_add({
hud_elem_type = "image",
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 },
})
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({
hud_elem_type = "statbar",
position = pos,
text = textures.bar,
text = bar_image,
number = barnumber,
alignment = {x=-1,y=-1},
offset = offset,
})
if hb.settings.bar_type == "progress_bar" then
ids.text = player:hud_add({
hud_elem_type = "text",
position = pos,
@ -182,6 +218,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
direction = 0,
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
state.hidden = start_hidden
state.value = start_value
@ -264,7 +301,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
end
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
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
else
@ -279,6 +316,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
hudtable.hudstate[name].barlength = new_barlength
end
if hb.settings.bar_type == "progress_bar" then
local new_text = string.format(hudtable.format_string, hudtable.label, new_value, new_max_value)
if new_text ~= hudtable.hudstate[name].text then
player:hud_change(hudtable.hudids[name].text, "text", new_text)
@ -286,18 +324,21 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
end
end
end
end
end
function hb.hide_hudbar(player, identifier)
local name = player:get_player_name()
local hudtable = hb.get_hudtable(identifier)
if(hudtable.hudstate[name].hidden == false) then
if hb.settings.bar_type == "progress_bar" then
if hudtable.hudids[name].icon ~= nil then
player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0})
end
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", "")
end
player:hud_change(hudtable.hudids[name].bar, "number", 0)
hudtable.hudstate[name].hidden = true
end
end
@ -309,14 +350,16 @@ function hb.unhide_hudbar(player, identifier)
local name = player:get_player_name()
local value = hudtable.hudstate[name].value
local max = hudtable.hudstate[name].max
if hb.settings.bar_type == "progress_bar" then
if hudtable.hudids[name].icon ~= nil then
player:hud_change(hudtable.hudids[name].icon, "scale", {x=1,y=1})
end
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", hb.value_to_barlength(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
end
end
@ -336,7 +379,7 @@ end
--register built-in HUD bars
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)
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B