Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
[hudbars] Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymkwi committed Aug 13, 2016
1 parent 71b4cac commit 1b99087
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 147 deletions.
40 changes: 29 additions & 11 deletions mods/hudbars/API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
API documentation for the HUD bars mod 1.2.1
API documentation for the HUD bars mod 1.4.1
============================================

## Introduction
Expand Down Expand Up @@ -50,8 +50,9 @@ This function registers a new custom HUD bar definition to the HUD bars mod, so
and unhidden on a per-player basis.
Note this does not yet display the HUD bar.

The HUD bars will be displayed in a “first come, first serve” order. This mod does not allow fow a custom order or a way to set it
manually in a reliable way.
The HUD bars will be displayed in a “first come, first serve” order. This API does not allow fow a custom order or a way to set it
manually in a reliable way. However, you can use the setting `hudbars_sorting` for this. See the advanced setting menu in Minetest
for more information.


#### Parameters
Expand Down Expand Up @@ -99,22 +100,39 @@ Always `nil`.


## Modifying a HUD bar
After a HUD bar has been added, you can change the current and maximum value on a per-player basis.
After a HUD bar has been added, you can change the current and maximum value and other attributes on a per-player basis.
You use the function `hb.change_hudbar` for this.

### `hb.change_hudbar(player, identifier, new_value, new_max_value)`
Changes the values of an initialized HUD bar for a certain player. `new_value` and `new_max_value`
can be `nil`; if one of them is `nil`, that means the value is unchanged. If both values
are `nil`, this function is a no-op.
This function also tries minimize the amount of calls to `hud_change` of the Minetest Lua API, and
therefore, network traffic. `hud_change` is only called if it is actually needed, i.e. when the
actual length of the bar or the displayed string changed, so you do not have to worry about it.
### `hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon, new_bgicon, new_bar, new_label, new_text_color)`
Changes the values and the appearance of an initialized HUD bar for a certain player. `new_value`
and `new_max_value` are the most important parameters as they specify the new current and maximum new values, you do not need
to worry too much about the other parameters.

The following parameters are less important and provided for styling the HUD bar after registration (if
this is desired). The “styling” parameters parallel the parameters of `hb.register_hudbar`. It is
recommended to not change the style of a HUD bar too often as this can be distracting or confusing
for players.

`new_value`, `new_max_value` `new_icon`, `new_bgicon`, `new_bar`, `new_label` and `new_text_color` can be
`nil`; if one of them is `nil`, that means the value is unchanged. If all those values are `nil`, this
function is a no-op.

This function tries to minimize the amount of calls to `hud_change` of the Minetest Lua API
(and thus, network traffic), when you only change the value and/or maximum value. In this case,
`hud_change` is only called if it is actually needed, e.g. when the actual length of the bar
or the displayed string changed, so you do not have to worry about it. There is, however, no
such network optimization for the “styling” parameters, so keep this in mind.

#### Parameters
* `player`: `ObjectRef` of the player to which the HUD bar belongs to
* `identifier`: The identifier of the HUD bar type to change, as specified in `hb.register_hudbar`.
* `new_value`: The new current value of the HUD bar
* `new_max_value`: The new maximum value of the HUD bar
* `new_icon`: File name of the new icon
* `new_bgicon`: File name of the new background icon for the modern-style statbar
* `new_bar`: File name of the new bar segment image
* `new_label`: A new text label of the HUD bar. Note the format string still applies
* `new_text_color`: A 3-octet number defining the new color of the text.

#### Return value
Always `nil`.
Expand Down
51 changes: 51 additions & 0 deletions mods/hudbars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# HUD bars

## Description
This mod changes the HUD of Minetest. It replaces the default health and breath
symbols by horizontal colored bars with text showing the number.

Furthermore, it enables other mods to add their own custom bars to the HUD,
this mod will place them accordingly.

**Important**: Keep in mind if running a server with this mod, that the custom
position should be displayed correctly on every screen size.

## Current version
The current version is 1.4.1.

This software uses [semantic versioning](http://semver.org), as defined by version 2.0.0 of the SemVer
standard.

## Settings
This mod can be configured quite a bit. You can change HUD bar appearance, offsets, ordering, and more.
Use the advanced settings menu in Minetest for detailed configuration.

## API
The API is used to add your own custom HUD bars.
Documentation for the API of this mod can be found in `API.md`.

## Legal
### License of source code
Author: Wuzzy (2015)

This mod was forked from the “Better HUD” [hud] mod by BlockMen.

This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License (WTFPL), version 2, as published by Sam Hocevar.

### Licenses 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, modified again by Wuzzy
* `hudbars_bgicon_breath.png`—based on previous image, edited by Wuzzy (WTFPL)
* `hudbars_bar_health.png`—Wuzzy (WTFPL)
* `hudbars_bar_breath.png`—Wuzzy (WTFPL)
* `hudbars_bar_background.png`—Wuzzy (WTFPL)

### License references

* [CC-BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/)
* [WTFPL](http://sam.zoy.org/wtfpl/COPYING)
109 changes: 0 additions & 109 deletions mods/hudbars/README.txt

This file was deleted.

17 changes: 17 additions & 0 deletions mods/hudbars/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ as of version 2.0.0 of the standard <http://semver.org/>.
1.2.1
-----
- Fix crash when enable_damage is changed in mid-game

1.3.0
-----
- Make all settings avaialbe in Minetest's advanced settings menu
- Fix HUD bars overlap when both hudbars_tick and hudbars_vmargin were set
- Use Markdown syntax in readme file
- Fix some factual mistakes in readme file
- Add metadata: mod.conf, description.txt, screenshot.png

1.4.0
-----
- Allow to change HUD bar images and label after it has been registered
- Minor API.md correction

1.4.1
-----
- Fix bug in hb.change_hudbar being a no-op if new_value and new_max value are nil
1 change: 1 addition & 0 deletions mods/hudbars/depends.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
intllib?
1 change: 1 addition & 0 deletions mods/hudbars/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replaces the health and breath symbols in the HUD by “progress bars” and shows exact values. Other mods can add more progress bars for custom player stats.
9 changes: 0 additions & 9 deletions mods/hudbars/hud_legacy.lua

This file was deleted.

58 changes: 40 additions & 18 deletions mods/hudbars/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,12 @@ hb.settings.start_offset_left.y = hb.load_setting("hudbars_start_offset_left_y",
hb.settings.start_offset_right.x = hb.load_setting("hudbars_start_offset_right_x", "number", 15)
hb.settings.start_offset_right.y = hb.load_setting("hudbars_start_offset_right_y", "number", -86)

hb.settings.vmargin = hb.load_setting("hudbars_tick", "number", 24)
hb.settings.vmargin = hb.load_setting("hudbars_vmargin", "number", 24)
hb.settings.tick = hb.load_setting("hudbars_tick", "number", 0.1)

-- experimental setting: Changing this setting is not officially supported, do NOT rely on it!
hb.settings.forceload_default_hudbars = hb.load_setting("hudbars_forceload_default_hudbars", "bool", true)

--[[
- hudbars_alignment_pattern: This setting changes the way the HUD bars are ordered on the display. You can choose
between a zig-zag pattern or a vertically stacked pattern.
The following values are allowed:
zigzag: Starting from the left bottom, the next is right from the first,
the next is above the first, the next is right of the third, etc.
This is the default.
stack_up: The HUD bars are stacked vertically, going upwards.
stack_down: The HUD bars are stacked vertically, going downwards.
]]

-- Misc. settings
hb.settings.alignment_pattern = hb.load_setting("hudbars_alignment_pattern", "string", "zigzag", {"zigzag", "stack_up", "stack_down"})
hb.settings.bar_type = hb.load_setting("hudbars_bar_type", "string", "progress_bar", {"progress_bar", "statbar_classic", "statbar_modern"})
Expand Down Expand Up @@ -177,7 +166,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
local ids = {}
local state = {}
local name = player:get_player_name()
local bgscale, iconscale, text, barnumber
local bgscale, iconscale, text, barnumber, bgiconnumber
if start_max == 0 or start_hidden then
bgscale = { x=0, y=0 }
else
Expand All @@ -186,10 +175,12 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
if start_hidden then
iconscale = { x=0, y=0 }
barnumber = 0
bgiconnumber = 0
text = ""
else
iconscale = { x=1, y=1 }
barnumber = hb.value_to_barlength(start_value, start_max)
bgiconnumber = hb.settings.statbar_length
text = string.format(format_string, label, start_value, start_max)
end
if hb.settings.bar_type == "progress_bar" then
Expand Down Expand Up @@ -218,7 +209,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
position = pos,
scale = bgscale,
text = textures.bgicon,
number = hb.settings.statbar_length,
number = bgiconnumber,
alignment = {x=-1,y=-1},
offset = { x = offset.x, y = offset.y },
size = ""
Expand Down Expand Up @@ -248,7 +239,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
alignment = {x=1,y=1},
number = text_color,
direction = 0,
offset = { x = offset.x + 2, y = offset.y },
offset = { x = offset.x + 2, y = offset.y - 1},
})
end
-- Do not forget to update hb.get_hudbar_state if you add new fields to the state table
Expand Down Expand Up @@ -294,8 +285,8 @@ function hb.init_hudbar(player, identifier, start_value, start_max, start_hidden
hb.hudtables[identifier].add_all(player, hudtable, start_value, start_max, start_hidden)
end

function hb.change_hudbar(player, identifier, new_value, new_max_value)
if new_value == nil and new_max_value == nil then
function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon, new_bgicon, new_bar, new_label, new_text_color)
if new_value == nil and new_max_value == nil and new_icon == nil and new_bgicon == nil and new_bar == nil and new_label == nil and new_text_color == nil then
return
end

Expand All @@ -320,6 +311,33 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
new_max_value = hudtable.hudstate[name].max
end

if hb.settings.bar_type == "progress_bar" then
if new_icon ~= nil and hudtable.hudids[name].icon ~= nil then
player:hud_change(hudtable.hudids[name].icon, "text", new_icon)
end
if new_bgicon ~= nil and hudtable.hudids[name].bgicon ~= nil then
player:hud_change(hudtable.hudids[name].bgicon, "text", new_bgicon)
end
if new_bar ~= nil then
player:hud_change(hudtable.hudids[name].bar , "text", new_bar)
end
if new_label ~= nil then
hudtable.label = new_label
local new_text = string.format(hudtable.format_string, new_label, hudtable.hudstate[name].value, hudtable.hudstate[name].max)
player:hud_change(hudtable.hudids[name].text, "text", new_text)
end
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)
end
if new_bgicon ~= nil and hudtable.hudids[name].bg ~= nil then
player:hud_change(hudtable.hudids[name].bg, "text", new_bgicon)
end
end

local main_error_text =
"[hudbars] Bad call to hb.change_hudbar, identifier: “"..tostring(identifier).."”, player name: “"..name.."”. "
if new_max_value < new_value then
Expand Down Expand Up @@ -369,6 +387,8 @@ function hb.hide_hudbar(player, identifier)
end
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
player:hud_change(hudtable.hudids[name].text, "text", "")
elseif hb.settings.bar_type == "statbar_modern" then
player:hud_change(hudtable.hudids[name].bg, "number", 0)
end
player:hud_change(hudtable.hudids[name].bar, "number", 0)
hudtable.hudstate[name].hidden = true
Expand All @@ -390,6 +410,8 @@ function hb.unhide_hudbar(player, identifier)
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)))
elseif hb.settings.bar_type == "statbar_modern" then
player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length)
end
player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max))
hudtable.hudstate[name].hidden = false
Expand All @@ -412,7 +434,7 @@ end
--register built-in HUD bars
if minetest.setting_getbool("enable_damage") or hb.settings.forceload_default_hudbars then
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", bgicon = "hudbars_bgicon_breath.png" }, 10, 10, true)
end

local function hide_builtin(player)
Expand Down
Loading

0 comments on commit 1b99087

Please sign in to comment.