Module:Minimap: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
>OmegaK2
(Module for the minimap icons from the game's minimap icon sprite sheet)
 
No edit summary
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
--
--
-- Module for bestiary templates
-- Module for minimap templates
--
--


Line 26: Line 26:
-- Number of columns in the sprite sheet
-- Number of columns in the sprite sheet
local columns = 14
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


-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
Line 51: Line 80:


function p.minimap_icon(frame)
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
     -- Get args
     tpl_args = getArgs(frame, {
     local tpl_args = getArgs(frame, {
         parentFirst = true
         parentFirst = true
     })
     })
     frame = m_util.misc.get_frame(frame)
     frame = m_util.misc.get_frame(frame)
      
      
     local size
     -- Validate & convert size to pixels
     if tpl_args.size == 'large' or tpl_args.size == '64' then
     h.args.size(tpl_args, frame)
        size = 64
    elseif tpl_args.size == 'medium' or tpl_args.size == '32'then
        size = 32
    elseif tpl_args.size == 'small' or 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
      
      
     minimap = mw.loadData('Module:Minimap/minimap_icons_lookup')
     local minimap = mw.loadData('Module:Minimap/minimap_icons')
      
      
     index = minimap[tpl_args.id]
     local ratio = 64/tpl_args.size
    if index == nil then
        error(string.format(i18n.errors.invalid_minimap_icon, tostring(tpl_args.id)))
    end
   
    -- Offset for lua indexes
    index = index - 1
    column = index % columns
    row = math.floor(index/columns)
      
      
     local span = mw.html.create('span')
     local span = mw.html.create('span')
     span
     span
         :addClass('minimap_icon')
         :addClass('minimap_icon')
         :addClass('minimap_' .. size)
         :addClass('minimap_grid')
         :css('background-position-x', (-1 * column * size) .. 'px')
        :css('width', x/ratio .. 'px')
         :css('background-position-y', (-1 * row * size) .. 'px')
         :css('height', y/ratio .. 'px')
         :css('background-size', string.format('%spx auto', x/ratio))
          
          
     if tpl_args.text then
     for index, data in ipairs(minimap) do
        local row, column = h.position(index)
         span
         span
            :addClass('tooltip-activator')
             :tag('span')
             :tag('span')
                 :addClass('tooltip-content')
                 :addClass('tooltip-activator')
                :wikitext(tpl_args.text)
                :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
     end
   
       
     return tostring(span)
     return tostring(span)
end
end

Latest revision as of 13:25, 25 September 2021

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