WillaRL_

statisticDisplayAPI for OC

Dec 9th, 2020
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. --By Willa_/WillaRL_
  2.  
  3. local event = require("event")
  4. local component = require("component")
  5. local gpu = component.gpu
  6.  
  7. local statDispArr = {}
  8. statDispArr.__index = statDispArr
  9. setmetatable(statDispArr, {__call = function(cls,...) return cls.new(...) end,})
  10.  
  11. local statisticGraph = {}
  12. statisticGraph.__index = statisticGraph
  13. setmetatable(statisticGraph, {__call = function(cls,...) return cls.new(...) end,})
  14.  
  15. local statisticText = {}
  16. statisticText.__index = statisticText
  17. setmetatable(statisticText, {__call = function(cls,...) return cls.new(...) end,})
  18.  
  19.  
  20. -------------------------
  21. --START CLASS statDispArr
  22. -------------------------
  23.  
  24. function statDispArr.new()
  25.   local self = setmetatable({}, statDispArr)
  26.   self.statsArr = {}
  27.   return self
  28. end
  29.  
  30.  
  31. function statDispArr:addGraph(behaviour, x2, x1, y2, y1, colourBack, colourFront, dataFunc, min, max)
  32.   local newStatDisp = statisticGraph(behaviour, x2, x1, y2, y1, colourBack, colourFront, dataFunc, min, max)
  33.   table.insert(self.statsArr, newStatDisp)
  34. end
  35.  
  36. function statDispArr:addText(x, y, colour, dataFunc)
  37.   local newStatDisp = statisticText(x, y, colour, dataFunc)
  38.   table.insert(self.statsArr, newStatDisp)
  39. end
  40.  
  41.  
  42. function statDispArr:start(resolution)
  43.   while true do
  44.   for i, display in ipairs(self.statsArr) do
  45.     display:draw()
  46.   end
  47.  
  48.   os.sleep(resolution)
  49.   end
  50. end
  51.  
  52. -----------------------
  53. --END CLASS statDispArr
  54. -----------------------
  55.  
  56.  
  57. ----------------------------
  58. --START CLASS statisticGraph
  59. ----------------------------
  60.  
  61. function statisticGraph.new(behaviour, x2, x1, y2, y1, colourBack, colourFront, dataFunc, min, max)
  62.   local self = setmetatable({}, statisticGraph)
  63.  
  64.   self.xMin = x1
  65.   self.yMin = y1
  66.   self.xMax = x2
  67.   self.yMax = y2
  68.   self.max = max
  69.   self.min = min
  70.  
  71.   self.colourBack = colourBack
  72.   self.colourFront = colourFront
  73.  
  74.   self.behaviour = behaviour
  75.   self.dataFunc = dataFunc
  76.   self.scaleFactor = self:__scaleFactor()
  77.  
  78.   return self
  79. end
  80.  
  81.  
  82. function statisticGraph:__scaleFactor()
  83.   if(self.behaviour == 0) then
  84.     return (self.xMax - self.xMin)/(self.max - self.min)
  85.  
  86.   else
  87.     return (self.yMax - self.yMin)/(self.max - self.min)
  88.  
  89.   end
  90.  
  91. end
  92.  
  93. function statisticGraph:draw()
  94.   if(self.behaviour == 0) then
  95.     self:__drawHoriz()
  96.     return true
  97.  
  98.   else
  99.     self:__drawVert()
  100.     return true
  101.   end
  102. end
  103.  
  104.  
  105. function statisticGraph:__drawHoriz()
  106.   local val = self.dataFunc()
  107.   local adjustedVal = math.floor(val*self.scaleFactor)
  108.  
  109.   local oldColour = gpu.setBackground(self.colourBack, false)
  110.   gpu.fill(self.xMin, self.yMin, self.xMax - self.xMin, self.yMax - self.yMin, " ")
  111.  
  112.   gpu.setBackground(self.colourFront, false)
  113.   gpu.fill(self.xMin, self.yMin, adjustedVal, self.yMax - self.yMin, " ")
  114.  
  115.   gpu.setBackground(oldColour)
  116. end
  117.  
  118.  
  119. function statisticGraph:__drawVert()
  120.   local val = self.dataFunc()
  121.   local adjustedVal = math.floor(val*self.scaleFactor)
  122.  
  123.   local oldColour = gpu.setBackground(self.colourFront, false)
  124.  
  125.   gpu.fill(self.xMin, self.yMin, self.xMax - self.xMin, self.yMax - self.yMin, " ")
  126.   gpu.setBackground(self.colourBack, false)
  127.  
  128.   gpu.fill(self.xMin, self.yMin, self.xMax - self.xMin, self.yMax - adjustedVal - self.yMin, " ")
  129.   gpu.setBackground(oldColour)
  130. end
  131.  
  132. --------------------------
  133. --END CLASS statisticGraph
  134. --------------------------
  135.  
  136.  
  137.  
  138. --------------------------
  139. --START CLASS statisticText
  140. --------------------------
  141.  
  142. function statisticText.new(x,y, colour, dataFunc)
  143.   local self = setmetatable({}, statisticText)
  144.   self.x = x
  145.   self.y = y
  146.   self.colour = colour
  147.   self.dataFunc = dataFunc
  148.   return self
  149. end
  150.  
  151.  
  152. function statisticText:draw()
  153.   local oldColour = gpu.setForeground(self.colour)
  154.   local val = self.dataFunc()
  155.  
  156.   gpu.set(self.x, self.y, val)
  157.   gpu.setForeground(oldColour)
  158. end
  159.  
  160. -------------------------
  161. --END CLASS statisticText
  162. -------------------------
  163.  
  164.  
  165.  
  166.  
  167. return statDispArr
Advertisement
Add Comment
Please, Sign In to add comment