Advertisement
Guest User

gui

a guest
Apr 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.16 KB | None | 0 0
  1. mon = peripheral.find("monitor")
  2.  
  3. if not mon then
  4.   error("Por favor, adjunta monitor")
  5. end
  6.  
  7. mon.setTextScale(1)
  8. mon.setTextColor(colors.white)
  9. mon.setBackgroundColor(colors.black)
  10. button = {}
  11. charts = {}
  12.  
  13. function clearAll()
  14.   button = {}
  15.   charts = {}
  16.   mon.clear()
  17. end
  18.  
  19. function addButton(nombre, func, xMin, xMax, yMin, yMax, offColor, onColor)
  20.   color = offColor or colors.red
  21.   activeColor = onColor or colors.lime
  22.   button[nombre] = {}
  23.   button[nombre]["func"] = func
  24.   button[nombre]["active"] = false
  25.   button[nombre]["xMin"] = xMin
  26.   button[nombre]["yMin"] = yMin
  27.   button[nombre]["xMax"] = xMax
  28.   button[nombre]["yMax"] = yMax
  29.   button[nombre]["color"] = color
  30.   button[nombre]["activeColor"] = activeColor
  31. end
  32.  
  33. function addChart(nombre, xMin, xMax, yMin, yMax, valor, color, etiqueta)
  34.   charts[nombre] = {}
  35.   charts[nombre]["xMin"] = xMin
  36.   charts[nombre]["xMax"] = xMax
  37.   charts[nombre]["yMin"] = yMin
  38.   charts[nombre]["yMax"] = yMax
  39.   charts[nombre]["valor"] = valor --El valor es en porcentaje
  40.   charts[nombre]["color"] = color
  41.   charts[nombre]["etiqueta"] = etiqueta
  42. end
  43.  
  44. function updateChart(nombre, valor, color)
  45.   charts[nombre]["valor"] = valor
  46.   charts[nombre]["color"] = color
  47.   screenChart()
  48. end
  49.  
  50. function screenButton()
  51.   local currcolor
  52.   for nombre, data in pairs(button)do
  53.     local active = data["active"]
  54.     if active  == true then
  55.       currcolor = data["activeColor"]
  56.     else
  57.       currcolor = data["color"]
  58.     end
  59.     fillButton(nombre, currcolor, data)
  60.   end
  61. end
  62.  
  63. function screenChart()
  64.   for nombre, data in pairs(charts)do
  65.     fillChart(nombre, data)
  66.   end
  67. end
  68.  
  69. function fillButton(text, color, bData)
  70.   mon.setBackgroundColor(color)
  71.   local yspot = math.floor((bData["yMin"] + bData["yMax"])/2)
  72.   local xspot = math.floor((bData["xMax"] - bData["xMin"] - string.len(text)) /2) +1
  73.   for j = bData["yMin"], bData["yMax"] do
  74.     mon.setCursorPos(bData["xMin"], j)
  75.     if j == yspot then
  76.       for k = 0, bData["xMax"] - bData["xMin"] - string.len(text) + 1 do
  77.         if k == xspot then
  78.           mon.write(text)
  79.         else
  80.           mon.write(" ")
  81.         end
  82.       end
  83.     else
  84.       for i = bData["xMin"], bData["xMax"] do
  85.         mon.write(" ")
  86.       end
  87.     end
  88.   end
  89.    mon.setBackgroundColor(colors.black)
  90. end  
  91.  
  92. function fillChart(nombre, cData)
  93.   local h = cData["yMax"] - cData["yMin"]
  94.   local percentPerStep = math.floor(100/h)
  95.   local barHeight = math.floor(cData["valor"]/percentPerStep)
  96.  
  97.   local xspot = math.floor((cData["xMax"] - cData["xMin"]) /2) -1
  98.   local yspot = math.floor((cData["yMin"] + cData["yMax"]) /2)
  99.   local text = cData["valor"] .. "%"
  100.  
  101.   if cData["etiqueta"] ~= nil then
  102.     etiquetaX = math.floor((cData["xMax"] - cData["xMin"] - string.len(cData["etiqueta"])) /2)
  103.     etiqueta(cData["xMin"] + etiquetaX, cData["yMax"] + 1,  cData["etiqueta"])
  104.   end
  105.  
  106.   local emptyColor = 0
  107.   if cData["color"] == colors.gray then
  108.     emptyColor = colors.lightGray
  109.   else
  110.     emptyColor = colors.gray
  111.   end
  112.  
  113.   if cData["color"] == colors.white then
  114.     mon.setTextColor(colors.black)
  115.   end
  116.  
  117.   for j = cData["yMin"], cData["yMax"] do
  118.       mon.setCursorPos(cData["xMin"], j)
  119.       if j == yspot then
  120.         for k = 0, cData["xMax"] - cData["xMin"] - string.len(text) + 1 do
  121.           if barHeight + cData["yMax"] >= cData["yMax"] + (cData["yMax"] - j) then
  122.             mon.setBackgroundColor(cData["color"])
  123.           else
  124.             mon.setBackgroundColor(emptyColor)
  125.           end
  126.           if k == xspot then
  127.             mon.write(text)
  128.           else
  129.             mon.write(" ")
  130.           end
  131.         end
  132.       else
  133.         for i = cData["xMin"], cData["xMax"] do
  134.           if barHeight + cData["yMax"] >= cData["yMax"] + (cData["yMax"] - j)  then
  135.             mon.setBackgroundColor(cData["color"])
  136.           else
  137.             mon.setBackgroundColor(emptyColor)
  138.           end
  139.             mon.write(" ")
  140.          end
  141.       end
  142.    end
  143.    mon.setBackgroundColor(colors.black)
  144.    mon.setTextColor(colors.white)
  145. end
  146.  
  147. function toggleButton(nombre)
  148.   button[nombre]["active"] = not button[nombre]["active"]
  149.   screenButton()
  150. end
  151.  
  152. function flashButton(nombre)
  153.   toggleButton(nombre)
  154.   sleep(0.25)
  155.   toggleButton(nombre)
  156. end
  157.  
  158. function checkXY(x, y)
  159.   for nombre, data in pairs(button) do
  160.     if y>=data["yMin"] and y<= data["yMax"] then
  161.       if x>=data["xMin"] and x<=data["xMax"] then
  162.         data["func"]()
  163.         flashButton(nombre)
  164.         return true
  165.       end
  166.     end
  167.   end
  168.   return false
  169. end
  170.          
  171.  
  172. function addTitulo(text)
  173.   w, h = mon.getSize()
  174.   mon.setCursorPos((w-string.len(text))/2+1, 1)
  175.   mon.write(text)
  176. end
  177.  
  178. function etiqueta(x, y, text)
  179.   mon.setCursorPos(x, y)
  180.   mon.write(text)
  181. end
  182.  
  183. function addEtiqueta(x,y,text,color)
  184.   mon.setCursorPos(x,y)
  185.   mon.setTextColor(color)
  186.   mon.write(text)
  187.   mon.setTextColor(colors.white)
  188. end
  189.  
  190. function startButtonCharts()
  191.   mon.clear()
  192.   screenButton()
  193.   screenChart()
  194. end
  195.  
  196. -- FUNCIONES Prog Princial:
  197.  
  198. --function getClick()
  199. --  event,side,xPos,yPos=os.pullEvent("monitor_touch")
  200. --  checkXY(xPos,yPos)
  201. --end
  202. --
  203. --while true do
  204. --  getClick()
  205. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement