Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2.  
  3. if not mon then
  4. print("Could not find a Monitor")
  5. end
  6.  
  7. mon.setTextScale(1)
  8. mon.setTextColor(colors.white)
  9. mon.setBackgroundColor(colors.black)
  10.  
  11.  
  12. buttons = {}
  13. charts = {}
  14. labels = {}
  15.  
  16. function clearAll()
  17. buttons = {}
  18. charts = {}
  19. labels = {}
  20. mon.clear()
  21. end
  22.  
  23. function addButton(name, text, func, xmin, xmax, ymin, ymax, color, activeColor, bType, activeText)
  24. local color = color or colors.red
  25. local activeColor = activeColor or colors.lime
  26. buttons[name] = {}
  27. buttons[name]["text"] = text
  28. buttons[name]["func"] = func
  29. buttons[name]["active"] = false
  30. buttons[name]["xmin"] = xmin
  31. buttons[name]["xmax"] = xmax
  32. buttons[name]["ymin"] = ymin
  33. buttons[name]["ymax"] = ymax
  34. buttons[name]["color"] = color
  35. buttons[name]["activeColor"] = activeColor
  36. buttons[name]["type"] = bType
  37. buttons[name]["activeText"] = activeText
  38. end
  39.  
  40. function addChart(name, xmin, xmax, ymin, ymax, value, color, label)
  41. local color = color or colors.lime
  42. charts[name] = {}
  43. charts[name]["xmin"] = xmin
  44. charts[name]["xmax"] = xmax
  45. charts[name]["ymin"] = ymin
  46. charts[name]["ymax"] = ymax
  47. charts[name]["value"] = value
  48. charts[name]["color"] = color
  49. charts[name]["label"] = label
  50. end
  51.  
  52. function addLabel(name, x, y, value, color)
  53. local color = color or colors.white
  54. labels[name] = {}
  55. labels[name]["x"] = x
  56. labels[name]["y"] = y
  57. labels[name]["value"] = value
  58. labels[name]["color"] = color
  59. end
  60.  
  61. function drawButtons()
  62. local currColor
  63.  
  64. for name, data in pairs(buttons) do
  65. local active = data["active"]
  66. if active == true then
  67. currColor = data["activeColor"]
  68. else
  69. currColor = data["color"]
  70. end
  71. drawButton(name, data, currColor)
  72. end
  73. end
  74.  
  75. function drawCharts()
  76. for name, data in pairs(charts) do
  77. drawChart(name, data)
  78. end
  79. end
  80.  
  81. function drawLabels()
  82. for name, data in pairs(labels) do
  83. drawLabel(data["x"], data["y"], data["value"], data["color"])
  84. end
  85. end
  86.  
  87. function drawButton(name, data, currColor)
  88. mon.setBackgroundColor(currColor)
  89.  
  90. local text = data["text"]
  91. if data["active"] == true then
  92. text = data["activeText"]
  93. end
  94.  
  95. local xspot = math.floor((data["xmax"] - data["xmin"] - string.len(text)) / 2) + 1
  96. local yspot = math.floor((data["ymin"] + data["ymax"]) / 2)
  97.  
  98. for i = data["ymin"], data["ymax"] do
  99. mon.setCursorPos(data["xmin"], i)
  100.  
  101. if i == yspot then
  102. for j = 0, data["xmax"] - data["xmin"] - string.len(text) + 1 do
  103. if j == xspot then
  104. mon.write(text)
  105. else
  106. mon.write(" ")
  107. end
  108. end
  109. else
  110. for k = data["xmin"], data["xmax"] do
  111. mon.write(" ")
  112. end
  113. end
  114. end
  115. mon.setBackgroundColor(colors.black)
  116. end
  117.  
  118. function drawChart(name, data)
  119. local h = data["ymax"] - data["ymin"]
  120. local percentagePerStep = math.floor(100 / h)
  121. local barHeight = math.floor(data["value"] / percentagePerStep)
  122.  
  123. local xspot = math.floor((data["xmax"] - data["xmin"]) / 2) - 1
  124. local yspot = math.floor((data["ymin"] + data["ymax"]) / 2)
  125. local text = data["value"] .. "%"
  126.  
  127. if data["label"] ~= nil then
  128. labelX = math.floor((data["xmax"] - data["xmin"] - string.len(data["label"])) / 2)
  129. drawLabel(data["xmin"] + labelX, data["ymax"] + 1, data["label"], colors.white)
  130. end
  131.  
  132. local emptyColor = 0
  133. if data["color"] == colors.gray then
  134. emptyColor = colors.lightGray
  135. else
  136. emptyColor = colors.gray
  137. end
  138.  
  139. if data["color"] == colors.white then
  140. mon.setTextColor(colors.black)
  141. end
  142.  
  143. for i = data["ymin"], data["ymax"] do
  144. mon.setCursorPos(data["xmin"], i)
  145. if i == yspot then
  146. for j = 0, data["xmax"] - data["xmin"] - string.len(text) + 1 do
  147. if barHeight + data["ymax"] >= data["ymax"] + (data["ymax"] - i) then
  148. mon.setBackgroundColor(data["color"])
  149. else
  150. mon.setBackgroundColor(emptyColor)
  151. end
  152.  
  153. if j == xspot then
  154. mon.write(text)
  155. else
  156. mon.write(" ")
  157. end
  158. end
  159. else
  160. for k = data["xmin"], data["xmax"] do
  161. if barHeight + data["ymax"] >= data["ymax"] + (data["ymax"] - i) then
  162. mon.setBackgroundColor(data["color"])
  163. else
  164. mon.setBackgroundColor(emptyColor)
  165. end
  166.  
  167. mon.write(" ")
  168.  
  169. end
  170. end
  171. end
  172. mon.setBackgroundColor(colors.black)
  173. mon.setTextColor(colors.white)
  174. end
  175.  
  176. function paintGUI()
  177. mon.clear()
  178. drawButtons()
  179. drawCharts()
  180. drawLabels()
  181. end
  182.  
  183. function drawLabel(x, y, value, color)
  184. mon.setCursorPos(x, y)
  185. mon.setTextColor(color)
  186. mon.write(value)
  187. end
  188.  
  189. function drawHeading(text)
  190. w, h = mon.getSize()
  191. mon.setCursorPos((w - string.len(text)) / 2 + 1, 1)
  192. mon.write(text)
  193. end
  194.  
  195. function toggleButton(name)
  196. buttons[name]["active"] = not buttons[name]["active"]
  197. print(buttons[name]["active"])
  198. paintGUI()
  199. end
  200.  
  201. function flashButton(name)
  202. toggleButton(name)
  203. sleep(0.15)
  204. toggleButton(name)
  205. end
  206.  
  207. function updateLabel(name, value, color)
  208. labels[name]["value"] = value;
  209. if color ~= nil then
  210. labels[name]["color"] = color
  211. end
  212.  
  213. paintGUI()
  214. end
  215.  
  216. function updateChart(name, value, color)
  217. charts[name]["value"] = value
  218. if color ~= nil then
  219. charts[name]["color"] = color
  220. end
  221.  
  222. paintGUI()
  223. end
  224.  
  225. function checkxy(x, y)
  226. for name, data in pairs(buttons) do
  227. if y >= data["ymin"] and y <= data["ymax"] then
  228. if x >= data["xmin"] and x <= data["xmax"] then
  229. if data["type"] == "button" then
  230. flash(name)
  231. data["func"]()
  232. elseif data["type"] == "switch" then
  233. data["func"]()
  234. toggleButton(name)
  235. else
  236. error("That's not a button type bro.")
  237. end
  238. return true
  239. end
  240. end
  241. end
  242.  
  243. return false
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement