aboutsummaryrefslogtreecommitdiff
path: root/src/script/common
ModeNameSize
-rw-r--r--CMakeLists.txt265logplain
-rw-r--r--c_content.cpp40110logplain
-rw-r--r--c_content.h7042logplain
-rw-r--r--c_converter.cpp14117logplain
-rw-r--r--c_converter.h5983logplain
-rw-r--r--c_internal.cpp5068logplain
-rw-r--r--c_internal.h4396logplain
-rw-r--r--c_types.cpp1036logplain
-rw-r--r--c_types.h1407logplain
'#n150'>150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
local tx = {}
setmetatable(tx, {__call = function(_, ...) return tx.base(...) end})

function tx.escape(str)
	return (string.gsub(tostring(str), [[([%^:\])]], [[\%1]]))
end

local function getargs(...)
	return select("#", ...), {...}
end

local function curry(f, x)
	return function(...)
		return f(x, ...)
	end
end

local function xmkmodifier(func)
	return function(self, ...)
		table.insert(self, (func(...)))
		return self
	end
end

local function mkmodifier(fmt, spec)
	return xmkmodifier(function(...)
		local count = select("#", ...)
		local args = {...}
		for k, f in pairs(spec) do
			args[k] = f(args[k])
		end
		return string.format(fmt, unpack(args, 1, count))
	end)
end

-- Texture object
local tx_lib = {}
local tx_mt = {
	__index = tx_lib,
	__tostring = function(self)
		return table.concat(self, "^")
	end,
	__concat = function(a, b)
		return tx.raw(("%s^%s"):format(tostring(a), tostring(b)))
	end,
}

function tx.raw(str)
	return setmetatable({str}, tx_mt)
end
function tx.base(str)
	return tx.raw(tx.escape(str))
end
-- TODO: use [fill when 5.8.0 becomes widely used client-side
function tx.fill(w, h, color)
	return tx"advtrains_hud_bg.png":resize(w, h):colorize(color)
end

-- Most texture modifiers
tx_lib.colorize = xmkmodifier(function(c, a)
	local str = ("[colorize:%s"):format(tx.escape(c))
	if a then
		str = str .. ":" .. a
	end
	return str
end)
tx_lib.multiply = mkmodifier("[multiply:%s", {tx.escape})
tx_lib.resize = mkmodifier("[resize:%dx%d", {})
tx_lib.transform = mkmodifier("[transform%s", {tx.escape})
tx_lib.makealpha = mkmodifier("[makealpha:%s", {tx.escape})
tx_lib.verticalframe = mkmodifier("[verticalframe:%d:%d]", {})

-- [combine

local combine = {}

function combine:add(x, y, ent)
	table.insert(self.st, ([[%d,%d=%s]]):format(x, y, tx.escape(tostring(ent))))
	return self
end

local combine_mt = {
	__index = combine,
	__tostring = function(self)
		return table.concat(self.st, ":")
	end,
}

function tx.combine(w, h, bg)
	local base = ("[combine:%dx%d"):format(w, h)
	local obj = setmetatable({width = w, height = h, st = {base}}, combine_mt)
	if bg then
		obj:add_fill(0, 0, w, h, bg)
	end
	return obj
end

function combine:add_fill(x, y, w, h, ...)
	if w < 1 or h < 1 then
		return obj
	end
	return self:add(x, y, tx.fill(w, h, ...))
end

local function add_multicolor_fill(n, self, x, y, w, h, ...)
	local argc, argv = getargs(...)
	local t = 0
	for k = 1, argc, 2 do
		t = t + argv[k]
	end
	local newargs = {x, y, w, h}
	local sk, wk = n, n+2
	local s = newargs[wk]/t
	for k = 1, argc, 2 do
		local v = argv[k] * s
		newargs[wk] = v
		newargs[5] = argv[k+1]
		self:add_fill(unpack(newargs))
		newargs[sk] = newargs[sk] + v
	end
	return self
end
combine.add_multicolor_fill_topdown = curry(add_multicolor_fill, 2)
combine.add_multicolor_fill_leftright = curry(add_multicolor_fill, 1)

local function add_segmentbar(n, self, x, y, w, h, m, c, ...)
	local argc, argv = getargs(...)
	local baseargs = {x, y, w, h}
	local ss = (baseargs[n+2]+m)/c
	local bs = ss - m
	for k = 1, argc, 3 do
		local lower, upper, fill = argv[k], argv[k+1], argv[k+2]
		lower = math.max(0, math.floor(lower))+1
		upper = math.min(c, math.floor(upper))
		if lower <= upper then
			local args = {x, y, w, h, fill}
			args[n+2] = bs
			args[n] = args[n] + ss*(lower-1)
			for i = lower, upper do
				self:add_fill(unpack(args))
				args[n] = args[n] + ss
			end
		end
	end
	return self
end