Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GaugeObj = {}
- require("lib")
- function GaugeObj:new(screen, x, y, w, h, min, max, unit, cBg, cGauge, cMarks)
- local obj = obj or {}
- setmetatable(obj, {__index = self})
- obj.screen = screen
- obj.monitor = screen:getMonitor()
- obj.x = x or 1
- obj.y = y or 1
- obj.w = w or 1
- obj.h = h or 1
- obj.min = min or 0
- obj.max = max or 100
- obj.cBg = cBg or colors.black
- obj.cGauge = cGauge or colors.green
- obj.cMarks = cMarks or colors.cyan
- obj.level = 0.0
- obj.setpoint = nil
- obj.unit = unit or ""
- obj.markCount = 4
- obj.parent = nil
- return obj
- end
- function GaugeObj:draw()
- local old = term.redirect(self.monitor)
- local oldTC = self.monitor.getTextColor()
- local oldBC = self.monitor.getBackgroundColor()
- local sX, sY = self.parent:getStartPos()
- local eX, eY = self.parent:getEndPos()
- local newX, newY, newW, newH
- if self.parent then
- newX = (self.x + sX) - 1
- newY = (self.y + sY) - 1
- newW = (newX + self.w) - 2
- newH = (newY + self.h) - 2
- else
- newX = self.x
- newY = self.y
- newW = self.w + self.x
- newH = self.h + self.y
- end
- local perc = (self.level/self.max)
- --local perc = 0.95
- local height = math.floor(self.h * perc)
- -- background
- if self.level <= self.min or height == 0 then
- paintutils.drawFilledBox(newX, newY, newW, newH, self.cBg)
- else
- paintutils.drawFilledBox(newX, newY, newW, newH, self.cGauge)
- end
- self.monitor.setCursorPos(1,1)
- --print(height.." "..perc.." "..self.h)
- -- bar
- if height > 0 and height <= self.h then
- local h2 = math.clamp(self.h - height, newY, self.h+newY)
- paintutils.drawFilledBox(newX, newY, newW, h2, self.cBg)
- end
- -- marks
- local i = 1
- for i = 1, self.markCount, 1 do
- local perc = (i/self.markCount)
- local pixY = math.floor(self.h * perc)+self.y
- paintutils.drawPixel(newX-1, pixY, self.cMarks)
- self.screen:write(round(self.max-(perc*self.max))..self.unit, sX, pixY)
- end
- self.screen:write(self.max..self.unit, sX, sY)
- paintutils.drawPixel(newX-1, sY, self.cMarks)
- -- setpoint dot
- if self.setpoint then
- local perc = math.clamp(self.setpoint/self.max, 0, 1)
- local pixY = math.floor(math.clamp(self.h - (self.h * perc) + self.y, sY, eY))
- paintutils.drawPixel(newX-1, pixY, colors.lime)
- end
- self.monitor.setTextColor(oldTC)
- self.monitor.setBackgroundColor(oldBC)
- term.redirect(old)
- end
- function GaugeObj:setParent(obj)
- local add = function()
- self.parent:addObject(self)
- end
- self.parent = obj
- pcall(add)
- end
- function GaugeObj:setLevel(x)
- self.level = x or 0
- end
- function GaugeObj:setSetpoint(x)
- self.setpoint = x
- end
- function GaugeObj:getParent()
- return self.parent
- end
- function GaugeObj:setMarkCount(x)
- self.markCount = x
- end
- function GaugeObj:setColorBackground(c)
- self.cBg = c or colors.black
- end
- function GaugeObj:setColorGauge(c)
- self.cGauge = c or colors.green
- end
- function GaugeObj:setColorMarks(c)
- self.cMarks = c or colors.cyan
- end
- function GaugeObj:setX(x)
- self.x = x
- end
- function GaugeObj:getX()
- return self.x
- end
- function GaugeObj:setY(y)
- self.y = y
- end
- function GaugeObj:getY()
- return self.y
- end
- function GaugeObj:setW(w)
- self.w = w
- end
- function GaugeObj:getW()
- return self.w
- end
- function GaugeObj:setH(h)
- self.h = h
- end
- function GaugeObj:getH()
- return self.h
- end
Advertisement
Add Comment
Please, Sign In to add comment