From 4ac3b2553910f6f3b7f12ee340beb3e6897ee8c5 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 19 May 2015 23:35:47 +0200 Subject: [PATCH] Add alternative statbar display modes For those nostalgists. ;-) --- README.txt | 11 ++- init.lua | 123 +++++++++++++++++++---------- textures/hudbars_bgicon_health.png | Bin 0 -> 302 bytes 3 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 textures/hudbars_bgicon_health.png diff --git a/README.txt b/README.txt index f349690..bf7777f 100644 --- a/README.txt +++ b/README.txt @@ -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) diff --git a/init.lua b/init.lua index e6dbe8b..71e7275 100644 --- a/init.lua +++ b/init.lua @@ -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 - return math.ceil((value/max) * hb.settings.max_bar_length) + 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,41 +162,63 @@ 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 - ids.bg = player:hud_add({ - hud_elem_type = "image", - position = pos, - scale = bgscale, - text = "hudbars_bar_background.png", - alignment = {x=1,y=1}, - offset = { x = offset.x - 1, y = offset.y - 1 }, - }) - if textures.icon ~= nil then - ids.icon = player:hud_add({ + if hb.settings.bar_type == "progress_bar" then + ids.bg = player:hud_add({ hud_elem_type = "image", position = pos, - scale = iconscale, - text = textures.icon, - alignment = {x=-1,y=1}, - offset = { x = offset.x - 3, y = offset.y }, + scale = bgscale, + text = "hudbars_bar_background.png", + alignment = {x=1,y=1}, + offset = { x = offset.x - 1, y = offset.y - 1 }, }) + if textures.icon ~= nil then + ids.icon = player:hud_add({ + hud_elem_type = "image", + position = pos, + scale = iconscale, + text = textures.icon, + alignment = {x=-1,y=1}, + 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, }) - ids.text = player:hud_add({ - hud_elem_type = "text", - position = pos, - text = text, - alignment = {x=1,y=1}, - number = text_color, - direction = 0, - offset = { x = offset.x + 2, y = offset.y }, + if hb.settings.bar_type == "progress_bar" then + ids.text = player:hud_add({ + hud_elem_type = "text", + position = pos, + text = text, + alignment = {x=1,y=1}, + number = text_color, + 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,10 +316,12 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value) hudtable.hudstate[name].barlength = new_barlength end - 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) - hudtable.hudstate[name].text = new_text + 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) + hudtable.hudstate[name].text = new_text + end end end end @@ -292,12 +331,14 @@ 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 hudtable.hudids[name].icon ~= nil then - player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0}) + 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].text, "text", "") 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", "") 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 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}) + 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].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)) - player:hud_change(hudtable.hudids[name].text, "text", tostring(string.format(hudtable.format_string, hudtable.label, 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 diff --git a/textures/hudbars_bgicon_health.png b/textures/hudbars_bgicon_health.png new file mode 100644 index 0000000000000000000000000000000000000000..e2be2768e569748d3d19bc3fd02e8bf36f5bfa03 GIT binary patch literal 302 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5he4R}c>anMpx^^f z7srr_IY%!Wt~#P1z?SfSf|H6XhjMUF(nP1G4)!fyTJAl$uhp1yZF~OBr0&d?|HUb8 ztPZuo@vZs}>wN_UXGosmQF2=E`}Nk9O*$K{HO*YO)ouM-J^{g`9*w07rZh*az&zt8kNnXyH@^QZnikZw^;`Xc6*GSF+1Ni!UT}Sx6G#5x+uIUj zuU|i4bH2W8VsTr?o_U81jRYl`&h7jt!GA!4`TbwrH+$O+pWW$IEKpEtnqypauHank yq5Q_U*_Wc6T@;kkZ_6{ftDiULZ=C=482iEur