From 61cef21cd11f5c187eae908ce57a774c954dbfb1 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Wed, 7 Apr 2021 20:39:49 +0800 Subject: [PATCH] Add right-to-left statbars, for hunger and air. --- mods/HUD/hudbars/API.md | 5 +++-- mods/HUD/hudbars/init.lua | 33 +++++++++++++++++++++------------ mods/HUD/mcl_hbarmor/init.lua | 2 +- mods/PLAYER/mcl_hunger/init.lua | 6 +++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/mods/HUD/hudbars/API.md b/mods/HUD/hudbars/API.md index ca6144ad1..ee112eceb 100644 --- a/mods/HUD/hudbars/API.md +++ b/mods/HUD/hudbars/API.md @@ -17,7 +17,7 @@ To give you a *very* brief overview over this API, here is the basic workflow on In order to use this API, you should be aware of a few basic rules in order to understand it: * A HUD bar is an approximate graphical representation of the ratio of a current value and a maximum value, i.e. current health of 15 and maximum health of 20. A full HUD bar represents 100%, an empty HUD bar represents 0%. -* The current value must always be equal to or smaller then the maximum +* The current value must always be equal to or smaller then the maximum * Both current value and maximum must not be smaller than 0 * Both current value and maximum must be real numbers. So no NaN, infinity, etc. * The HUD bar will be hidden if the maximum equals 0. This is intentional. @@ -45,7 +45,7 @@ a vertical gradient. ### Icon A 16×16 image shown left of the HUD bar. This is optional. -### `hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)` +### `hb.register_hudbar(identifier, text_color, label, textures, direction, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)` This function registers a new custom HUD bar definition to the HUD bars mod, so it can be later used to be displayed, changed, hidden and unhidden on a per-player basis. Note this does not yet display the HUD bar. @@ -63,6 +63,7 @@ for more information. * `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. 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. +* `direction`: Either left to right(0), or right to left(1). * `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_start_hidden`: The HUD bar will be initially start hidden by default when added to a player. Use `hb.unhide_hudbar` to unhide it. diff --git a/mods/HUD/hudbars/init.lua b/mods/HUD/hudbars/init.lua index e1ff3f5f1..6f90aa03d 100644 --- a/mods/HUD/hudbars/init.lua +++ b/mods/HUD/hudbars/init.lua @@ -124,7 +124,7 @@ function hb.get_hudbar_position_index(identifier) end end -function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config) +function hb.register_hudbar(identifier, text_color, label, textures, direction, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config) minetest.log("action", "hb.register_hudbar: "..tostring(identifier)) local hudtable = {} local pos, offset @@ -133,30 +133,33 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta if hb.settings.alignment_pattern == "stack_up" then pos = hb.settings.pos_left offset = { - x = hb.settings.start_offset_left.x, + x = direction == 0 and hb.settings.start_offset_left.x or -hb.settings.start_offset_right.x, 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, + x = direction == 0 and hb.settings.start_offset_right.x or -hb.settings.start_offset_left.x, y = hb.settings.start_offset_left.y + hb.settings.vmargin * index } - else + else -- zigzag if index % 2 == 0 then pos = hb.settings.pos_left offset = { - x = hb.settings.start_offset_left.x, + -- -(24+18) = -42. using linear eq, -42 = -258m - 24. + x = direction == 0 and hb.settings.start_offset_left.x or (-42+24)/(-258.0) * hb.settings.start_offset_left.x - 24, y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2) } else pos = hb.settings.pos_right offset = { - x = hb.settings.start_offset_right.x, + -- 24*10+30 - 24 = 234. using linear eq, 234 = 16m - 24. + x = direction == 0 and hb.settings.start_offset_right.x or (234+24)/(16) * hb.settings.start_offset_right.x - 24, y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2) } end end + if format_string == nil then format_string = N("@1: @2/@3") end @@ -181,6 +184,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta local state = {} local name = player:get_player_name() local bgscale, iconscale, text, barnumber, bgiconnumber + if start_max == 0 or start_hidden then bgscale = { x=0, y=0 } else @@ -197,6 +201,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta bgiconnumber = hb.settings.statbar_length text = make_label(format_string, format_string_config, label, start_value, start_max) end + if hb.settings.bar_type == "progress_bar" then ids.bg = player:hud_add({ hud_elem_type = "image", @@ -219,6 +224,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta }) end end + local bar_image, bgicon, bar_size if hb.settings.bar_type == "progress_bar" then bar_image = textures.bar @@ -234,10 +240,12 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta bgicon = textures.bgicon bar_size = {x=24, y=24} end + local text2 if hb.settings.bar_type == "statbar_modern" then text2 = bgicon end + ids.bar = player:hud_add({ hud_elem_type = "statbar", position = pos, @@ -247,7 +255,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta item = bgiconnumber, alignment = {x=-1,y=-1}, offset = offset, - direction = 0, + direction = direction, size = bar_size, z_index = 1, }) @@ -258,7 +266,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta text = text, alignment = {x=1,y=1}, number = text_color, - direction = 0, + direction = direction, offset = { x = offset.x + 2, y = offset.y - 1}, z_index = 2, }) @@ -298,7 +306,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta hudtable.default_start_max = default_start_max hb.hudbars_count= hb.hudbars_count + 1 - + hb.hudtables[identifier] = hudtable end @@ -359,6 +367,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon if new_text_color ~= nil then player:hud_change(hudtable.hudids[name].text, "number", new_text_color) end + else if new_icon ~= nil and hudtable.hudids[name].bar ~= nil then player:hud_change(hudtable.hudids[name].bar, "text", new_icon) @@ -474,8 +483,8 @@ end --register built-in HUD bars if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then - hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, 20, 20, false) - hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" }, 10, 10, true) + hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, 0, 20, 20, false) + hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" }, 1, 10, 10, true) end local function hide_builtin(player) @@ -520,7 +529,7 @@ local function update_hud(player, has_damage) --air local breath_max = player:get_properties().breath_max local breath = player:get_breath() - + if breath >= breath_max and hb.settings.autohide_breath == true then hb.hide_hudbar(player, "breath") else diff --git a/mods/HUD/mcl_hbarmor/init.lua b/mods/HUD/mcl_hbarmor/init.lua index 4434f5dad..89b2db7a8 100644 --- a/mods/HUD/mcl_hbarmor/init.lua +++ b/mods/HUD/mcl_hbarmor/init.lua @@ -57,7 +57,7 @@ local function custom_hud(player) end --register and define armor HUD bar -hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 20, mcl_hbarmor.autohide) +hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 0, 20, mcl_hbarmor.autohide) function mcl_hbarmor.get_armor(player) if not player or not armor.def then diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index 5ae45591c..b640dfdc9 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -89,10 +89,10 @@ function mcl_hunger.update_exhaustion_hud(player, exhaustion) end -- register saturation hudbar -hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) +hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 1, 20, 20, false) if mcl_hunger.debug then - hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d")) - hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d")) + hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, 1, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d")) + hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 1, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d")) end minetest.register_on_joinplayer(function(player)