Module:Sandbox/Supernovah/Minimap

-- -- Module for bestiary templates --

--local m_cargo = require('Module:Cargo') local m_util = require('Module:Util') local getArgs = require('Module:Arguments').getArgs

local p = {}

-- -- Strings --

local i18n = { errors = { invalid_icon_size = 'The specified icon size "%s" is invalid. Only large (64), medium (32) and small (16) are supported.', invalid_minimap_icon = 'The specified minimap icon id "%s" could not be found', }, }

-- -- Helper functions and globals --

-- Number of columns in the sprite sheet local columns = 14 local x = 896 local y = 2432

local h = {}

function h.position(index) -- Offset for lua indexes index = index - 1 column = index % columns row = math.floor(index/columns) return row, column end

h.args = {} function h.args.size(tpl_args) local size if tpl_args.size == 'large' or tonumber(tpl_args.size) == 64 then size = 64 elseif tpl_args.size == 'medium' or tonumber(tpl_args.size) == 32 then size = 32 elseif tpl_args.size == 'small' or tonumber(tpl_args.size) == 16 or tpl_args.size == nil then size = 16 else error(string.format(i18n.errors.invalid_icon_size, tpl_args.size)) end tpl_args.size = size end

-- -- Cargo tables -- --[[local tables = {}

tables.table_name = { table = 'table_name', order = {'id'}, fields = { id = { field = 'id', type = 'String', required = true, },	}, } ]] -- -- Page functions --

local p = {}

function p.minimap_icon(frame) local args = getArgs(frame, {		parentFirst = true	})

return p._minimap_icon(args) end

-- param args -- args.id string from minimap_icons_lookup -- args.size 'small', 'medium', 'large', '16', '32', '64', 16, 32, 64, or nil -- args.text string function p._minimap_icon(args) -- Validate & convert size to pixels h.args.size(args) local minimap = mw.loadData('Module:Minimap/minimap_icons_lookup') local index = minimap[args.id] if index == nil then error(string.format(i18n.errors.invalid_minimap_icon, tostring(args.id))) end local row, column = h.position(index) local span = mw.html.create('span') span :addClass('minimap_icon') :addClass('minimap_' .. args.size) :css('background-position-x', (-1 * column * args.size) .. 'px') :css('background-position-y', (-1 * row * args.size) .. 'px') if args.text then span :addClass('tooltip-activator') :tag('span') :addClass('tooltip-content') :wikitext(args.text) end return tostring(span) end

function p.minimap_icon_list(frame) local args = getArgs(frame, {		parentFirst = true	})

return p._minimap_icon_list(args) end

-- renders a list of dot separated icons function p._minimap_icon_list(args) local namedArgs = {} local rest = {}

for key, val in pairs(args) do		if type(key) ~= 'number' then -- rest namedArgs[key] = val else -- id, size, etc table.insert(rest, val) end end local minimap = mw.loadData('Module:Minimap/minimap_icons_lookup') local span = mw.html.create('span') -- the parent element

span:cssText('margin: auto; text-align: center;')

local count = 0

for i, val in pairs(rest) do		span:wikitext(p._minimap_icon({ id = val, size = namedArgs.size }))

count = count + 1 end

return tostring(span) end

-- -- End --

return p