MaTaXeToS

ComputerCraft TouchScreen Password

Jun 20th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.74 KB | None | 0 0
  1. -- ZONA A EDITAR
  2. local _password_ = "5926"                       -- Password del sistema
  3. local _password_length_ = 4                     -- Longitud de la password
  4. local _color_buttons_ = colors.black                -- Color default de los botones
  5. local _color_active_buttons_ = colors.lime      -- Color cuando haces click en el boton
  6. local _color_text = colors.white                -- Color de texto
  7. local _color_background = colors.black          -- Color de fondo
  8. local _redstone_output_ = "bottom"              -- Salida de redstone signal
  9. local _redstone_output_time_ = 5                -- Tiempo de redstone signal
  10. -- FIN DE ZONA A EDITAR
  11.  
  12. -- NO MODIFICAR
  13. -- TODO EL SISTEMA ESTA PARAMETRIZADO
  14. -- GRACIAS
  15.  
  16. -- System Vars
  17. local side = nil
  18.  
  19. for _, s in pairs(rs.getSides()) do
  20.     if peripheral.getType(s) == "monitor" then
  21.         side = s
  22.     end
  23. end
  24. if not side then
  25.   print("No se ha encontrado monitor")
  26.   print("Porfavor, ponga uno.")
  27.   error()
  28. end
  29.  
  30. local button={}
  31. local tmp_password = {""}
  32. local password = {""}
  33. local x
  34. local y
  35. local mon = peripheral.wrap(side)
  36.  
  37. -- Main Function
  38. function main()
  39.     mon.setTextScale(1)
  40.     mon.setTextColor(_color_text)
  41.     mon.setBackgroundColor(_color_background)
  42.  
  43.     if (fs.exists("tasks") == false) then
  44.         writeTask("_clear_")
  45.     end
  46.    
  47.     x, y = mon.getSize()
  48.     fillTable()
  49.     checkTasks()
  50.     while true do
  51.        mon.clear()
  52.        screen()
  53.        local e,side,x,y = os.pullEvent("monitor_touch")
  54.        print(x..":"..y)
  55.        checkxy(x,y)
  56.        sleep(.1)
  57.     end
  58.    
  59. end
  60.  
  61. -- Add Button array
  62. function setTable(name, value, func, xmin, xmax, ymin, ymax)
  63.    button[name] = {}
  64.    button[name]["value"] = value
  65.    button[name]["func"] = func
  66.    button[name]["active"] = false
  67.    button[name]["xmin"] = xmin
  68.    button[name]["ymin"] = ymin
  69.    button[name]["xmax"] = xmax
  70.    button[name]["ymax"] = ymax
  71. end
  72.  
  73. -- ClickButton handle
  74. function clickButton(name)
  75.     tmp_password[#tmp_password+1] = name
  76.    
  77.     fill(name, _color_active_buttons_, button[name])
  78.     sleep(.1)
  79.     fill(name, _color_buttons_, button[name])
  80.    
  81.     if ((#tmp_password-1) == _password_length_) then
  82.         checkPassword('checkPassword')
  83.     end
  84. end
  85.  
  86. -- CHECK PASSWORD
  87. function checkPassword(name)
  88.     local tmp = ""
  89.     for i = 1, y do
  90.         if tmp_password[i] ~= nil then
  91.             tmp = tmp .. tmp_password[i]
  92.         end
  93.     end
  94.    
  95.     if tmp == _password_ then
  96.         writeTask("open_door")
  97.         os.reboot()
  98.     else
  99.         os.reboot()
  100.     end
  101.    
  102. end
  103.  
  104. -- WRITE TASK
  105. function writeTask(task)
  106.     fs.delete("tasks")
  107.     local file = fs.open("tasks", "w")
  108.     file.write(task)
  109.     file.close()
  110. end
  111.  
  112. -- CHECKTASK ON STARTUP
  113. function checkTasks()
  114.     local file = fs.open("tasks", "r")
  115.     local data = file.readAll()
  116.     if (data == "open_door") then
  117.         redstone.setOutput(_redstone_output_, true)
  118.        
  119.         sleep(_redstone_output_time_)
  120.        
  121.         redstone.setOutput(_redstone_output_, false)
  122.        
  123.         writeTask("_clear_")
  124.        
  125.         os.reboot()
  126.     end
  127. end
  128.  
  129. -- BASIC STRUCTURE
  130. function fillTable()
  131.    setTable("1", "1",clickButton, 1, 2, 1, 1)
  132.    setTable("2", "2",clickButton, 3, 4, 1, 1)
  133.    setTable("3", "3",clickButton, 5, 6, 1, 1)
  134.    
  135.    setTable("4", "4",clickButton, 1, 2, 3, 3)
  136.    setTable("5", "5",clickButton, 3, 4, 3, 3)
  137.    setTable("6", "6",clickButton, 5, 6, 3, 3)
  138.    
  139.    setTable("7", "7",clickButton, 1, 2, 5, 5)
  140.    setTable("8", "8",clickButton, 3, 4, 5, 5)
  141.    setTable("9", "9",clickButton, 5, 6, 5, 5)
  142.    
  143.    setTable("enviar", "enviar", checkPassword, 20, 30, 17, 19)
  144. end    
  145.  
  146. -- Print button
  147. function fill(text, color, bData)
  148.    mon.setBackgroundColor(color)
  149.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  150.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  151.    for j = bData["ymin"], bData["ymax"] do
  152.       mon.setCursorPos(bData["xmin"], j)
  153.       if j == yspot then
  154.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  155.             if k == xspot then
  156.                mon.write(text)
  157.             else
  158.                mon.write(" ")
  159.             end
  160.          end
  161.       else
  162.          for i = bData["xmin"], bData["xmax"] do
  163.             mon.write(" ")
  164.          end
  165.       end
  166.    end
  167.    mon.setBackgroundColor(colors.black)
  168. end
  169.  
  170. -- Print all buttons
  171. function screen()
  172.    local currColor
  173.    
  174.    for name,data in pairs(button) do
  175.       local on = data["active"]
  176.       fill(name, _color_buttons_, data)
  177.    end
  178. end
  179.      
  180. -- internal click function
  181. function checkxy(x, y)
  182.    for name, data in pairs(button) do
  183.       if y>=data["ymin"] and  y <= data["ymax"] then
  184.          if x>=data["xmin"] and x<= data["xmax"] then
  185.             data["func"](name)
  186.             data["active"] = not data["active"]
  187.          end
  188.       end
  189.    end
  190. end
  191.  
  192.  
  193. function heading(text)
  194.    w, h = mon.getSize()
  195.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  196.    mon.write(text)
  197. end
  198.  
  199. -- Call Main program
  200. main()
  201.  
  202. -- TODOS LOS DERECHOS IFDS MaTaXeToS
Add Comment
Please, Sign In to add comment