Advertisement
Guest User

icon builder function

a guest
Mar 8th, 2020
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. table.deepestcopy = function (tablein)
  2.   -- use to copy a non-referential table, will break for infinitely referential tables
  3.   local tableout = {}
  4.   local key = nil
  5.   local thing = nil
  6.  
  7.   for key, thing in pairs(tablein) do
  8.     if type(thing) == "table" then
  9.       tableout[key] = table.deepestcopy(thing)
  10.     else
  11.       tableout[key] = thing
  12.     end
  13.   end
  14.  
  15.   return tableout
  16. end
  17.  
  18. local local_create_icon = function(base_icons, new_icons, options)
  19.     if type(base_icons) ~= "table" then return base_icons end
  20.     if type(options) ~= "table" then options = {} end
  21.     local icon = nil
  22.  
  23.     if not options.rescale then options.rescale = 1 end
  24.     if not options.origin then options.origin = {0,0} end
  25.     if not new_icons then
  26.         if options.from ~= nil and type(options.from) == "table" then
  27.             if options.from.icons then new_icons = options.from.icons
  28.             elseif options.from.icon then new_icons = {{icon = options.from.icon}}
  29.             else error("Table given had no icons.") end
  30.  
  31.             for _, icon in pairs(new_icons) do
  32.                 if not icon.icon_size then icon.icon_size = options.from.icon_size end
  33.                 if not icon.icon_size then error("Had icon "..icon.icon.." but size is missing.")
  34.                 end
  35.             end
  36.         else
  37.           error("Couldn't build icons: no icons and no table to build from.")
  38.         end
  39.     end
  40.  
  41.     local icons = table.deepestcopy(new_icons)
  42.  
  43.     for _, icon in pairs(base_icons) do
  44.         if not icon.scale then icon.scale = 1 end
  45.         if not icon.shift then icon.shift = {0,0} end
  46.     end
  47.  
  48.     if options.type == nil or options.type == "recipe" then
  49.         for _, icon in pairs(icons) do
  50.             if not icon.icon_size then icon.icon_size = 32 end
  51.         end
  52.     end
  53.  
  54.     local extra_scale
  55.     for _, icon in pairs(icons) do
  56.         if not icon.scale then icon.scale = 1 end
  57.         if not icon.shift then icon.shift = {0,0} end
  58.  
  59.         extra_scale = 1
  60.         if base_icons[1] then
  61.             if (base_icons[1].icon_size * base_icons[1].scale) ~= (icon.icon_size * icon.scale) then
  62.                 extra_scale = (base_icons[1].icon_size * base_icons[1].scale) / (icon.icon_size)
  63.             end
  64.         else
  65.             if (icons[1].icon_size * icons[1].scale) ~= (icon.icon_size * icon.scale) then
  66.                 extra_scale = (icons[1].icon_size * icons[1].scale) / (icon.icon_size)
  67.             end
  68.         end
  69.  
  70.         icon.shift[1] = icon.shift[1]/icon.scale
  71.         icon.shift[2] = icon.shift[2]/icon.scale
  72.  
  73.         icon.scale = icon.scale * options.rescale * extra_scale
  74.         icon.shift[1] = icon.shift[1] * icon.scale + options.origin[1] * icon.scale * icon.icon_size
  75.         icon.shift[2] = icon.shift[2] * icon.scale + options.origin[2] * icon.scale * icon.icon_size
  76.     end
  77.  
  78.     for _, icon in pairs(icons) do
  79.         table.insert(base_icons, icon)
  80.     end
  81. --[[
  82.     for _, icon in pairs(base_icons) do
  83.         if icon.shift and icon.shift[1] == 0 and icon.shift[2] == 0 then icon.shift = nil end
  84.         if icon.scale and icon.scale == 1 then icon.scale = nil end
  85.     end
  86. --]]
  87.     return base_icons
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement