Advertisement
fames

monitor

Dec 22nd, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1. MonitorObj = {}
  2.  
  3. function MonitorObj:new(periph, scale)
  4.     local obj = obj or {}
  5.     setmetatable(obj, {__index = self})
  6.  
  7.     obj.mon = peripheral.find("monitor") or periph -- if nothing is put in, find nearest monitor
  8.     obj.w, obj.h = obj.mon.getSize()
  9.  
  10.     obj.scale = scale or 1
  11.  
  12.     obj.mon.setTextScale(obj.scale)
  13.  
  14.     return obj
  15. end
  16.  
  17. function MonitorObj:clear()
  18.     local old = term.redirect(self.mon)
  19.     self.mon.setCursorPos(1,1)
  20.     self.mon.setTextScale(self.scale)
  21.     self.mon.setTextColor(colors.white)
  22.     self.mon.setBackgroundColor(colors.black)
  23.     self.mon.clear()
  24.     term.redirect(old)
  25. end
  26.  
  27. function MonitorObj:write(txt, x, y, cBg, cTxt)
  28.     local c1 = cBg or colors.black
  29.     local c2 = cTxt or colors.white
  30.  
  31.     local old = term.redirect(self.mon)
  32.     local oldTC = self.mon.getTextColor()       -- old text color
  33.     local oldBC = self.mon.getBackgroundColor() -- old background color
  34.  
  35.     self.mon.setCursorPos(x,y)
  36.     self.mon.setBackgroundColor(c1)
  37.     self.mon.setTextColor(c2)
  38.     self.mon.write(txt)
  39.  
  40.     self.mon.setTextColor(oldTC)
  41.     self.mon.setBackgroundColor(oldBC)
  42.     term.redirect(old)
  43. end
  44.  
  45. function MonitorObj:getMonitor()
  46.     return self.mon
  47. end
  48.  
  49. function MonitorObj:setMonitor(periph)
  50.     self:clear()
  51.     self.mon = periph
  52.     self:updateSize()
  53. end
  54.  
  55. function MonitorObj:getSize()
  56.     self:updateSize()
  57.     return self.w, self.h
  58. end
  59.  
  60. function MonitorObj:updateSize()
  61.     self.mon.setTextScale(self.scale)
  62.     local x,y = self.mon.getSize()
  63.     self.w = x
  64.     self.h = y
  65. end
  66.  
  67. function MonitorObj:setScale(scale)
  68.     self.scale = scale or 1
  69.     self:updateSize()
  70. end
  71.  
  72. function MonitorObj:getScale()
  73.     return self.scale
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement