Module:Sprite: Difference between revisions

Jump to navigation Jump to search
Added Custom Sprite Support
(Remove sprite level inline support)
(Added Custom Sprite Support)
Line 14: Line 14:
align = 'text-top'
align = 'text-top'
}
}
function getImage( image )
if not(image) then return nil end
local file = mw.title.new(image,"File").file
if not(file.exists) then return nil end
local f = mw.getCurrentFrame()
local t = {
url = f:expandTemplate{
title = 'FileUrl',
args = { image}
}
}
setmetatable(t, {__index = file})
return t
end


function Sprite:new (type)
function Sprite:new (type)
source = types[type]
local source = types[type]
if not(source) then
if not(source) then
error("Unknown type: " .. tostring(type))
error("Unknown type: " .. tostring(type))
end
end
o = {
o = {
__source = require(source),
__source = require(source),
}
setmetatable(o, {__index = self})
o:load()
return o
end
 
Sprite.Custom = {}
setmetatable(Sprite.Custom, {__index = Sprite})
local CustomSprite = Sprite.Custom
 
function CustomSprite:new(name, imagename)
local image = getImage(imagename)
if not(image) then
error("Couldn't find image: " .. tostring(imagename))
end
 
local size = image.width
 
o = {
__source = {
settings={
ids={[name]=1},
size = size,
sheetsize = size,
image = image
}
}
}
}
setmetatable(o, {__index = self})
setmetatable(o, {__index = self})
Line 36: Line 82:
scale = settings.scale,
scale = settings.scale,
autoScale = settings.autoscale,
autoScale = settings.autoscale,
image = settings.image or getImage(settings.imagename)
}
}


Line 77: Line 124:
function Sprite:calculateStyles()
function Sprite:calculateStyles()
styles = {}
styles = {}
function append(key, value) styles[#styles+1]=key .. ": " .. value return table end
function append(key, value) if value ~= nil then styles[#styles+1]=key .. ": " .. value end end
local settings = self.__settings
local settings = self.__settings


Line 92: Line 139:
local top = math.floor( pos / settings.tiles ) * settings.size * settings.scale
local top = math.floor( pos / settings.tiles ) * settings.size * settings.scale
append("background-position", '-' .. left .. 'px -' .. top .. 'px')
append("background-position", '-' .. left .. 'px -' .. top .. 'px')
if settings.image then
append("background", "url(" .. settings.image.url .. ")")
end


return table.concat( styles, "; " )
return table.concat( styles, "; " )
Line 99: Line 149:
local sprite
local sprite
if html then
if html then
sprite = html:tag("div")
if type(html) == "string" then
sprite = mw.html.create(html)
else
sprite = html:tag("div")
end
else
else
sprite = mw.html.create("div")
sprite = mw.html.create("div")
Bureaucrats, emailconfirmed, Interface administrators, staff, Administrators, translation-admin, translator, Widget editors
154

edits

Navigation menu