MattiasBuelens

CCGUI border

Dec 18th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. --[[
  2.  
  3.     ComputerCraft GUI
  4.     Border
  5.  
  6. --]]
  7.  
  8. local Border = common.newClass{
  9.     top     = 0,
  10.     right   = 0,
  11.     bottom  = 0,
  12.     left    = 0
  13. }
  14. ccgui.Border = Border
  15.  
  16. function ccgui.newBorder(...)
  17.     local args = { ... }
  18.     local n = #args
  19.  
  20.     -- Defaults
  21.     local top, right, bottom, left
  22.  
  23.     if n >= 4 then
  24.         -- All colors
  25.         top, right, bottom, left = unpack(args)
  26.     elseif n == 1 then
  27.         local x = args[1]
  28.         if type(x) == "table" then
  29.             -- Clone colors
  30.             top, right, bottom, left = x.top, x.right, x.bottom, x.left
  31.         else
  32.             -- One color for all
  33.             widths = ccgui.newMargins(x)
  34.         end
  35.     elseif n == 2 then
  36.         -- Vertical and horizontal colors
  37.         top, right, bottom, left = args[1], args[2], args[1], args[2]
  38.     elseif n == 3 then
  39.         -- Top, horizontal and bottom margins
  40.         top, right, bottom, left = args[1], args[2], args[3], args[2]
  41.     end
  42.  
  43.     return Border:new{
  44.         top     = tonumber(top),
  45.         right   = tonumber(right),
  46.         bottom  = tonumber(bottom),
  47.         left    = tonumber(left)
  48.     }
  49. end
  50.  
  51. function Border:get(side)
  52.     return self[side] or 0
  53. end
  54.  
  55. function Border:set(side, color)
  56.     if type(side) == "string" then
  57.         -- setColor(side, color)
  58.         self[side] = (color or 0)
  59.     elseif type(side) == "number" then
  60.         -- setColor(color)
  61.         color = side
  62.         self:set("top", color)
  63.         self:set("right", color)
  64.         self:set("bottom", color)
  65.         self:set("left", color)
  66.     end
  67. end
  68.  
  69. function Border:has(side)
  70.     return self[side] ~= 0
  71. end
  72.  
  73. function Border:margins()
  74.     return ccgui.Margins:new{
  75.         top     = self:has("top")       and 1 or 0,
  76.         right   = self:has("right")     and 1 or 0,
  77.         bottom  = self:has("bottom")    and 1 or 0,
  78.         left    = self:has("left")      and 1 or 0
  79.     }
  80. end
  81.  
  82. function Border:__tostring()
  83.     return "Border["
  84.         ..self:get("top")..","..self:get("right")..","
  85.         ..self:get("bottom")..","..self:get("left").."]"
  86. end
Advertisement
Add Comment
Please, Sign In to add comment