Module:Sprite: Difference between revisions

no edit summary
(Removed bug that resulted in using the wrong sprite if the name was unkown and setPosition calculating the right position and changing the css accordingly but storing it wrong internaly (of by 1))
No edit summary
Line 37: Line 37:
autoScale = settings.autoscale,
autoScale = settings.autoscale,
}
}
local lSet = self.__settings


setmetatable(lSet, {__index=defaultSettings})
setmetatable(self.__settings, {__index=defaultSettings})
 
 
styles = {}
--[[styles["background-image"] = (settings.url and "url(" .. settings.url .. ")") or mw.getCurrentFrame():expandTemplate{
title = 'FileUrl',
args = { settings.image}
} ]]
styles["background-size"] = lSet.sheetWidth * lSet.scale .. 'px auto'
 
local t = lSet.size * lSet.scale .. 'px'
 
styles.height = t
styles.width = t
styles["vertical-align"] = lSet.align
self.__styles = styles
end
end


Line 76: Line 60:


function Sprite:setPosition(pos)
function Sprite:setPosition(pos)
self.__settings.pos = pos or self.__settings.pos
return self
end
function Sprite:SetScale(scale)
self.__settings.scale = scale
return self
end
function Sprite:SetSize(size)
self.__settings.scale = size / self.__settings.size
return self
end
function Sprite:calculateStyles()
styles = {}
function append(key, value) styles[#styles+1]=key .. ": " .. value return table end
local settings = self.__settings
local settings = self.__settings


local pos = (pos or settings.pos) - 1
append ("background-size", settings.sheetWidth * settings.scale .. 'px auto')
 
local t = settings.size * settings.scale .. 'px'


append ("height", t)
append ("width", t)
append ("vertical-align", settings.align)
local pos = self.__settings.pos - 1
local left = pos % settings.tiles * settings.size * settings.scale
local left = pos % settings.tiles * settings.size * settings.scale
local top = math.floor( pos / settings.tiles ) * settings.size * settings.scale
local top = math.floor( pos / settings.tiles ) * settings.size * settings.scale
self.__styles["background-position"] = '-' .. left .. 'px -' .. top .. 'px'
append("background-position", '-' .. left .. 'px -' .. top .. 'px')


settings.pos = pos + 1
return table.concat( styles, "; " )
return self
end
end


Line 101: Line 108:
sprite:addClass(source.settings.classname)
sprite:addClass(source.settings.classname)


local styles = {}
sprite:cssText( self:calculateStyles() )
for k,v in pairs(self.__styles) do
styles[#styles + 1] = k .. ":" .. v
end
 
sprite:cssText( table.concat( styles, "; " ) )


return sprite
return sprite
Bureaucrats, emailconfirmed, Interface administrators, staff, Administrators, translation-admin, translator, Widget editors
154

edits