Advertisement
fames

window

Dec 22nd, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.93 KB | None | 0 0
  1. WindowObj = {}
  2.  
  3. function WindowObj:new(monitor, x, y, w, h, name, cBorder, cText, cTextBg, cBg)
  4.     local obj = obj or {}
  5.     setmetatable(obj, {__index = self})
  6.  
  7.     obj.mon = monitor or nil
  8.     obj.x = x or 1      -- starting pos x
  9.     obj.y = y or 1      -- starting pos y
  10.     obj.w = w or 1      -- total width x
  11.     obj.h = h or 1      -- total height y
  12.  
  13.     obj.name = name
  14.     obj.cBorder = cBorder or colors.cyan   
  15.     obj.cText = cText or colors.white      
  16.     obj.cTextBg = cTextBg or colors.black  
  17.     obj.cBg = cBg or colors.black
  18.  
  19.     obj.texts = {}
  20.  
  21.     obj.thickness = 1
  22.  
  23.     obj.extObj = {}
  24.     obj.parent = nil
  25.     return obj
  26. end
  27.  
  28. function WindowObj:write(txt, x, y, cBg, cTxt)
  29.     local old = term.redirect(self.mon)
  30.     local oldTC = self.mon.getTextColor()
  31.     local oldBC = self.mon.getBackgroundColor()
  32.  
  33.     cBg = self.cBg or cBg
  34.     cTxt = self.cText or cTxt
  35.  
  36.     local newX, newY
  37.     local oX, oY
  38.     if self.parent then
  39.         oX, oY = self.parent:getStartPos()
  40.         newX = self.x + oX + x
  41.         newY = self.y + oY + y
  42.     else
  43.         newX = self.x + x
  44.         newY = self.y + y
  45.     end
  46.  
  47.     self.mon.setCursorPos(newX, newY)
  48.     self.mon.setBackgroundColor(cBg)
  49.     self.mon.setTextColor(cTxt)
  50.     self.mon.write(txt)
  51.  
  52.     self.mon.setTextColor(oldTC)
  53.     self.mon.setBackgroundColor(oldBC)
  54.     term.redirect(old)
  55. end
  56.  
  57. function WindowObj:draw()
  58.     local old = term.redirect(self.mon)
  59.     local oldTC = self.mon.getTextColor()       -- old text color
  60.     local oldBC = self.mon.getBackgroundColor() -- old background color
  61.  
  62.     local newX, newY
  63.     local oX, oY
  64.     if self.parent then
  65.         oX, oY = self.parent:getStartPos()
  66.         newX = self.x + oX
  67.         newY = self.y + oY
  68.     else
  69.         newX = self.x
  70.         newY = self.y
  71.     end
  72.  
  73.     paintutils.drawBox(newX, newY, self.w + newX, self.h + newY, self.cBorder)
  74.     paintutils.drawFilledBox(newX+1, newY+1, self.w + newX -1, (self.h + newY)-1, self.cBg)
  75.  
  76.     self.mon.setTextColor(oldTC)
  77.     self.mon.setBackgroundColor(oldBC)
  78.     term.redirect(old)
  79.  
  80.     if self.name then
  81.         self:write(" "..self.name.." ", 2, 0, self.cTextBg, self.cText)
  82.     end
  83.  
  84.     if self.extObj then
  85.         self:drawObjects()
  86.     end
  87.  
  88.     if self.texts then
  89.         self:drawTexts()
  90.     end
  91. end
  92.  
  93. function WindowObj:addText(txt, x, y, cBg, cTxt)
  94.     local j = {}
  95.     table.insert(j, txt)
  96.     table.insert(j, x or 1)
  97.     table.insert(j, y or 1)
  98.     table.insert(j, cBg or self.cBg)
  99.     table.insert(j, cTxt or self.cText)
  100.  
  101.     table.insert(self.texts, j)
  102. end
  103.  
  104. function WindowObj:drawTexts()
  105.     local x = table.getn(self.texts)
  106.     local y = self.texts
  107.  
  108.     for i = 1, x, 1 do
  109.         self:write(y[i][1], y[i][2], y[i][3], y[i][4], y[i][5])
  110.     end
  111. end
  112.  
  113. function WindowObj:drawObjects()
  114.     local i = 0
  115.     for i = 1, table.getn(self.extObj), 1 do
  116.         self.extObj[i]:draw()
  117.     end
  118. end
  119.  
  120. function WindowObj:getObjects()
  121.     return self.extObj
  122. end
  123.  
  124. function WindowObj:addObject(o)
  125.     table.insert(self.extObj, o)
  126. end
  127.  
  128. function WindowObj:setParent(o)
  129.     local add = function()
  130.         self.parent:addObject(self)
  131.     end
  132.  
  133.     self.parent = o
  134.     if not pcall(add) then error("Error setting parent") end
  135. end
  136.  
  137. function WindowObj:getParent()
  138.     return self.parent
  139. end
  140.  
  141. function WindowObj:setMonitor(monitor)
  142.     self.mon = monitor
  143. end
  144.  
  145. function WindowObj:getMonitor()
  146.     return self.mon
  147. end
  148.  
  149. function WindowObj:setX(x)
  150.     self.x = x
  151. end
  152.  
  153. function WindowObj:getX()
  154.     if self.parent then
  155.         return self.x + self.parent:getX()
  156.     else
  157.         return self.x
  158.     end
  159. end
  160.  
  161. function WindowObj:setY(y)
  162.     self.y = y
  163. end
  164.  
  165. function WindowObj:getY()
  166.     if self.parent then
  167.         return self.y + self.parent:gety()
  168.     else
  169.         return self.y
  170.     end
  171. end
  172.  
  173. function WindowObj:setW(w)
  174.     self.w = w
  175. end
  176.  
  177. function WindowObj:getW()
  178.     return self.w
  179. end
  180.  
  181. function WindowObj:setH(h)
  182.     self.h = h
  183. end
  184.  
  185. function WindowObj:getH()
  186.     return self.h
  187. end
  188.  
  189. function WindowObj:setName(name)
  190.     self.name = name
  191. end
  192.  
  193. function WindowObj:getName()
  194.     return self.name
  195. end
  196.  
  197. function WindowObj:getStartPos()
  198.     -- return starting positions for other objects to draw onto the window (X, Y)
  199.     local xs, xy
  200.     if self.parent then
  201.         xs = self.x + self.thickness + self.parent:getX()
  202.         ys = self.y + self.thickness + self.parent:getY()
  203.     else
  204.         xs = self.x + self.thickness
  205.         ys = self.y + self.thickness
  206.     end
  207.     return xs,ys
  208. end
  209.  
  210. function WindowObj:getEndPos()
  211.     -- return end positions for other objects to draw onto the window
  212.     local xe, ye
  213.     if self.parent then
  214.         xe = self.w + self.x - self.thickness + self.parent:getX()
  215.         ye = self.h + self.y - self.thickness   + self.parent:getY()
  216.     else
  217.         xe = self.w + self.x - self.thickness
  218.         ye = self.h + self.y - self.thickness
  219.     end
  220.     return xe,ye
  221. end
  222.  
  223. function WindowObj:setColorBorder(color)
  224.     self.cBorder = color
  225. end
  226.  
  227. function WindowObj:setColorText(color)
  228.     self.cText = color
  229. end
  230.  
  231. function WindowObj:setColorTextBackground(color)
  232.     self.cTextBg = color
  233. end
  234.  
  235. function WindowObj:setColorBackground(color)
  236.     self.cBg = color
  237. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement