Code:
function onload(saved_data)
updateDecals()
end
function onDropped(_color)
updateDecals()
end
function updateDecals()
local decalMap = Global.call('getDecalMap')
local decalTable = self.getDecals() or {}
-- add any decals on object to the map / update existing
for i=1, #decalTable do
decalMap[decalTable[i].name] = decalTable[i].url
end
-- clear decals on this object
decalTable = {}
self.setDecals({})
-- gather information on all decals
local count = 0
for k,v in pairs(decalMap) do
count = count + 1
local decal = {}
decal.name = k
decal.url = v
table.insert(decalTable, decal)
end
table.sort (decalTable, function (k1, k2) return k1.name < k2.name end )
-- x * x * 1.5 > count
local maxX = math.ceil(math.sqrt(count / 1.5)) - 1
local scale = 2.0 / (maxX + 1)
local x = 0
local y = 0
for _,v in ipairs(decalTable) do
v.position = {-x * scale + 1.0 - scale/2, 1.2, y * scale - 1.5 + scale/2}
v.rotation = {90,180,0}
v.scale = {scale,scale,1.0}
if (x < maxX) then
x = x + 1
else
x = 0
y = y + 1
end
end
-- we do this several times as there appears to be an issue with updates
--- seemingly, setDecals doesn't clear all of the existing decals (even after the earlier clear).
--- There do not seem to be issues with it not adding new ones, only not removing old ones.
self.setDecals(decalTable)
self.setDecals(decalTable)
self.setDecals(decalTable)
end
--- Add to Global script
function getDecalMap()
decalMapTable = decalMapTable or {}
return decalMapTable
end