Module:Sprite: Difference between revisions
Jump to navigation
Jump to search
(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, | ||
} | } | ||
setmetatable( | setmetatable(self.__settings, {__index=defaultSettings}) | ||
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 | 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 | ||
append("background-position", '-' .. left .. 'px -' .. top .. 'px') | |||
return table.concat( styles, "; " ) | |||
end | end | ||
Line 101: | Line 108: | ||
sprite:addClass(source.settings.classname) | sprite:addClass(source.settings.classname) | ||
sprite:cssText( self:calculateStyles() ) | |||
sprite:cssText( | |||
return sprite | return sprite |
Revision as of 20:54, 20 November 2018
See Also
-- Based on work by the MC wiki: https://minecraft.gamepedia.com/ and https://minecraft.gamepedia.com/Module:Sprite
local Sprite = {}
local types = {
inventory = [[Module:InvSprite]],
}
local defaultSettings = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
function Sprite:new (type)
source = types[type]
if not(source) then
error("Unknown type: " .. tostring(type))
end
o = {
__source = require(source),
}
setmetatable(o, {__index = self})
o:load()
return o
end
function Sprite:load()
local settings = self.__source.settings
self.__settings = {
size = settings.size,
sheetWidth = settings.sheetsize,
tiles = settings.sheetsize / settings.size,
scale = settings.scale,
autoScale = settings.autoscale,
}
setmetatable(self.__settings, {__index=defaultSettings})
end
function Sprite:setItem(item)
if self.__source.ids[item] then
self:setPosition(self.__source.ids[item].pos)
else
self:setPosition(nil) --will use default
end
return self
end
function Sprite:Item(item)
local sprite = {}
setmetatable(sprite, {__index = self})
local settings = {}
setmetatable(settings, {__index = self.__settings})
sprite.__settings = settings
return sprite:setItem(item)
end
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
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 top = math.floor( pos / settings.tiles ) * settings.size * settings.scale
append("background-position", '-' .. left .. 'px -' .. top .. 'px')
return table.concat( styles, "; " )
end
function Sprite:GenerateHTML (html)
local sprite
if html then
sprite = html:tag("div")
else
sprite = mw.html.create("div")
end
sprite:addClass("sprite")
local source = self.__source
sprite:addClass(source.settings.classname)
sprite:cssText( self:calculateStyles() )
return sprite
end
return Sprite