Advertisement
Guest User

graphAPI

a guest
Aug 8th, 2015
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. graphs = {}
  2. mon = term.current()
  3.  
  4. function addGraph(name, x, y, width, height, color, value, maxValue)
  5.   horizontal = false
  6.   drawP = true
  7.   drawOutline = true
  8.  
  9.   graphs[name] = {
  10.     name = name,
  11.     x = x,
  12.     y = y,
  13.     width = width,
  14.     height = height,
  15.     color = color,
  16.     value = value,
  17.     maxValue = maxValue,
  18.     horizontal = horizontal,
  19.     drawP = drawP,
  20.     drawOutline = drawOutline
  21.   }
  22. end
  23.  
  24. function updateValue(name, value)
  25.   maxValue = graphs[name].maxValue
  26.  
  27.   if value > maxValue then
  28.     error("Value is greater than max value")
  29.   end
  30.  
  31.   graphs[name].value = value
  32. end
  33.  
  34. function updateMaxValue(name, maxValue)
  35.   if value > maxValue then
  36.     value = 0
  37.   end
  38.  
  39.   graphs[name]["maxValue"] = maxValue
  40. end
  41.  
  42. function updateDrawProgress(name, drawP)
  43.   graphs[name].drawP = drawP
  44. end
  45.  
  46. function setHorizontal(name, horizontal)
  47.   graphs[name].horizontal = horizontal
  48. end
  49.  
  50. function setShowText(name, showText)
  51.   graphs[name].drawP = showText
  52. end
  53.  
  54. function setShowOutline(name, showOutline)
  55.   graphs[name].drawOutline = showOutline
  56. end
  57.  
  58. function updateColor(name, color)
  59.   graphs[name].color = color
  60. end
  61.  
  62. function setMonitor(monitor)
  63.   mon = monitor
  64. end
  65.  
  66. function draw()
  67.   term.redirect(mon)
  68.   isColor = term.isColor()
  69.  
  70.   if not isColor then
  71.     error("Color is not supported on this monitor. Cannot continue!")
  72.   end
  73.  
  74.   term.setBackgroundColor(colors.black)
  75.  
  76.   term.clear()
  77.   for name,graph in pairs(graphs) do
  78.     x = graph["x"]
  79.     y = graph["y"]
  80.    
  81.     width = graph["width"]
  82.     height = graph["height"]
  83.    
  84.     endX = x + width
  85.     endY = y + height
  86.    
  87.     color = graph["color"]
  88.     value = graph["value"]
  89.     maxValue = graph["maxValue"]
  90.     horizontal = graph["horizontal"]
  91.     drawP = graph["drawP"]
  92.     drawOutline = graph["drawOutline"]
  93.    
  94.     offset = (height / maxValue) * (maxValue - value)
  95.    
  96.     if horizontal == true then
  97.       offset = (width / maxValue) * (maxValue - value)
  98.       paintutils.drawFilledBox(x, y, endX - offset, endY, color)
  99.     else
  100.       paintutils.drawFilledBox(x, y + offset, endX, endY, color)
  101.     end
  102.    
  103.     if drawOutline == true then
  104.       paintutils.drawBox(x, y, endX, endY, color)
  105.     end
  106.      
  107.     text = ((value / maxValue) * 100) .. "%"
  108.    
  109.     if drawP == true then
  110.       textX = x + ((width / 2) - (text:len() / 2))
  111.       textY = y + (height / 2) + (offset / 2)
  112.      
  113.       if horizontal then
  114.         textX = x + ((width / 2) - (text:len() / 2))
  115.         textY = y + (height / 2)
  116.       end
  117.      
  118.       term.setCursorPos(textX, textY)
  119.       term.write(text)
  120.     end
  121.   end
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement