Module:SNES button

From ALttP Speedrunning Wiki
Revision as of 19:37, 2 June 2018 by Kan (talk | contribs) (ill care about getting css to load from module later)
Jump to: navigation, search

Documentation for this module may be created at button/doc&action=edit&redlink=1 Module:SNES button/doc

local p = {}

function p.main(frame)
	local args = frame:getParent().args

	local b = args[1] or 'A'

	return p._main(b)
end

function p._main(b)
	b = mw.text.trim(b:lower())

	local ret

	if b:find('[<>^vd]') then
		ret = dpad(b)
	else
		ret = button(b)
	end

	return tostring(ret)
end

function dpad(arg)
	local ret = mw.html.create('span')

	ret:addClass('snes-button-dpad')
		:tag('span')
			:addClass('snes-button-dpad-vert')
			:done()
		:tag('span')
			:addClass('snes-button-dpad-horz')
			:done()
		:tag('span')
			:addClass('snes-button-dpad-mid')
			:done()
		:tag('span')
			:addClass('snes-button-dpad-templatearg')
			:wikitext(arg)
			:done()
		:done()

	if arg:find('%^') then
		ret:addClass('snes-button-dpad-up')
	end

	if arg:find('>') then
		ret:addClass('snes-button-dpad-right')
	end

	if arg:find('v') then
		ret:addClass('snes-button-dpad-down')
	end

	if arg:find('<') then
		ret:addClass('snes-button-dpad-left')
	end

	return ret
end

function button(arg)
	local buttonClass

	local arg2 = mw.text.split(arg, '')
	arg2[1] = arg2[1]:upper()
	arg2 = table.concat(arg2)

	local exactClass = 'snes-button-' .. arg2

	if arg == 'a' or arg == 'b' or arg == 'x' or arg == 'y' then
		buttonClass = 'snes-button-face'
	elseif arg == 'l' or arg == 'r' then
		buttonClass = 'snes-button-bumper'
	elseif arg == 'start' or arg == 'select' then
		buttonClass = 'snes-button-mid'
	end
	
	local ret = mw.html.create('span')

	ret:addClass(buttonClass)
		:addClass(exactClass)
		:wikitext(arg2)

	return ret
end

return p