fames

gauge

Dec 22nd, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. GaugeObj = {}
  2. require("lib")
  3.  
  4. function GaugeObj:new(screen, x, y, w, h, min, max, unit, cBg, cGauge, cMarks)
  5.     local obj = obj or {}
  6.     setmetatable(obj, {__index = self})
  7.  
  8.     obj.screen = screen
  9.     obj.monitor = screen:getMonitor()
  10.  
  11.     obj.x = x or 1  
  12.     obj.y = y or 1
  13.     obj.w = w or 1
  14.     obj.h = h or 1
  15.  
  16.     obj.min = min or 0
  17.     obj.max = max or 100
  18.  
  19.     obj.cBg = cBg or colors.black
  20.     obj.cGauge = cGauge or colors.green
  21.     obj.cMarks = cMarks or colors.cyan
  22.  
  23.     obj.level = 0.0
  24.     obj.setpoint = nil
  25.     obj.unit = unit or ""
  26.     obj.markCount = 4
  27.  
  28.     obj.parent = nil
  29.     return obj
  30. end
  31.  
  32. function GaugeObj:draw()
  33.     local old = term.redirect(self.monitor)
  34.     local oldTC = self.monitor.getTextColor()
  35.     local oldBC = self.monitor.getBackgroundColor()
  36.  
  37.     local sX, sY = self.parent:getStartPos()
  38.     local eX, eY = self.parent:getEndPos()
  39.  
  40.     local newX, newY, newW, newH
  41.     if self.parent then
  42.         newX = (self.x + sX) - 1
  43.         newY = (self.y + sY) - 1
  44.         newW = (newX + self.w) - 2
  45.         newH = (newY + self.h) - 2
  46.     else
  47.         newX = self.x
  48.         newY = self.y
  49.         newW = self.w + self.x
  50.         newH = self.h + self.y
  51.     end
  52.     local perc = (self.level/self.max)
  53.     --local perc = 0.95
  54.     local height = math.floor(self.h * perc)
  55.  
  56.  
  57.  
  58.     -- background
  59.     if self.level <= self.min or height == 0 then
  60.         paintutils.drawFilledBox(newX, newY, newW, newH, self.cBg)
  61.     else
  62.         paintutils.drawFilledBox(newX, newY, newW, newH, self.cGauge)
  63.     end
  64.  
  65.     self.monitor.setCursorPos(1,1)
  66.     --print(height.." "..perc.." "..self.h)
  67.  
  68.     -- bar
  69.     if height > 0 and height <= self.h then
  70.         local h2 = math.clamp(self.h - height, newY, self.h+newY)
  71.         paintutils.drawFilledBox(newX, newY, newW, h2, self.cBg)
  72.     end
  73.  
  74.     -- marks
  75.     local i = 1
  76.     for i = 1, self.markCount, 1 do
  77.         local perc = (i/self.markCount)
  78.         local pixY = math.floor(self.h * perc)+self.y
  79.         paintutils.drawPixel(newX-1, pixY, self.cMarks)
  80.         self.screen:write(round(self.max-(perc*self.max))..self.unit, sX, pixY)
  81.     end
  82.     self.screen:write(self.max..self.unit, sX, sY)
  83.     paintutils.drawPixel(newX-1, sY, self.cMarks)
  84.  
  85.     -- setpoint dot
  86.     if self.setpoint then
  87.         local perc = math.clamp(self.setpoint/self.max, 0, 1)
  88.         local pixY = math.floor(math.clamp(self.h - (self.h * perc) + self.y, sY, eY))
  89.         paintutils.drawPixel(newX-1, pixY, colors.lime)
  90.     end
  91.  
  92.     self.monitor.setTextColor(oldTC)
  93.     self.monitor.setBackgroundColor(oldBC)
  94.     term.redirect(old)
  95. end
  96.  
  97. function GaugeObj:setParent(obj)
  98.     local add = function()
  99.         self.parent:addObject(self)
  100.     end
  101.  
  102.     self.parent = obj
  103.     pcall(add)
  104. end
  105.  
  106. function GaugeObj:setLevel(x)
  107.     self.level = x or 0
  108. end
  109. function GaugeObj:setSetpoint(x)
  110.     self.setpoint = x
  111. end
  112.  
  113. function GaugeObj:getParent()
  114.     return self.parent
  115. end
  116.  
  117. function GaugeObj:setMarkCount(x)
  118.     self.markCount = x
  119. end
  120.  
  121. function GaugeObj:setColorBackground(c)
  122.     self.cBg = c or colors.black
  123. end
  124.  
  125. function GaugeObj:setColorGauge(c)
  126.     self.cGauge = c or colors.green
  127. end
  128.  
  129. function GaugeObj:setColorMarks(c)
  130.     self.cMarks = c or colors.cyan
  131. end
  132.  
  133. function GaugeObj:setX(x)
  134.     self.x = x
  135. end
  136.  
  137. function GaugeObj:getX()
  138.     return self.x
  139. end
  140.  
  141. function GaugeObj:setY(y)
  142.     self.y = y
  143. end
  144.  
  145. function GaugeObj:getY()
  146.     return self.y
  147. end
  148.  
  149. function GaugeObj:setW(w)
  150.     self.w = w
  151. end
  152.  
  153. function GaugeObj:getW()
  154.     return self.w
  155. end
  156.  
  157. function GaugeObj:setH(h)
  158.     self.h = h
  159. end
  160.  
  161. function GaugeObj:getH()
  162.     return self.h
  163. end
Advertisement
Add Comment
Please, Sign In to add comment