forked from mtcontrib/minetest_hudbars
		
	Compare commits
	
		
			1 Commits
		
	
	
		
			28dcf8af65
			...
			hud_replac
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | b60e24e11a | 
							
								
								
									
										138
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										138
									
								
								init.lua
									
									
									
									
									
								
							| @@ -100,6 +100,38 @@ local function player_exists(player) | |||||||
| 	return player ~= nil and player:is_player() | 	return player ~= nil and player:is_player() | ||||||
| end | end | ||||||
|  |  | ||||||
|  | local function barindex_to_pos_and_offset(index) | ||||||
|  | 	local pos, offset | ||||||
|  | 	if hb.settings.alignment_pattern == "stack_up" then | ||||||
|  | 		pos = hb.settings.pos_left | ||||||
|  | 		offset = { | ||||||
|  | 			x = hb.settings.start_offset_left.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, | ||||||
|  | 			y = hb.settings.start_offset_left.y + hb.settings.vmargin * index | ||||||
|  | 		} | ||||||
|  | 	else | ||||||
|  | 		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 * (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 * ((index-1)/2) | ||||||
|  | 			} | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  | 	return pos, offset | ||||||
|  | end | ||||||
|  |  | ||||||
| -- Table which contains all players with active default HUD bars (only for internal use) | -- Table which contains all players with active default HUD bars (only for internal use) | ||||||
| hb.players = {} | hb.players = {} | ||||||
|  |  | ||||||
| @@ -145,33 +177,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta | |||||||
| 	local pos, offset | 	local pos, offset | ||||||
| 	local index = math.floor(hb.get_hudbar_position_index(identifier)) | 	local index = math.floor(hb.get_hudbar_position_index(identifier)) | ||||||
| 	hb.registered_slots[index] = true | 	hb.registered_slots[index] = true | ||||||
| 	if hb.settings.alignment_pattern == "stack_up" then |  | ||||||
| 		pos = hb.settings.pos_left | 	pos, offset = barindex_to_pos_and_offset(index) | ||||||
| 		offset = { |  | ||||||
| 			x = hb.settings.start_offset_left.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, |  | ||||||
| 			y = hb.settings.start_offset_left.y + hb.settings.vmargin * index |  | ||||||
| 		} |  | ||||||
| 	else |  | ||||||
| 		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 * (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 * ((index-1)/2) |  | ||||||
| 			} |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| 	if format_string == nil then | 	if format_string == nil then | ||||||
| 		format_string = S("%s: %d/%d") | 		format_string = S("%s: %d/%d") | ||||||
| 	end | 	end | ||||||
| @@ -241,7 +249,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta | |||||||
| 			bar_image = textures.icon | 			bar_image = textures.icon | ||||||
| 			bar_size = {x=24, y=24} | 			bar_size = {x=24, y=24} | ||||||
| 		end | 		end | ||||||
| 		ids.bar = player:hud_add({ | 		local bardef = { | ||||||
| 			hud_elem_type = "statbar", | 			hud_elem_type = "statbar", | ||||||
| 			position = pos, | 			position = pos, | ||||||
| 			text = bar_image, | 			text = bar_image, | ||||||
| @@ -250,7 +258,10 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta | |||||||
| 			offset = offset, | 			offset = offset, | ||||||
| 			direction = 0, | 			direction = 0, | ||||||
| 			size = bar_size, | 			size = bar_size, | ||||||
| 		}) | 		} | ||||||
|  | 		if hudtable.identifier ~= "health" and hudtable.identifier ~= "breath" then | ||||||
|  | 			ids.bar = player:hud_add(bardef) | ||||||
|  | 		end | ||||||
| 		if hb.settings.bar_type == "progress_bar" then | 		if hb.settings.bar_type == "progress_bar" then | ||||||
| 			ids.text = player:hud_add({ | 			ids.text = player:hud_add({ | ||||||
| 				hud_elem_type = "text", | 				hud_elem_type = "text", | ||||||
| @@ -343,7 +354,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon | |||||||
| 		if new_bgicon ~= nil and hudtable.hudids[name].bgicon ~= nil then | 		if new_bgicon ~= nil and hudtable.hudids[name].bgicon ~= nil then | ||||||
| 			player:hud_change(hudtable.hudids[name].bgicon, "text", new_bgicon) | 			player:hud_change(hudtable.hudids[name].bgicon, "text", new_bgicon) | ||||||
| 		end | 		end | ||||||
| 		if new_bar ~= nil then | 		if new_bar ~= nil and hudtable.hudids[name].bar ~= nil then | ||||||
| 			player:hud_change(hudtable.hudids[name].bar , "text", new_bar) | 			player:hud_change(hudtable.hudids[name].bar , "text", new_bar) | ||||||
| 		end | 		end | ||||||
| 		if new_label ~= nil then | 		if new_label ~= nil then | ||||||
| @@ -386,7 +397,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon | |||||||
|  |  | ||||||
| 		if value_changed or max_changed then | 		if value_changed or max_changed then | ||||||
| 			local new_barlength = hb.value_to_barlength(new_value, new_max_value) | 			local new_barlength = hb.value_to_barlength(new_value, new_max_value) | ||||||
| 			if new_barlength ~= hudtable.hudstate[name].barlength then | 			if new_barlength ~= hudtable.hudstate[name].barlength and hudtable.hudids[name].bar ~= nil then | ||||||
| 				player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value)) | 				player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value)) | ||||||
| 				hudtable.hudstate[name].barlength = new_barlength | 				hudtable.hudstate[name].barlength = new_barlength | ||||||
| 			end | 			end | ||||||
| @@ -418,7 +429,9 @@ function hb.hide_hudbar(player, identifier) | |||||||
| 		elseif hb.settings.bar_type == "statbar_modern" then | 		elseif hb.settings.bar_type == "statbar_modern" then | ||||||
| 			player:hud_change(hudtable.hudids[name].bg, "number", 0) | 			player:hud_change(hudtable.hudids[name].bg, "number", 0) | ||||||
| 		end | 		end | ||||||
|  | 		if hudtable.hudids[name].bar ~= nil then | ||||||
| 			player:hud_change(hudtable.hudids[name].bar, "number", 0) | 			player:hud_change(hudtable.hudids[name].bar, "number", 0) | ||||||
|  | 		end | ||||||
| 		hudtable.hudstate[name].hidden = true | 		hudtable.hudstate[name].hidden = true | ||||||
| 	end | 	end | ||||||
| 	return true | 	return true | ||||||
| @@ -443,7 +456,9 @@ function hb.unhide_hudbar(player, identifier) | |||||||
| 		elseif hb.settings.bar_type == "statbar_modern" then | 		elseif hb.settings.bar_type == "statbar_modern" then | ||||||
| 			player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length) | 			player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length) | ||||||
| 		end | 		end | ||||||
|  | 		if hudtable.hudids[name].bar ~= nil then | ||||||
| 			player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max)) | 			player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max)) | ||||||
|  | 		end | ||||||
| 		hudtable.hudstate[name].hidden = false | 		hudtable.hudstate[name].hidden = false | ||||||
| 	end | 	end | ||||||
| 	return true | 	return true | ||||||
| @@ -465,17 +480,59 @@ end | |||||||
|  |  | ||||||
| --register built-in HUD bars | --register built-in HUD bars | ||||||
| if minetest.setting_getbool("enable_damage") or hb.settings.forceload_default_hudbars then | if minetest.setting_getbool("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) | 	local healthtextures = { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" } | ||||||
| 	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) | 	local breathtextures = { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" } | ||||||
| end |  | ||||||
|  |  | ||||||
| local function hide_builtin(player) | 	hb.register_hudbar("health", 0xFFFFFF, S("Health"), healthtextures, 20, 20, false) | ||||||
| 	local flags = player:hud_get_flags() | 	hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), breathtextures, 10, 10, true) | ||||||
| 	flags.healthbar = false |  | ||||||
| 	flags.breathbar = false |  | ||||||
| 	player:hud_set_flags(flags) |  | ||||||
| end |  | ||||||
|  |  | ||||||
|  | 	-- Overwrite the builtin statbars for the best client prediction | ||||||
|  |  | ||||||
|  | 	-- FIXME: Health icons are drawn behind the modern statbar background | ||||||
|  | 	-- FIXME: Breath bar is drawn above text | ||||||
|  |  | ||||||
|  | 	local bar_health, bar_breath, size_health, size_breath | ||||||
|  | 	if hb.settings.bar_type == "progress_bar" then | ||||||
|  | 		bar_health = healthtextures.bar | ||||||
|  | 		bar_breath = breathtextures.bar | ||||||
|  | 		size_health  = {x=hb.value_to_barlength(1,20)*2, y=16} | ||||||
|  | 		size_breath = {x=hb.value_to_barlength(1,20)*2, y=16} | ||||||
|  | 	elseif hb.settings.bar_type == "statbar_classic" or hb.settings.bar_type == "statbar_modern" then | ||||||
|  | 		bar_health = healthtextures.icon | ||||||
|  | 		bar_breath = breathtextures.icon | ||||||
|  | 		size_health = {x=24, y=24} | ||||||
|  | 		size_breath = {x=24, y=24} | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	local hudtable = hb.get_hudtable("health") | ||||||
|  | 	local pos, offset = barindex_to_pos_and_offset(hb.get_hudbar_position_index("health")) | ||||||
|  | 	local bardef = { | ||||||
|  | 		hud_elem_type = "statbar", | ||||||
|  | 		position = pos, | ||||||
|  | 		text = bar_health, | ||||||
|  | 		number = 20, | ||||||
|  | 		alignment = {x=-1,y=-1}, | ||||||
|  | 		offset = offset, | ||||||
|  | 		direction = 0, | ||||||
|  | 		size = size_health, | ||||||
|  | 	} | ||||||
|  | 	minetest.hud_replace_builtin("health", bardef) | ||||||
|  |  | ||||||
|  | 	pos, offset = barindex_to_pos_and_offset(hb.get_hudbar_position_index("breath")) | ||||||
|  | 	hudtable = hb.get_hudtable("breath") | ||||||
|  | 	bardef = { | ||||||
|  | 		hud_elem_type = "statbar", | ||||||
|  | 		position = pos, | ||||||
|  | 		text = bar_breath, | ||||||
|  | 		number = 20, | ||||||
|  | 		alignment = {x=-1,y=-1}, | ||||||
|  | 		offset = offset, | ||||||
|  | 		direction = 0, | ||||||
|  | 		size = size_breath, | ||||||
|  | 	} | ||||||
|  | 	minetest.hud_replace_builtin("breath", bardef) | ||||||
|  |  | ||||||
|  | end | ||||||
|  |  | ||||||
| local function custom_hud(player) | local function custom_hud(player) | ||||||
| 	if minetest.setting_getbool("enable_damage") or hb.settings.forceload_default_hudbars then | 	if minetest.setting_getbool("enable_damage") or hb.settings.forceload_default_hudbars then | ||||||
| @@ -529,7 +586,6 @@ minetest.register_on_respawnplayer(function(player) | |||||||
| end) | end) | ||||||
|  |  | ||||||
| minetest.register_on_joinplayer(function(player) | minetest.register_on_joinplayer(function(player) | ||||||
| 	hide_builtin(player) |  | ||||||
| 	custom_hud(player) | 	custom_hud(player) | ||||||
| 	hb.players[player:get_player_name()] = player | 	hb.players[player:get_player_name()] = player | ||||||
| end) | end) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user