Advertisement
Deltachi

barAPI

Aug 22nd, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. --local mon = peripheral.wrap("top")
  2. --mon.setTextScale(1)
  3. --mon.setTextColor(colors.white)
  4. local bar={}
  5. --mon.setBackgroundColor(colors.black)
  6.  
  7. function clearTable()
  8. bar = {}
  9. end
  10.  
  11. function newBar(name, id, type, text, func, xmin, xmax, ymin, ymax)
  12. bar[name] = {}
  13. bar[name]["id"] = id
  14. bar[name]["type"] = type
  15. bar[name]["text"] = text
  16. bar[name]["value"] = value
  17. bar[name]["func"] = func
  18. bar[name]["color"] = colors.green
  19. bar[name]["xmin"] = xmin
  20. bar[name]["ymin"] = ymin
  21. bar[name]["xmax"] = xmax
  22. bar[name]["ymax"] = ymax
  23. end
  24.  
  25.  
  26. function fill(text, color, bData)
  27. mon.setBackgroundColor(colors.gray)
  28. local yColor = (bData["ymax"]-bData["ymin"])*(1-bData["value"]/100)+bData["ymin"]
  29. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  30. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  31. for j = bData["ymin"], bData["ymax"] do
  32. mon.setCursorPos(bData["xmin"], j)
  33. if j >= yColor then mon.setBackgroundColor(color) end
  34. if j == yspot then
  35. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  36. if k == xspot then
  37. mon.write(text)
  38. else
  39. mon.write(" ")
  40. end
  41. end
  42. else
  43. for i = bData["xmin"], bData["xmax"] do
  44. mon.write(" ")
  45. end
  46. end
  47. end
  48. mon.setBackgroundColor(colors.black)
  49. end
  50.  
  51. function screen()
  52. local currColor
  53. for name,data in pairs(bar) do
  54. local currColor = data["color"]
  55. fill(data["text"], currColor, data)
  56. end
  57. end
  58.  
  59. function setValue(name,val)
  60. bar[name]["value"] = val
  61. local currColor = colors.red
  62. if val >= 95 then
  63. colors.blue
  64. elseif val >= 75 then
  65. colors.green
  66. elseif val >= 50 then
  67. colors.lime
  68. elseif val >= 25 then
  69. colors.yellow
  70. elseif val >= 5 then
  71. colors.orange
  72. else
  73. colors.red
  74. end
  75. screen()
  76. end
  77.  
  78. function checkxy(x, y)
  79. for name, data in pairs(bar) do
  80. if y>=data["ymin"] and y <= data["ymax"] then
  81. if x>=data["xmin"] and x<= data["xmax"] then
  82. data["func"]()
  83. return true
  84. --data["active"] = not data["active"]
  85. --print(name)
  86. end
  87. end
  88. end
  89. return false
  90. end
  91.  
  92. function heading(text)
  93. w, h = mon.getSize()
  94. mon.setCursorPos((w-string.len(text))/2+1, 1)
  95. mon.write(text)
  96. end
  97.  
  98. function label(w, h, text)
  99. mon.setCursorPos(w, h)
  100. mon.write(text)
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement