This repository has been archived on 2020-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
minetest-mod-sparktech/sparkapi/lua/formspec.lua

61 lines
2.4 KiB
Lua

local DEFAULT_BG_COLOR = "fc05e344"
local DEFAULT_FG_COLOR = "fc059dd0"
local DEFAULT_HL_COLOR = "fc059db0"
if not sparktech then sparktech = {} end
function sparktech.makebar(texture, posx, posy, sizex, sizey, value, maxvalue, direction)
-- direction = where to cut the texture either from thr right(0), below(1) left(2) or above (3)
-- default is right(0)
--texture texture name , no folder required
--sizex the size to modify
if direction == nil then direction = 0 end --direction is optional this way
-- format : image[X,Y;W,H;texture_name]
local ratio = value / maxvalue
if direction == 0 then
sizex = ratio * sizex
elseif direction == 1 then
sizey = ratio * sizey
elseif direction == 2 then
posx = posx + (sizex - (ratio * sizex))
sizex = ratio * sizex
elseif direction == 3 then
posy = posy + (sizey - (ratio * sizey))
sizey = ratio * sizey
end
return "image[" .. posx .. "," .. posy .. ";" .. sizex .. "," .. sizey .. ";" .. texture .. "]"
end
function verify_hexcolor_or_nil(s)
if not s or #s ~= 8 or s:find("[^abcdefABCDEF0123456789]") then
return nil
end
return s
end
function sparktech.add_inventory(size_x, size_y, formspec) -- pass an unfinished formspec with minimum size, this function adds an inventory
local bg_color = verify_hexcolor_or_nil(minetest.settings:get("spark_gui_background")) or DEFAULT_BG_COLOR
local fg_color = verify_hexcolor_or_nil(minetest.settings:get("spark_gui_foreground")) or DEFAULT_FG_COLOR
local fg_highlight = verify_hexcolor_or_nil(minetest.settings:get("spark_gui_highlight")) or DEFAULT_HL_COLOR
local gui_immersive
if minetest.settings:get("spark_gui_full") then
gui_immersive = "true"
else
gui_immersive = "false"
end
if not size_x or size_x < 13.9 then size_x = 13.9 end
if not size_y or size_y < 0 then size_y = 0 end
local l_formspec = "size[" .. tostring(size_x) .. "," .. (size_y + 4.8) .. "]" ..
"real_coordinates[true]" ..
"list[current_player;main;0," .. size_y .. ";8,3;8]" ..
"list[current_player;main;0," .. (size_y + 3.8 ) .. ";8,1;]"..
"list[current_player;craft;10.4," .. size_y .. ";3,3]" ..
"list[current_player;craftpreview;10.4," .. (size_y + 3.8) .. ";1,1]" ..
"listcolors[#" .. fg_color .. ";#" .. fg_highlight .. "]" ..
"bgcolor[#" .. bg_color .. ";" .. gui_immersive .. "]"
if not formspec then formspec = "" end
l_formspec = l_formspec .. formspec
return l_formspec
end