diff --git a/README.txt b/README.txt index 0673436..5e06c43 100644 --- a/README.txt +++ b/README.txt @@ -35,6 +35,28 @@ 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 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. API: ---- diff --git a/init.lua b/init.lua index 49e2443..5373db1 100644 --- a/init.lua +++ b/init.lua @@ -5,6 +5,12 @@ hb.hudtables = {} -- number of registered HUD bars hb.hudbars_count = 0 +-- index of the first HUD bar slot, used for automatic positioning +hb.first_free_hudbar_slot = 0 + +-- table which recoreds which HUD bar slots have been “registered” so far; used for automatic positioning +hb.registered_slots = {} + hb.settings = {} -- default settings @@ -25,6 +31,31 @@ if autohide_breath ~= nil then hb.settings.autohide_breath = autohide_breath end +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 + +do + local max + for k,v in pairs(hb.settings.sorting_reverse) do + if max == nil then + max = k + elseif k > max then + max = k + end + end + hb.first_free_hudbar_slot = max + 1 +end + -- Table which contains all players with active default HUD bars (only for internal use) hb.players = {} @@ -40,20 +71,39 @@ function hb.get_hudtable(identifier) return hb.hudtables[identifier] 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) local hudtable = {} 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.sorting_reverse[index] == nil then + hb.first_free_hudbar_slot = hb.first_free_hudbar_slot + 1 + end + 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 * math.floor(hb.hudbars_count/2) + 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, - 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 if format_string == nil then