sorcery  ui.lua at tip

File lib/ui.lua from the latest check-in


local l = sorcery.lib
local dui = sorcery.data.ui

return {
	form = function()
		return {
			src = "";
			width = 8;
			pad = 0;
			curs = {x = 0, y = 0, maxh = 0};
			nl = function(self, h)
				h = h or 0
				self.curs.x = 0
				self.curs.y = math.max(h, self.curs.y + self.curs.maxh)
				self.curs.maxh = 0
			end;
			atttach = function(self, elt, x, y, w, h, ...)
				local content = ''
				if self.width - self.curs.x < w then self:nl() end
				for _, v in pairs{...} do
					content = content .. ';' .. minetest.formspec_escape(tostring(v))
				end
				self.src = self.src .. string.format('%s[%f,%f;%f,%f%s]', elt, x,y, w,h, content)
				if h > self.curs.maxh then self.curs.maxh = h end
			end;
			add = function(self, elt, w, h, ...)
				local ax, ay = self.curs.x, self.curs.y
				self:attach(elt, ax,ay, w,h, ...)
				self.curs.x = self.curs.x + w + self.pad
				if self.curs.x > self.width then self:nl() end
				return ax, ay
			end;
			render = function()
				return string.format("size[%f,%f]%s", self.width, self.curs.y + self.pad + self.curs.maxh, self.src)
			end;
		}
	end;

	tooltip = function(a)
		local color = a.color and a.color:readable()
		if color == nil then color = l.color(136,158,177) end
		local str = a.title
		if a.desc then
			str = str .. '\n' .. color:fmt(minetest.wrap_text(a.desc,60))
		end
		if a.props then
			-- str = str .. '\n'
			for _,prop in pairs(a.props) do
				local c
				if prop.color and l.color.id(prop.color) then
					c = prop.color:readable()
				elseif dui.colors[prop.affinity] then
					c = l.color(dui.colors[prop.affinity])
				else
					c = l.color(dui.colors.neutral)
				end

				str = str .. '\n ' .. c:fmt('* ')

				if prop.title then
					str = str .. c:brighten(1.3):fmt(prop.title) .. ': '
				end

				local lines = minetest.wrap_text(prop.desc, 55, true)
				str = str .. c:fmt(lines[1])
				for i=2,#lines do
					str = str .. '\n' .. string.rep(' ',5) .. c:fmt(lines[i])
				end
			end
		end
		return color:darken(0.8):bg(str)
	end;
}