Module:Infobox: Difference between revisions
Show only icon with tooltip for stat labels (via update-page on MediaWiki MCP Server) |
Add original dimensions to stats, render at 3x scale (via update-page on MediaWiki MCP Server) |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Stat definitions: key -> { text, icon, isBonus } | -- Stat definitions: key -> { text, icon, width, height, isBonus } | ||
-- Dimensions are original size, will be rendered at 3x | |||
local stats = { | local stats = { | ||
wgt = { text = 'Weight', icon = 'Weight.png' }, | wgt = { text = 'Weight', icon = 'Weight.png', width = 9, height = 8 }, | ||
rng = { text = 'Range', icon = 'Range.png' }, | rng = { text = 'Range', icon = 'Range.png', width = 8, height = 8 }, | ||
dmg = { text = 'Damage', icon = 'Damage.png' }, | dmg = { text = 'Damage', icon = 'Damage.png', width = 8, height = 8 }, | ||
eq = { text = 'Equip Size', icon = 'Weight.png' }, | eq = { text = 'Equip Size', icon = 'Weight.png', width = 9, height = 8 }, | ||
hp = { text = 'HP', icon = 'Health.png', isBonus = true }, | hp = { text = 'HP', icon = 'Health.png', width = 9, height = 8, isBonus = true }, | ||
mp = { text = 'MP', icon = 'Mana.png', isBonus = true }, | mp = { text = 'MP', icon = 'Mana.png', width = 7, height = 8, isBonus = true }, | ||
ab = { text = 'Ability', icon = 'Ability.png', isBonus = true }, | ab = { text = 'Ability', icon = 'Ability.png', width = 9, height = 9, isBonus = true }, | ||
mag = { text = 'Magic', icon = 'Magic.png', isBonus = true }, | mag = { text = 'Magic', icon = 'Magic.png', width = 9, height = 8, isBonus = true }, | ||
arm = { text = 'Armor', icon = 'Armor.png', isBonus = true }, | arm = { text = 'Armor', icon = 'Armor.png', width = 9, height = 8, isBonus = true }, | ||
def = { text = 'Defense', icon = 'Defense.png', isBonus = true }, | def = { text = 'Defense', icon = 'Defense.png', width = 6, height = 8, isBonus = true }, | ||
ms = { text = 'Move Speed', icon = 'Move_Speed.png', isBonus = true }, | ms = { text = 'Move Speed', icon = 'Move_Speed.png', width = 7, height = 8, isBonus = true }, | ||
as = { text = 'Attack Speed', icon = 'Attack_Speed.png', isBonus = true }, | as = { text = 'Attack Speed', icon = 'Attack_Speed.png', width = 8, height = 8, isBonus = true }, | ||
hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', isBonus = true }, | hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', width = 8, height = 8, isBonus = true }, | ||
mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', isBonus = true }, | mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', width = 7, height = 8, isBonus = true }, | ||
} | } | ||
-- Other icons with dimensions | |||
local icons = { | |||
type = { icon = 'Question_Mark.png', width = 6, height = 8 }, | |||
} | |||
local SCALE = 3 | |||
local function formatBonus(value) | local function formatBonus(value) | ||
| Line 28: | Line 36: | ||
end | end | ||
return value | return value | ||
end | |||
local function renderIcon(icon, width, height, tooltip) | |||
local w = width * SCALE | |||
local h = height * SCALE | |||
local img = string.format('[[File:%s|%dx%dpx|link=]]', icon, w, h) | |||
if tooltip then | |||
return string.format('<span title="%s">%s</span>', tooltip, img) | |||
end | |||
return img | |||
end | end | ||
| Line 71: | Line 89: | ||
end | end | ||
function p:addRow(label, value, | function p:addRow(label, value, iconKey) | ||
if not value or value == '' then | if not value or value == '' then | ||
return self | return self | ||
| Line 80: | Line 98: | ||
:css('text-align', 'right') | :css('text-align', 'right') | ||
if | local iconData = icons[iconKey] | ||
th: | if iconData then | ||
th:wikitext(renderIcon(iconData.icon, iconData.width, iconData.height, label)) | |||
else | else | ||
th:wikitext(label) | th:wikitext(label) | ||
| Line 111: | Line 128: | ||
if stat.icon then | if stat.icon then | ||
th: | th:wikitext(renderIcon(stat.icon, stat.width, stat.height, stat.text)) | ||
else | else | ||
th:wikitext(stat.text) | th:wikitext(stat.text) | ||
| Line 143: | Line 158: | ||
:addImage(spriteContent) | :addImage(spriteContent) | ||
:addRow('Slot', args.slot) | :addRow('Slot', args.slot) | ||
:addRow('Type', args.category and string.format('[[:Category:%s|%s]]', args.category, args.category), ' | :addRow('Type', args.category and string.format('[[:Category:%s|%s]]', args.category, args.category), 'type') | ||
:addStat('wgt', args.wgt) | :addStat('wgt', args.wgt) | ||
:addStat('rng', args.rng) | :addStat('rng', args.rng) | ||
Revision as of 08:13, 22 January 2026
Documentation for this module may be created at Module:Infobox/doc
local p = {}
-- Stat definitions: key -> { text, icon, width, height, isBonus }
-- Dimensions are original size, will be rendered at 3x
local stats = {
wgt = { text = 'Weight', icon = 'Weight.png', width = 9, height = 8 },
rng = { text = 'Range', icon = 'Range.png', width = 8, height = 8 },
dmg = { text = 'Damage', icon = 'Damage.png', width = 8, height = 8 },
eq = { text = 'Equip Size', icon = 'Weight.png', width = 9, height = 8 },
hp = { text = 'HP', icon = 'Health.png', width = 9, height = 8, isBonus = true },
mp = { text = 'MP', icon = 'Mana.png', width = 7, height = 8, isBonus = true },
ab = { text = 'Ability', icon = 'Ability.png', width = 9, height = 9, isBonus = true },
mag = { text = 'Magic', icon = 'Magic.png', width = 9, height = 8, isBonus = true },
arm = { text = 'Armor', icon = 'Armor.png', width = 9, height = 8, isBonus = true },
def = { text = 'Defense', icon = 'Defense.png', width = 6, height = 8, isBonus = true },
ms = { text = 'Move Speed', icon = 'Move_Speed.png', width = 7, height = 8, isBonus = true },
as = { text = 'Attack Speed', icon = 'Attack_Speed.png', width = 8, height = 8, isBonus = true },
hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', width = 8, height = 8, isBonus = true },
mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', width = 7, height = 8, isBonus = true },
}
-- Other icons with dimensions
local icons = {
type = { icon = 'Question_Mark.png', width = 6, height = 8 },
}
local SCALE = 3
local function formatBonus(value)
if not value or value == '' then
return nil
end
local num = tonumber(value)
if num and num > 0 then
return '+' .. value
end
return value
end
local function renderIcon(icon, width, height, tooltip)
local w = width * SCALE
local h = height * SCALE
local img = string.format('[[File:%s|%dx%dpx|link=]]', icon, w, h)
if tooltip then
return string.format('<span title="%s">%s</span>', tooltip, img)
end
return img
end
function p.new(frame)
local builder = {
root = mw.html.create('table'),
frame = frame
}
builder.root
:addClass('wikitable')
:addClass('infobox')
:css('float', 'right')
:css('clear', 'right')
:css('margin', '0 0 1em 1em')
:css('width', '250px')
setmetatable(builder, { __index = p })
return builder
end
function p:addClass(class)
self.root:addClass(class)
return self
end
function p:addHeader(text)
self.root:tag('tr')
:tag('th')
:attr('colspan', 2)
:css('text-align', 'center')
:wikitext(text)
return self
end
function p:addImage(content)
self.root:tag('tr')
:tag('td')
:attr('colspan', 2)
:css('text-align', 'center')
:wikitext(content)
return self
end
function p:addRow(label, value, iconKey)
if not value or value == '' then
return self
end
local row = self.root:tag('tr')
local th = row:tag('th')
:css('text-align', 'right')
local iconData = icons[iconKey]
if iconData then
th:wikitext(renderIcon(iconData.icon, iconData.width, iconData.height, label))
else
th:wikitext(label)
end
row:tag('td')
:css('text-align', 'left')
:wikitext(value)
return self
end
function p:addStat(key, value)
if not value or value == '' then
return self
end
local stat = stats[key]
if not stat then
return self
end
local displayValue = stat.isBonus and formatBonus(value) or value
local row = self.root:tag('tr')
local th = row:tag('th')
:css('text-align', 'right')
if stat.icon then
th:wikitext(renderIcon(stat.icon, stat.width, stat.height, stat.text))
else
th:wikitext(stat.text)
end
row:tag('td')
:css('text-align', 'left')
:wikitext(displayValue)
return self
end
function p:finish()
return tostring(self.root)
end
function p.item(frame)
local args = frame:getParent().args
local box = p.new(frame)
:addClass('item-infobox')
local sprite = args.sprite or args.name
local spriteContent = frame:expandTemplate{
title = 'Sprite',
args = { sprite, '64', class = 'pageimage' }
}
box:addHeader(args.name or 'Unknown')
:addImage(spriteContent)
:addRow('Slot', args.slot)
:addRow('Type', args.category and string.format('[[:Category:%s|%s]]', args.category, args.category), 'type')
:addStat('wgt', args.wgt)
:addStat('rng', args.rng)
:addStat('dmg', args.dmg)
:addStat('eq', args.eq)
:addStat('hp', args.hp)
:addStat('mp', args.mp)
:addStat('ab', args.ab)
:addStat('mag', args.mag)
:addStat('arm', args.arm)
:addStat('def', args.def)
:addStat('ms', args.ms)
:addStat('as', args.as)
:addStat('hpregen', args.hpregen)
:addStat('mpregen', args.mpregen)
local categories = ''
if args.category then
categories = categories .. '[[Category:' .. args.category .. ']]'
end
return box:finish() .. categories
end
return p