Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local instances = {}
- local target = term
- local area = {
- x = 1;
- y = 1;
- width = 1;
- height = 1;
- color = colors.white;
- global = false;
- }
- function area:init()
- self.attached = {}
- end
- -- setting callbacks
- function area:draw() end
- function area:clicked(x, y, rx, ry) end
- function area:dragged(x, y, rx, ry) end
- -- edge positioning
- function area:left(left)
- return self:topLeft(left, self.y)
- end
- function area:top(top)
- return self:topLeft(self.x, top)
- end
- function area:right(right, anchored)
- if anchored == true then
- local w,h = target.getSize()
- right = w - right + 1
- end
- return self:left(right - self.width + 1)
- end
- function area:bottom(bottom, anchored)
- if anchored == true then
- local w,h = target.getSize()
- bottom = h - bottom + 1
- end
- return self:top(bottom - self.height + 1)
- end
- -- corner positioning
- function area:topLeft(x, y)
- for i=1, #self.attached do
- local v = self.attached[i]
- local xdiff = v.x - self.x
- local ydiff = v.y - self.y
- v:topLeft(x + xdiff, y + ydiff)
- end
- self.x = x
- self.y = y
- return self
- end
- function area:topRight(x, y, anchored)
- self:right(x, anchored)
- return self:top(y)
- end
- function area:bottomLeft(x, y, anchored)
- self:left(x)
- return self:bottom(y, anchored)
- end
- function area:bottomRight(x, y, anchored)
- self:right(x, anchored)
- return self:bottom(y, anchored)
- end
- -- centering
- function area:centerx(x)
- local w,h = target.getSize()
- x = x or w/2
- return self:left(x - self.width/2 + 1)
- end
- function area:centery(y)
- local w,h = target.getSize()
- y = y or h/2
- return self:top(y - self.height/2 + 1)
- end
- function area:center(x, y)
- self:centerx(x)
- return self:centery(y)
- end
- -- edge midpoint positioning
- function area:midLeft(x, y)
- self:left(x)
- return self:centery(y)
- end
- function area:midTop(x, y)
- self:centerx(x)
- return self:top(y)
- end
- function area:midRight(x, y, anchored)
- self:right(x, anchored)
- return self:centery(y)
- end
- function area:midBottom(x, y, anchored)
- self:centerx(x)
- return self:bottom(y, anchored)
- end
- -- absolute edge sizing
- function area:sizeLeft(x)
- local right = self.x + self.width - 1
- self:left(x)
- self.width = right - self.x + 1
- return self
- end
- function area:sizeTop(y)
- local bottom = self.y + self.height - 1
- self:top(y)
- self.height = bottom - self.x + 1
- return self
- end
- function area:sizeRight(x, anchored)
- if anchored == true then
- local w,h = target.getSize()
- x = w - x + 1
- end
- self.width = x - self.x + 1
- return self
- end
- function area:sizeBottom(y, anchored)
- if anchored == true then
- local w,h = target.getSize()
- y = h - y + 1
- end
- self.height = y - self.y + 1
- return self
- end
- -- absolute corner sizing
- function area:sizeTopLeft(x, y)
- self:sizeLeft(x)
- return self:sizeTop(y)
- end
- function area:sizeTopRight(x, y, anchored)
- self:sizeRight(x, anchored)
- return self:sizeTop(y)
- end
- function area:sizeBottomLeft(x, y, anchored)
- self:sizeLeft(x)
- return self:sizeBottom(y, anchored)
- end
- function area:sizeBottomRight(x, y, anchored)
- self:sizeRight(x, anchored)
- return self:sizeBottom(y, anchored)
- end
- -- drawing
- function area:fill()
- local line = string.rep(' ', self.width)
- target.setBackgroundColor(self.color)
- for y=self.y, self.y + self.height - 1 do
- target.setCursorPos(self.x, y)
- target.write(line)
- end
- return self
- end
- function area:goToFront()
- for i=1, #instances do
- local v = instances[i]
- if v == self then
- --[[
- for i=1, #self.attached do
- local v = self.attached[i]
- v:goToFront()
- end
- ]]
- table.insert(instances, table.remove(instances, i))
- break
- end
- end
- end
- -- attachment
- function area:attachTo(other)
- assert(other and other.attached, "Attached object is not an area")
- table.insert(other.attached, self)
- return self
- end
- -- bound access and checking
- function area:bounds()
- return self.x, self.y, self.x + self.width - 1, self.y + self.height - 1
- end
- function area:contains(x, y)
- local left, top, right, bottom = self:bounds()
- return x >= left and x <= right and y >= top and y <= bottom
- end
- -- api functions
- function new(width, height, color, global)
- local instance = setmetatable(
- { width = width, height = height, color = color, global = global },
- { __index = area })
- instance:init()
- table.insert(instances, instance)
- return instance
- end
- function drawAll()
- for i=1, #instances do
- local v = instances[i]
- v:fill()
- v:draw()
- end
- end
- function clickEvent(x, y, global)
- for i=1, #instances do
- local v = instances[i]
- if global == true or v.global == true or v:contains(x, y) then
- v:clicked(x, y, x - v.x, y - v.y)
- end
- end
- end
- function dragEvent(x, y, global)
- for i=1, #instances do
- local v = instances[i]
- if global == true or v.global == true or v:contains(x, y) then
- v:dragged(x, y, x - v.x, y - v.y)
- end
- end
- end
- function setTarget(newTarget)
- assert(newTarget, "arealib.setTarget: No target given")
- target = newTarget
- return target
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement