Kingdaro

arealib

Aug 5th, 2013
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local instances = {}
  2. local target = term
  3.  
  4. local area = {
  5.     x = 1;
  6.     y = 1;
  7.     width = 1;
  8.     height = 1;
  9.     color = colors.white;
  10.     global = false;
  11. }
  12.  
  13. function area:init()
  14.     self.attached = {}
  15. end
  16.  
  17. -- setting callbacks
  18. function area:draw() end
  19. function area:clicked(x, y, rx, ry) end
  20. function area:dragged(x, y, rx, ry) end
  21.  
  22. -- edge positioning
  23. function area:left(left)
  24.     return self:topLeft(left, self.y)
  25. end
  26.  
  27. function area:top(top)
  28.     return self:topLeft(self.x, top)
  29. end
  30.  
  31. function area:right(right, anchored)
  32.     if anchored == true then
  33.         local w,h = target.getSize()
  34.         right = w - right + 1
  35.     end
  36.     return self:left(right - self.width + 1)
  37. end
  38.  
  39. function area:bottom(bottom, anchored)
  40.     if anchored == true then
  41.         local w,h = target.getSize()
  42.         bottom = h - bottom + 1
  43.     end
  44.     return self:top(bottom - self.height + 1)
  45. end
  46.  
  47. -- corner positioning
  48. function area:topLeft(x, y)
  49.     for i=1, #self.attached do
  50.         local v = self.attached[i]
  51.         local xdiff = v.x - self.x
  52.         local ydiff = v.y - self.y
  53.         v:topLeft(x + xdiff, y + ydiff)
  54.     end
  55.     self.x = x
  56.     self.y = y
  57.     return self
  58. end
  59.  
  60. function area:topRight(x, y, anchored)
  61.     self:right(x, anchored)
  62.     return self:top(y)
  63. end
  64.  
  65. function area:bottomLeft(x, y, anchored)
  66.     self:left(x)
  67.     return self:bottom(y, anchored)
  68. end
  69.  
  70. function area:bottomRight(x, y, anchored)
  71.     self:right(x, anchored)
  72.     return self:bottom(y, anchored)
  73. end
  74.  
  75. -- centering
  76. function area:centerx(x)
  77.     local w,h = target.getSize()
  78.     x = x or w/2
  79.     return self:left(x - self.width/2 + 1)
  80. end
  81.  
  82. function area:centery(y)
  83.     local w,h = target.getSize()
  84.     y = y or h/2
  85.     return self:top(y - self.height/2 + 1)
  86. end
  87.  
  88. function area:center(x, y)
  89.     self:centerx(x)
  90.     return self:centery(y)
  91. end
  92.  
  93. -- edge midpoint positioning
  94. function area:midLeft(x, y)
  95.     self:left(x)
  96.     return self:centery(y)
  97. end
  98.  
  99. function area:midTop(x, y)
  100.     self:centerx(x)
  101.     return self:top(y)
  102. end
  103.  
  104. function area:midRight(x, y, anchored)
  105.     self:right(x, anchored)
  106.     return self:centery(y)
  107. end
  108.  
  109. function area:midBottom(x, y, anchored)
  110.     self:centerx(x)
  111.     return self:bottom(y, anchored)
  112. end
  113.  
  114. -- absolute edge sizing
  115. function area:sizeLeft(x)
  116.     local right = self.x + self.width - 1
  117.     self:left(x)
  118.     self.width = right - self.x + 1
  119.     return self
  120. end
  121.  
  122. function area:sizeTop(y)
  123.     local bottom = self.y + self.height - 1
  124.     self:top(y)
  125.     self.height = bottom - self.x + 1
  126.     return self
  127. end
  128.  
  129. function area:sizeRight(x, anchored)
  130.     if anchored == true then
  131.         local w,h = target.getSize()
  132.         x = w - x + 1
  133.     end
  134.     self.width = x - self.x + 1
  135.     return self
  136. end
  137.  
  138. function area:sizeBottom(y, anchored)
  139.     if anchored == true then
  140.         local w,h = target.getSize()
  141.         y = h - y + 1
  142.     end
  143.     self.height = y - self.y + 1
  144.     return self
  145. end
  146.  
  147. -- absolute corner sizing
  148. function area:sizeTopLeft(x, y)
  149.     self:sizeLeft(x)
  150.     return self:sizeTop(y)
  151. end
  152.  
  153. function area:sizeTopRight(x, y, anchored)
  154.     self:sizeRight(x, anchored)
  155.     return self:sizeTop(y)
  156. end
  157.  
  158. function area:sizeBottomLeft(x, y, anchored)
  159.     self:sizeLeft(x)
  160.     return self:sizeBottom(y, anchored)
  161. end
  162.  
  163. function area:sizeBottomRight(x, y, anchored)
  164.     self:sizeRight(x, anchored)
  165.     return self:sizeBottom(y, anchored)
  166. end
  167.  
  168. -- drawing
  169. function area:fill()
  170.     local line = string.rep(' ', self.width)
  171.     target.setBackgroundColor(self.color)
  172.     for y=self.y, self.y + self.height - 1 do
  173.         target.setCursorPos(self.x, y)
  174.         target.write(line)
  175.     end
  176.     return self
  177. end
  178.  
  179. function area:goToFront()
  180.     for i=1, #instances do
  181.         local v = instances[i]
  182.         if v == self then
  183.             --[[
  184.             for i=1, #self.attached do
  185.                 local v = self.attached[i]
  186.                 v:goToFront()
  187.             end
  188.             ]]
  189.             table.insert(instances, table.remove(instances, i))
  190.             break
  191.         end
  192.     end
  193. end
  194.  
  195. -- attachment
  196. function area:attachTo(other)
  197.     assert(other and other.attached, "Attached object is not an area")
  198.     table.insert(other.attached, self)
  199.     return self
  200. end
  201.  
  202. -- bound access and checking
  203. function area:bounds()
  204.     return self.x, self.y, self.x + self.width - 1, self.y + self.height - 1
  205. end
  206.  
  207. function area:contains(x, y)
  208.     local left, top, right, bottom = self:bounds()
  209.     return x >= left and x <= right and y >= top and y <= bottom
  210. end
  211.  
  212. -- api functions
  213. function new(width, height, color, global)
  214.     local instance = setmetatable(
  215.         { width = width, height = height, color = color, global = global },
  216.         { __index = area })
  217.  
  218.     instance:init()
  219.     table.insert(instances, instance)
  220.     return instance
  221. end
  222.  
  223. function drawAll()
  224.     for i=1, #instances do
  225.         local v = instances[i]
  226.         v:fill()
  227.         v:draw()
  228.     end
  229. end
  230.  
  231. function clickEvent(x, y, global)
  232.     for i=1, #instances do
  233.         local v = instances[i]
  234.         if global == true or v.global == true or v:contains(x, y) then
  235.             v:clicked(x, y, x - v.x, y - v.y)
  236.         end
  237.     end
  238. end
  239.  
  240. function dragEvent(x, y, global)
  241.     for i=1, #instances do
  242.         local v = instances[i]
  243.         if global == true or v.global == true or v:contains(x, y) then
  244.             v:dragged(x, y, x - v.x, y - v.y)
  245.         end
  246.     end
  247. end
  248.  
  249. function setTarget(newTarget)
  250.     assert(newTarget, "arealib.setTarget: No target given")
  251.     target = newTarget
  252.     return target
  253. end
RAW Paste Data