techage/power/formspecs.lua

78 lines
2.1 KiB
Lua
Raw Normal View History

2020-01-26 01:22:06 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Power Formspec Functions
]]--
--local P2S = minetest.pos_to_string
--local M = minetest.get_meta
--local N = function(pos) return minetest.get_node(pos).name end
--local S = techage.S
local in_range = techage.in_range
function techage.power.percent(max_val, curr_val)
return math.min(math.ceil(((curr_val or 0) * 100.0) / (max_val or 1.0)), 100)
end
function techage.power.formspec_load_bar(charging, max_val)
local percent
charging = charging or 0
max_val = max_val or 1
if charging ~= 0 then
percent = 50 + math.ceil((charging * 50.0) / max_val)
end
if charging > 0 then
return "techage_form_level_off.png^[lowpart:"..percent..":techage_form_level_charge.png"
elseif charging < 0 then
return "techage_form_level_unload.png^[lowpart:"..percent..":techage_form_level_off.png"
else
return "techage_form_level_off.png"
end
end
function techage.power.formspec_power_bar(max_power, current_power)
if (current_power or 0) == 0 then
return "techage_form_level_bg.png"
end
local percent = techage.power.percent(max_power, current_power)
percent = (percent + 5) / 1.22 -- texture correction
return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png"
end
2020-02-06 22:51:30 +03:00
function techage.power.formspec_label_bar(x, y, label, max_power, current_power, unit)
2020-01-26 01:22:06 +03:00
local percent, ypos
current_power = current_power or 0
2020-02-06 22:51:30 +03:00
max_power = max_power or 1
unit = unit or "ku"
2020-01-26 01:22:06 +03:00
if current_power == 0 then
percent = 0
ypos = 2.8
else
percent = techage.power.percent(max_power, current_power)
-- 0.4 to 2.8 = 2.4
local offs = 2.4 - (current_power / max_power) * 2.4
ypos = 0.4 + in_range(offs, 0.4, 2.4)
end
percent = (percent + 5) / 1.1 -- texture correction
return "container["..x..","..y.."]"..
"box[0,0;2.3,3.3;#395c74]"..
"label[0.2,0;"..label.."]"..
2020-02-06 22:51:30 +03:00
"label[0.7,0.4;"..max_power.." "..unit.."]"..
2020-01-26 01:22:06 +03:00
"image[0,0.5;1,3;"..
"techage_form_level_bg.png^[lowpart:"..percent..
":techage_form_level_fg.png]"..
2020-02-06 22:51:30 +03:00
"label[0.7,"..ypos..";"..current_power.." "..unit.."]"..
2020-01-26 01:22:06 +03:00
"container_end[]"
end