Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WindowObj = {}
- function WindowObj:new(monitor, x, y, w, h, name, cBorder, cText, cTextBg, cBg)
- local obj = obj or {}
- setmetatable(obj, {__index = self})
- obj.mon = monitor or nil
- obj.x = x or 1 -- starting pos x
- obj.y = y or 1 -- starting pos y
- obj.w = w or 1 -- total width x
- obj.h = h or 1 -- total height y
- obj.name = name
- obj.cBorder = cBorder or colors.cyan
- obj.cText = cText or colors.white
- obj.cTextBg = cTextBg or colors.black
- obj.cBg = cBg or colors.black
- obj.texts = {}
- obj.thickness = 1
- obj.extObj = {}
- obj.parent = nil
- return obj
- end
- function WindowObj:write(txt, x, y, cBg, cTxt)
- local old = term.redirect(self.mon)
- local oldTC = self.mon.getTextColor()
- local oldBC = self.mon.getBackgroundColor()
- cBg = self.cBg or cBg
- cTxt = self.cText or cTxt
- local newX, newY
- local oX, oY
- if self.parent then
- oX, oY = self.parent:getStartPos()
- newX = self.x + oX + x
- newY = self.y + oY + y
- else
- newX = self.x + x
- newY = self.y + y
- end
- self.mon.setCursorPos(newX, newY)
- self.mon.setBackgroundColor(cBg)
- self.mon.setTextColor(cTxt)
- self.mon.write(txt)
- self.mon.setTextColor(oldTC)
- self.mon.setBackgroundColor(oldBC)
- term.redirect(old)
- end
- function WindowObj:draw()
- local old = term.redirect(self.mon)
- local oldTC = self.mon.getTextColor() -- old text color
- local oldBC = self.mon.getBackgroundColor() -- old background color
- local newX, newY
- local oX, oY
- if self.parent then
- oX, oY = self.parent:getStartPos()
- newX = self.x + oX
- newY = self.y + oY
- else
- newX = self.x
- newY = self.y
- end
- paintutils.drawBox(newX, newY, self.w + newX, self.h + newY, self.cBorder)
- paintutils.drawFilledBox(newX+1, newY+1, self.w + newX -1, (self.h + newY)-1, self.cBg)
- self.mon.setTextColor(oldTC)
- self.mon.setBackgroundColor(oldBC)
- term.redirect(old)
- if self.name then
- self:write(" "..self.name.." ", 2, 0, self.cTextBg, self.cText)
- end
- if self.extObj then
- self:drawObjects()
- end
- if self.texts then
- self:drawTexts()
- end
- end
- function WindowObj:addText(txt, x, y, cBg, cTxt)
- local j = {}
- table.insert(j, txt)
- table.insert(j, x or 1)
- table.insert(j, y or 1)
- table.insert(j, cBg or self.cBg)
- table.insert(j, cTxt or self.cText)
- table.insert(self.texts, j)
- end
- function WindowObj:drawTexts()
- local x = table.getn(self.texts)
- local y = self.texts
- for i = 1, x, 1 do
- self:write(y[i][1], y[i][2], y[i][3], y[i][4], y[i][5])
- end
- end
- function WindowObj:drawObjects()
- local i = 0
- for i = 1, table.getn(self.extObj), 1 do
- self.extObj[i]:draw()
- end
- end
- function WindowObj:getObjects()
- return self.extObj
- end
- function WindowObj:addObject(o)
- table.insert(self.extObj, o)
- end
- function WindowObj:setParent(o)
- local add = function()
- self.parent:addObject(self)
- end
- self.parent = o
- if not pcall(add) then error("Error setting parent") end
- end
- function WindowObj:getParent()
- return self.parent
- end
- function WindowObj:setMonitor(monitor)
- self.mon = monitor
- end
- function WindowObj:getMonitor()
- return self.mon
- end
- function WindowObj:setX(x)
- self.x = x
- end
- function WindowObj:getX()
- if self.parent then
- return self.x + self.parent:getX()
- else
- return self.x
- end
- end
- function WindowObj:setY(y)
- self.y = y
- end
- function WindowObj:getY()
- if self.parent then
- return self.y + self.parent:gety()
- else
- return self.y
- end
- end
- function WindowObj:setW(w)
- self.w = w
- end
- function WindowObj:getW()
- return self.w
- end
- function WindowObj:setH(h)
- self.h = h
- end
- function WindowObj:getH()
- return self.h
- end
- function WindowObj:setName(name)
- self.name = name
- end
- function WindowObj:getName()
- return self.name
- end
- function WindowObj:getStartPos()
- -- return starting positions for other objects to draw onto the window (X, Y)
- local xs, xy
- if self.parent then
- xs = self.x + self.thickness + self.parent:getX()
- ys = self.y + self.thickness + self.parent:getY()
- else
- xs = self.x + self.thickness
- ys = self.y + self.thickness
- end
- return xs,ys
- end
- function WindowObj:getEndPos()
- -- return end positions for other objects to draw onto the window
- local xe, ye
- if self.parent then
- xe = self.w + self.x - self.thickness + self.parent:getX()
- ye = self.h + self.y - self.thickness + self.parent:getY()
- else
- xe = self.w + self.x - self.thickness
- ye = self.h + self.y - self.thickness
- end
- return xe,ye
- end
- function WindowObj:setColorBorder(color)
- self.cBorder = color
- end
- function WindowObj:setColorText(color)
- self.cText = color
- end
- function WindowObj:setColorTextBackground(color)
- self.cTextBg = color
- end
- function WindowObj:setColorBackground(color)
- self.cBg = color
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement