Module:Icon

From Artifact Wiki
Jump to: navigation, search

Documentation for Module:Icon [edit] [edit doc]

Info Icon.png See also: Icon template.

This module provides improtant icons as a formatted image.

Preview


Attack
Weapon
Attack
Armor Armor
Health
Accessory
Health
Consumable Consumable
Spell Spell
Improvement Improvement
Cooldown Cooldown
Gold Gold
None
Default Default

Usage


Within templates:
{{#invoke:Icon|main}}

Within modules:

1 -- At the start of the program
2 local getIcon = require('Module:Icon').getIcon
1 -- Where you need the icon
2 local icon = getIcon('icon name')

local getArgs = require( 'Module:Arguments' ).main
local p = {}


function p.main(frame)
	-- Normalize arguments
	local args = getArgs()
	return p._main( args )
end


function p._main(args)
	local name = args[1]			-- 1. argument of the template (Name)
	local size = args[2]			-- 2. argument of the template (Size) [optional]
	local caption = args[3]			-- 3. argument of the template (Caption) [optional]
	local link = args['link']		-- 'link' argument of the template (Link) [optional]
	local background = args['bg']	-- 'bg' argument of the template (Background) [optional]
	
	return p.getIcon(name, size, caption, link, background)
end


-- Returns the formatted Icon. You can use this function in other modules.
function p.getIcon(name, size, caption, link, background)
	-- Fix size format, if needed
	if(not p.isempty(size) and string.sub(size, -2) == 'px') then
		size = string.gsub(size, "px", "")
	end
	if(p.isempty(link)) then
		link = ''
	end
	if(p.isempty(background)) then
		background = 'false'
	end
	
	size = size or '20'			-- Default is 20
	caption = caption or name	-- Default is Name
	link = link or ''			-- Default is None
	
	local file = p.getFile(name)
	
	-- If the icon is 'none', return without formatting
	if(p.isempty(file)) then
		return ''
	end
  	
  	local formattedImage = string.format('[[%s|link=%s|%spx|%s]]', file, link, size, caption)
  	
  	-- Ignore casing
  	background = string.lower(background)
  	
  	-- Add background if not specified otherwise
  	if(background == 'false' or background == 'no') then
  		return formattedImage
  	end
  	
  	local bgImage = string.format('[[%s|link=%s|%spx|%s]]', 'File:Icon_BG.png', link, size, caption)
  	return string.format([==[<div style="display: inline-block;"><div style="display: flex; position: relative; justify-content: center; align-items: center;"><div style="position: absolute;">%s</div><div style="z-index: 3;">%s</div></div></div>]==], bgImage, formattedImage)
end

-- Collection of useful icons
function p.getFile(name)
	name =  string.lower(name) -- Ignore casing
	
	local fileName = 'No_Icon.png' -- Default
	
	-- None
	if(name == 'none') then
		return ''
	-- Armor
	elseif(name == 'armor') then
		fileName = 'Armor_Icon.png'
	-- Common Rarity	
	elseif(name == 'common') then
		fileName = 'Common_Rarity.png'
	-- Uncommon Rarity	
	elseif(name == 'uncommon') then
		fileName = 'Uncommon_Rarity.png'	
	-- Rare Rarity	
	elseif(name == 'rare') then
		fileName = 'Rare_Rarity.png'
	-- Basic Rarity	
	elseif(name == 'basic') then
		fileName = 'Basic_Rarity.png'
	-- Attack or Weapon
	elseif(name == 'attack' or name == 'weapon') then
		fileName = 'Attack_Icon.png'
	-- Health or Accessory
	elseif(name == 'health' or name == 'accessory') then
		fileName = 'Health_Icon.png'
	-- Consumable
	elseif(name == 'consumable') then
		fileName = 'Consumable_Icon.png'
	-- Gold
	elseif(name == 'gold') then
		fileName = 'Gold Icon.png'
	-- Spell
	elseif(name == 'spell') then
		fileName = 'Spell_Icon.png'
	-- Cooldown
	elseif(name == 'cooldown') then
		fileName = 'Cooldown Icon.png'
	-- Improvement
	elseif(name == 'improvement') then
		fileName = 'Improvement_Icon.png'
	-- Creature
	elseif(name == 'creature' or name == 'creep') then
		fileName = 'Creep_Icon.png'
	-- Hero
	elseif(name == 'hero') then
		fileName = 'Hero_Icon.png'
	-- Info
	elseif(name == 'info') then
		fileName = 'Info_Icon.png'
	-- Blue
	elseif(name == 'blue') then
		fileName = 'Artifact_Blue.png'
	-- Black
	elseif(name == 'black') then
		fileName = 'Artifact_Black.png'
	-- Green
	elseif(name == 'green') then
		fileName = 'Artifact_Green.png'
	-- Red
	elseif(name == 'red') then
		fileName = 'Artifact_Red.png'
	-- Default
	else
		fileName = 'No Icon.png'
	end
	
	return string.format("File:%s", fileName)
end


function p.isempty(s)
  return s == nil or s == ''
end

return p