Module:Minimap

From Path of Exile Wiki
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


This Module is used by Template:Minimap icon.

Related asset File:Minimap icons.png

--
-- Module for minimap 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 = 2816

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, frame)
    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

-- complete chart of all the icons rather then just single icons to reference the IDs
function p.minimap_chart(frame)
    -- Get args
    local tpl_args = getArgs(frame, {
        parentFirst = true
    })
    frame = m_util.misc.get_frame(frame)
    
    -- Validate & convert size to pixels
    h.args.size(tpl_args, frame)
    
    local minimap = mw.loadData('Module:Minimap/minimap_icons')
    
    local ratio = 64/tpl_args.size
    
    local span = mw.html.create('span')
    span
        :addClass('minimap_icon')
        :addClass('minimap_grid')
        :css('width', x/ratio .. 'px')
        :css('height', y/ratio .. 'px')
        :css('background-size', string.format('%spx auto', x/ratio))
        
    for index, data in ipairs(minimap) do
        local row, column = h.position(index)
        span
            :tag('span')
                :addClass('tooltip-activator')
                :addClass('minimap_'  .. tpl_args.size)
                -- CSS also starts counting at one it seems
                :css('grid-area', string.format('%s / %s', row+1, column+1, row+1, column+1))
                --:css('top', (tpl_args.size * row) .. 'px')
                --:css('left', (tpl_args.size * column) .. 'px')
                :tag('span')
                    :addClass('tooltip-content')
                    :wikitext(data.id)
    end
        
    return tostring(span)
end

-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------

return p