13 Articles
Project Nakagami

Template:ItemIcon: Difference between revisions

No edit summary
No edit summary
Tag: Reverted
Line 1: Line 1:
<includeonly>{{#invoke:ItemIcon|AddSprite|icon={{{icon|}}}|size={{{size|}}}|link={{{link|}}}}}</includeonly><noinclude>
local p = {} --package
Shows an item icon, such as {{#invoke:ItemIcon|AddSprite|icon=minecraft:apple#0}} an apple.


The following parameters are supported (all are optional):
-- returns HTML, name, defaultLink
{| class="wikitable"
function p.AddSpriteInternal(icon, size, additionalCssText)
|-
    local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet
! scope="col"| Parameter
    local SHEET_SIZE_X = 2048 -- how wide the spritesheet image is
! scope="col"| Use
    local SHEET_SIZE_Y = 23040 -- how tall the spritesheet image is
! scope="col"| Default
    local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element
|-
| icon
| The name of the icon to show. Names are defined in [[Module:ItemIconData]]
| none
|-
| size
| How large, in pixels, to show the icon
| 32
|-
| link
| An internal link (like <code>Fluxduct</code>), or an external link
| (none)
|}


</noinclude>
    local DataTable = mw.loadData('Module:ItemIconData')
    if not DataTable then DataTable = {} end
 
    local DataFromTable = DataTable.Locations[icon]
    if not DataFromTable then DataFromTable = { X = 0, Y = 0, N = 'NONE', L = nil } end
 
    local SpriteX = DataFromTable.X
    local SpriteY = DataFromTable.Y
 
    local IconScale = size / SHEET_ICON_SIZE
    local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
    local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
    local ScaledSheetSizeX = SHEET_SIZE_X * IconScale
    local ScaledSheetSizeY = SHEET_SIZE_Y * IconScale
 
    local SpriteHTML = mw.html.create('span');
    SpriteHTML:addClass(SPAN_CLASS)
   
    local StyleItems = {}
    StyleItems[#StyleItems + 1] = 'height:' .. size .. 'px'
    StyleItems[#StyleItems + 1] = 'width:' .. size .. 'px'
    StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px'
    StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'
 
    SpriteHTML:cssText(table.concat(StyleItems, ';') .. ';' .. additionalCssText)
 
    return tostring(SpriteHTML), (DataFromTable.N or 'NONE'), DataFromTable.L
end
 
-- Allowed Arguments:
-- icon: The name of the icon to show, defined in the image index.
-- size: The size, in pixels, to use for the icon on the page.
-- link: The page to link to if the icon is clicked
function p.AddSprite(frame)
    local DEFAULT_ICON_SIZE = 32 -- how big icons should be on the page if not specifically sized
   
    local IconName = frame:getArgument('icon'):expand()
    if not IconName then IconName = 'none' end
 
    local IconSize = tonumber(frame:getArgument('size'):expand())
    if not IconSize then IconSize = DEFAULT_ICON_SIZE end
 
    local LinkDest = frame:getArgument('link'):expand()
    if not LinkDest then LinkDest = '' end
 
    local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(IconName, IconSize, '')
 
    local UsedLink = link or DefaulLink
 
    if UsedLink ~= '' then
        if UsedLink:find('//') then -- External Link
            return '[' .. UsedLink .. ' ' .. IconHTML .. ']'
        else -- Internal Link
            return '[[' .. UsedLink .. '|' .. IconHTML .. ']]'
        end
    else
        return IconHTML
    end
end
 
function p.AddSpritePredef(frame)
    local FORMATS = {
        ['inline'] = { Size=32, Link=true, Newline=false, Name=true },
        ['itemtable'] = { Size=64, Link=true, Newline=true, Name=true },
        ['icon'] = { Size=32, Link=true, Newline=false, Name=false },
        ['nolink'] = { Size=32, Link=false, Newline=false, Name=true }
    }
 
    TemplateCall = frame:getParent();
   
    local ItemRef = TemplateCall:getArgument('item'):expand()
    if not ItemRef then ItemRef = TemplateCall.args[1] end
    if not ItemRef then ItemRef = 'none' end
 
    local FormatName = TemplateCall:getArgument('format'):expand()
    if not FormatName then FormatName = TemplateCall.args[2] end
    if (not FormatName) or (not FORMATS[FormatName]) then FormatName = 'inline' end
 
    local Format = FORMATS[FormatName]
 
    local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(ItemRef, Format.Size, '')
 
    local Link = TemplateCall:getArgument('link'):expand()
    if (not Link) or (Link == '') then Link = DefaultLink end
   
    local Formatted = IconHTML .. (Format.Newline and '<br />' or '') .. (Format.Name and ItemName or '')
 
    if Format.Link and Link then
        if Link:find('//') then -- External Link
            return '[' .. Link .. ' ' .. Formatted .. ']'
        else -- Internal Link
            return '[[' .. Link .. '|' .. Formatted .. ']]'
        end
    else
        return Formatted
    end
end
 
return p

Revision as of 05:21, 16 August 2024

local p = {} --package

-- returns HTML, name, defaultLink function p.AddSpriteInternal(icon, size, additionalCssText)

   local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet
   local SHEET_SIZE_X = 2048 -- how wide the spritesheet image is
   local SHEET_SIZE_Y = 23040 -- how tall the spritesheet image is
   local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element
   local DataTable = mw.loadData('Module:ItemIconData')
   if not DataTable then DataTable = {} end
   local DataFromTable = DataTable.Locations[icon]
   if not DataFromTable then DataFromTable = { X = 0, Y = 0, N = 'NONE', L = nil } end
   local SpriteX = DataFromTable.X
   local SpriteY = DataFromTable.Y
   local IconScale = size / SHEET_ICON_SIZE
   local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
   local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
   local ScaledSheetSizeX = SHEET_SIZE_X * IconScale
   local ScaledSheetSizeY = SHEET_SIZE_Y * IconScale
   local SpriteHTML = mw.html.create('span');
   SpriteHTML:addClass(SPAN_CLASS)
   
   local StyleItems = {}
   StyleItems[#StyleItems + 1] = 'height:' .. size .. 'px'
   StyleItems[#StyleItems + 1] = 'width:' .. size .. 'px'
   StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px'
   StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'
   SpriteHTML:cssText(table.concat(StyleItems, ';') .. ';' .. additionalCssText)
   return tostring(SpriteHTML), (DataFromTable.N or 'NONE'), DataFromTable.L

end

-- Allowed Arguments: -- icon: The name of the icon to show, defined in the image index. -- size: The size, in pixels, to use for the icon on the page. -- link: The page to link to if the icon is clicked function p.AddSprite(frame)

   local DEFAULT_ICON_SIZE = 32 -- how big icons should be on the page if not specifically sized
   
   local IconName = frame:getArgument('icon'):expand()
   if not IconName then IconName = 'none' end
   local IconSize = tonumber(frame:getArgument('size'):expand())
   if not IconSize then IconSize = DEFAULT_ICON_SIZE end
   local LinkDest = frame:getArgument('link'):expand()
   if not LinkDest then LinkDest =  end
   local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(IconName, IconSize, )
   local UsedLink = link or DefaulLink
   if UsedLink ~=  then
       if UsedLink:find('//') then -- External Link
           return '[' .. UsedLink .. ' ' .. IconHTML .. ']'
       else -- Internal Link
           return '' .. IconHTML .. ''
       end
   else
       return IconHTML
   end

end

function p.AddSpritePredef(frame)

   local FORMATS = {
       ['inline'] = { Size=32, Link=true, Newline=false, Name=true },
       ['itemtable'] = { Size=64, Link=true, Newline=true, Name=true },
       ['icon'] = { Size=32, Link=true, Newline=false, Name=false },
       ['nolink'] = { Size=32, Link=false, Newline=false, Name=true }
   }
   TemplateCall = frame:getParent();
   
   local ItemRef = TemplateCall:getArgument('item'):expand()
   if not ItemRef then ItemRef = TemplateCall.args[1] end
   if not ItemRef then ItemRef = 'none' end
   local FormatName = TemplateCall:getArgument('format'):expand()
   if not FormatName then FormatName = TemplateCall.args[2] end
   if (not FormatName) or (not FORMATS[FormatName]) then FormatName = 'inline' end
   local Format = FORMATS[FormatName]
   local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(ItemRef, Format.Size, )
   local Link = TemplateCall:getArgument('link'):expand()
   if (not Link) or (Link == ) then Link = DefaultLink end
   
   local Formatted = IconHTML .. (Format.Newline and '
' or ) .. (Format.Name and ItemName or )
   if Format.Link and Link then
       if Link:find('//') then -- External Link
           return '[' .. Link .. ' ' .. Formatted .. ']'
       else -- Internal Link
           return '' .. Formatted .. ''
       end
   else
       return Formatted
   end

end

return p