Module:Stonecutting: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
------------------------------------------------------------ | ------------------------------------------------------------ | ||
-- Stone-cutter recipe carousel | -- Module:Stonecutting · Stone-cutter recipe carousel | ||
------------------------------------------------------------ | ------------------------------------------------------------ | ||
local p = {} | local p = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
| Line 10: | Line 11: | ||
------------------------------------------------------------------------ | ------------------------------------------------------------------------ | ||
local function slotImage(label, image, link) | local function slotImage(label, image, link) | ||
if not label or label == '' then return '' end | if not label or label == '' then | ||
local file | return '' | ||
local tgt | end | ||
local file = (image ~= '' and image) or ('Grid_' .. label .. '.png') | |||
local tgt = (link ~= '' and link ) or label | |||
return string.format( | return string.format( | ||
'[[File:%s|32px|class=pixelated|link=%s|alt=%s]]', | '[[File:%s|32px|class=pixelated|link=%s|alt=%s]]', | ||
| Line 23: | Line 26: | ||
------------------------------------------------------------------------ | ------------------------------------------------------------------------ | ||
local function buildFrame(args, idx) | local function buildFrame(args, idx) | ||
local inL = args.input | local inL = args.input or args[1] or '' | ||
local inI = args['input-image'] | local inI = args['input-image'] or '' | ||
local inLn = args['input-link'] | local inLn = args['input-link'] or '' | ||
local outL = args['output' .. idx] | local outL = args['output' .. idx] or args[idx + 1] or '' | ||
local outI = args['output' .. idx .. '-image'] or '' | local outI = args['output' .. idx .. '-image'] or '' | ||
local outLn = args['output' .. idx .. '-link'] or '' | local outLn = args['output' .. idx .. '-link'] or '' | ||
| Line 34: | Line 37: | ||
html:wikitext('[[File:StonecutterGUI.png|180px|link=|alt=]]') | html:wikitext('[[File:StonecutterGUI.png|180px|link=|alt=]]') | ||
html:tag('div'):addClass('sc-slot') | html:tag('div'):addClass('sc-slot') | ||
:css{left = '11px', top | :css{left = '11px', top = '50px'} | ||
:wikitext(slotImage(inL, | :wikitext(slotImage(inL, inI, inLn)) | ||
html:tag('div'):addClass('sc-slot') | html:tag('div'):addClass('sc-slot') | ||
:css{left = '56.5px', top = '23px'} | :css{left = '56.5px', top = '23px'} | ||
:wikitext(slotImage(outL, outI, outLn)) | :wikitext(slotImage(outL, outI, outLn)) | ||
html:tag('div'):addClass('sc-slot') | html:tag('div'):addClass('sc-slot') | ||
:css{left = '127px', top = '50px'} | :css{left = '127px', top = '50px'} | ||
| Line 62: | Line 62: | ||
while true do | while true do | ||
local raw = args['output' .. (count + 1)] or args[count + 2] | local raw = args['output' .. (count + 1)] or args[count + 2] | ||
if not raw or trim(tostring(raw)) == '' then break end | if not raw or trim(tostring(raw)) == '' then | ||
break | |||
end | |||
count = count + 1 | count = count + 1 | ||
end | end | ||
| Line 69: | Line 71: | ||
end | end | ||
-- outer wrapper | -- outer wrapper | ||
local outer = mw.html.create('div') | local outer = mw.html.create('div') | ||
:addClass('stonecutting-carousel') | :addClass('stonecutting-carousel') | ||
| Line 80: | Line 80: | ||
end | end | ||
-- | ------------------------------------------------------------------ | ||
-- 1. STATIC stylesheet (same for every page) | |||
------------------------------------------------------------------ | |||
outer:wikitext(frame:extensionTag( | outer:wikitext(frame:extensionTag( | ||
'templatestyles', | 'templatestyles', | ||
nil, -- *no* body | |||
{src = 'Stonecutting/styles.css'} -- link to page | |||
)) | )) | ||
------------------------------------------------------------------ | ------------------------------------------------------------------ | ||
-- | -- 2. DYNAMIC stylesheet (depends on #outputs) | ||
------------------------------------------------------------------ | ------------------------------------------------------------------ | ||
local dur = count * 4 -- 4 s per | local dur = count * 4 -- 4 s per recipe | ||
local pct = 100 / count | local pct = 100 / count | ||
local vis = pct - 0.01 -- stay visible almost the whole slice | |||
local css = {} | local css = {} | ||
css[#css+1] = string.format( | css[#css+1] = string.format( | ||
'.stonecutting-carousel .sc-frame{animation:sc-cycle %ds | '.stonecutting-carousel .sc-frame{animation:sc-cycle %ds step-start infinite;}', | ||
dur | dur | ||
) | ) | ||
for i = 1, count do | for i = 1, count do | ||
css[#css+1] = string.format( | css[#css+1] = string.format( | ||
| Line 107: | Line 107: | ||
) | ) | ||
end | end | ||
css[#css+1] = string.format( | css[#css+1] = string.format( | ||
'@keyframes sc-cycle{0%%{opacity:1;} %.2f%%{opacity:1;} %.2f%%{opacity:0;} 100%%{opacity:0;}}', | '@keyframes sc-cycle{0%%{opacity:1;} %.2f%%{opacity:1;} %.2f%%{opacity:0;} 100%%{opacity:0;}}', | ||
vis, pct | |||
) | ) | ||
-- | -- ordinary <style> tag is safe and avoids the templatestyles pitfalls | ||
outer: | outer:tag('style'):wikitext(table.concat(css)) | ||
return tostring(outer) | return tostring(outer) | ||
Revision as of 02:16, 14 June 2025
Documentation for this module may be created at Module:Stonecutting/doc
------------------------------------------------------------
-- Module:Stonecutting · Stone-cutter recipe carousel
------------------------------------------------------------
local p = {}
local getArgs = require('Module:Arguments').getArgs
local trim = mw.text.trim
------------------------------------------------------------------------
-- slotImage(label, image, link) → wikitext thumbnail
------------------------------------------------------------------------
local function slotImage(label, image, link)
if not label or label == '' then
return ''
end
local file = (image ~= '' and image) or ('Grid_' .. label .. '.png')
local tgt = (link ~= '' and link ) or label
return string.format(
'[[File:%s|32px|class=pixelated|link=%s|alt=%s]]',
file, tgt, label
)
end
------------------------------------------------------------------------
-- buildFrame(args, idx) → html for one output variant
------------------------------------------------------------------------
local function buildFrame(args, idx)
local inL = args.input or args[1] or ''
local inI = args['input-image'] or ''
local inLn = args['input-link'] or ''
local outL = args['output' .. idx] or args[idx + 1] or ''
local outI = args['output' .. idx .. '-image'] or ''
local outLn = args['output' .. idx .. '-link'] or ''
local html = mw.html.create('div'):addClass('sc-frame')
html:wikitext('[[File:StonecutterGUI.png|180px|link=|alt=]]')
html:tag('div'):addClass('sc-slot')
:css{left = '11px', top = '50px'}
:wikitext(slotImage(inL, inI, inLn))
html:tag('div'):addClass('sc-slot')
:css{left = '56.5px', top = '23px'}
:wikitext(slotImage(outL, outI, outLn))
html:tag('div'):addClass('sc-slot')
:css{left = '127px', top = '50px'}
:wikitext(slotImage(outL, outI, outLn))
return tostring(html)
end
------------------------------------------------------------------------
-- main entry
------------------------------------------------------------------------
function p.carousel(frame)
local args = getArgs(frame, {removeBlanks = false})
-- count non-blank outputs
local count = 0
while true do
local raw = args['output' .. (count + 1)] or args[count + 2]
if not raw or trim(tostring(raw)) == '' then
break
end
count = count + 1
end
if count == 0 then
return '<span style="color:red">Stonecutting: no outputs supplied</span>'
end
-- outer wrapper
local outer = mw.html.create('div')
:addClass('stonecutting-carousel')
:css{width = '180px', height = '131px', position = 'relative'}
for i = 1, count do
outer:wikitext(buildFrame(args, i))
end
------------------------------------------------------------------
-- 1. STATIC stylesheet (same for every page)
------------------------------------------------------------------
outer:wikitext(frame:extensionTag(
'templatestyles',
nil, -- *no* body
{src = 'Stonecutting/styles.css'} -- link to page
))
------------------------------------------------------------------
-- 2. DYNAMIC stylesheet (depends on #outputs)
------------------------------------------------------------------
local dur = count * 4 -- 4 s per recipe
local pct = 100 / count
local vis = pct - 0.01 -- stay visible almost the whole slice
local css = {}
css[#css+1] = string.format(
'.stonecutting-carousel .sc-frame{animation:sc-cycle %ds step-start infinite;}',
dur
)
for i = 1, count do
css[#css+1] = string.format(
'.stonecutting-carousel .sc-frame:nth-child(%d){animation-delay:%ds;}',
i, (i - 1) * 4
)
end
css[#css+1] = string.format(
'@keyframes sc-cycle{0%%{opacity:1;} %.2f%%{opacity:1;} %.2f%%{opacity:0;} 100%%{opacity:0;}}',
vis, pct
)
-- ordinary <style> tag is safe and avoids the templatestyles pitfalls
outer:tag('style'):wikitext(table.concat(css))
return tostring(outer)
end
return p