starlit  ui.lua at [467a75e0dc]

File mods/vtlib/ui.lua artifact 765cced2b5 part of check-in 467a75e0dc


local l = ...

return {
	form = l.class {
		name = 'form';
		__index = {
			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;
			attach = 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(self)
				return string.format("size[%f,%f]%s", self.width, self.curs.y + self.pad + self.curs.maxh, self.src)
			end;
		};
		__tostring = function(self) return self:render() end;
		construct = function()
			return {
				src = "";
				width = 8;
				pad = 0;
				curs = {x = 0, y = 0, maxh = 0};
			}
		end;
	};

	tooltipper = function(dui)
		-- takes a configuration table mapping affinities to colors.
		-- 'neutral' is the only required affinity
		return function(a)
			local color = a.color and a.color:readable(0.65, 1.0)
			if color == nil then color = l.color(.5,.5,.5) 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(0.6, 1.0)
					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.2):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;
	end;
}