24,540 ARTICLES
ON THIS WIKI

Module:Grid/Layer


Documentation for this module may be created at Module:Grid/Layer/doc

local p = {}
local g = require("Module:Common")
local gi = require("Module:Grid/Item")

local delta_x = 36
local delta_y = 18

local function makeKey(u, v)
	return string.char(65 + u) .. tostring(1 + v) -- indices are zero-based
end

local function calcSize(size_u, size_v)
	local size = size_u + size_v - 2
	return 
		size,
		delta_x * (size_u + size_v - 2) + 42,
		delta_y * (size_u + size_v - 2) + 42
end
			
function p.drawGrid(size_u, size_v)
	local size, width, height = calcSize(size_u, size_v)
	local grid_base_x = 21 + delta_x * (size_v - 1)
	local grid_base_y = 12
	local grid_lines = {}

	for u = 1, size_u - 1 do
		local x1 = grid_base_x + delta_x * u
		local y1 = grid_base_y + delta_y * u
		table.insert(grid_lines, string.format("M%d,%dl-%d,%d", x1, y1, delta_x * size_v, delta_y * size_v))
	end

	for v = 1, size_v - 1 do
		local x1 = grid_base_x - delta_x * v
		local y1 = grid_base_y + delta_y * v
		table.insert(grid_lines, string.format("M%d,%dl%d,%d", x1, y1, delta_x * size_u, delta_y * size_u))
	end

	return table.concat(grid_lines, '')
end

function p.makeGrid(frame, args, size_u, size_v, bg, link, float)
	local size, width, height = calcSize(size_u, size_v)
	local base_x = 5 + delta_x * (size_v - 1)
	local base_y = 5
 
	-- u = (delta_x, delta_y)
	-- v = (-delta_x, delta_y)
 
	local items = {}
	local argsRecipeSubobjects = { link }
	for u = 0, size_u - 1 do
		for v = 0, size_v - 1 do
			local x = base_x + delta_x * u - delta_x * v
			local y = base_y + delta_y * u + delta_y * v
			local key = makeKey(u, v)
			local name = args[key]
			if g.isGiven(name) then
				table.insert(items, gi.makeItem(
					name,
					1,
					args[key .. "-image"] or "Grid " .. name .. ".png",
					args[key .. "-link"] or name,
					name,
					y,
					x,
					nil
				))
				if link then
					table.insert(argsRecipeSubobjects, name)
				end
			end
		end
	end

	local l = ""

	if bg then
		l = l .. string.format("<div class='grid-bg' data-grid='%s'>", p.drawGrid(size_u, size_v))
	end

	l = l .. frame:expandTemplate{title="Grid", args={
		link = link,
		float = float,
		width = width,
		height = height,
		content = table.concat(items, " ")
	}}
 
	if link then
		l = l .. frame:expandTemplate{title="RecipeSubobjects", args=argsRecipeSubobjects}
	end

	if bg then
		l = l .. "</div>"
	end

	return l
end

function p.legacy(frame)
	local frame, args = g.getFrameAndArgs(frame)

	local codes = {'E', 'F', 'G', 'D', 'C', 'B', 'A'}
	local size_u = tonumber(args.Vert)
	local size_v = tonumber(args.Hor)
	local args2 = {}
 
	if size_u < 1 or size_v < 1 or size_u > 7 or size_v > 7 then
		return "&lt;Invalid template invocation; Hor and Vert must be integers from 1 to 7&gt;";
	end

	for u = 0, size_u - 1 do
		for v = 0, size_v - 1 do
			local code = codes[u + 1] .. tostring(v + 1)
			local key = makeKey(u, v)
			args2[key] = args[code]
			args2[key .. "-image"] = args[code .. "-image"]
			args2[key .. "-link"] = args[code .. "-link"]
		end
	end

	return p.makeGrid(
		frame,
		args2,
		size_u,
		size_v,
		args.bg == "Yes",
		g.isGiven(args.link) and args.link,
		g.isGiven(args.float) and args.float
	)
end

function p.main(frame)
	local frame, args = g.getFrameAndArgs(frame)

	local size_u = tonumber(args.size_u)
	local size_v = tonumber(args.size_v)

	if size_u < 1 or size_v < 1 or size_u > 26 or size_v > 26 then
		return "&lt;Invalid template invocation; size_u and size_v must be integers from 1 to 26&gt;";
	end

	return p.makeGrid(
		frame,
		args,
		size_u,
		size_v,
		args.bg,
		g.isGiven(args.link) and args.link,
		g.isGiven(args.float) and args.float
	)
end

return p