MaTaXeToS

COMPUTER_CRAFT

Jan 14th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. local tmp_password = {""}
  6. local password = {""}
  7. mon.setBackgroundColor(colors.black)
  8.  
  9. local x
  10. local y
  11.  
  12. function setTable(name, value, func, xmin, xmax, ymin, ymax)
  13. button[name] = {}
  14. button[name]["value"] = value
  15. button[name]["func"] = func
  16. button[name]["active"] = false
  17. button[name]["xmin"] = xmin
  18. button[name]["ymin"] = ymin
  19. button[name]["xmax"] = xmax
  20. button[name]["ymax"] = ymax
  21. end
  22.  
  23. function clickButton(name)
  24. print("Click on: "..name)
  25. tmp_password[#tmp_password+1] = name
  26.  
  27. fill(name, colors.lime, button[name])
  28. sleep(.1)
  29. fill(name, colors.red, button[name])
  30.  
  31. print(#tmp_password)
  32. if (#tmp_password == 5) then
  33. print("COMPROBAMOS PASSWORD")
  34. checkPassword('checkPassword')
  35. end
  36. --saveAll()
  37. end
  38.  
  39. function checkPassword(name)
  40. local tmp = ""
  41. for i = 1, y do
  42. if tmp_password[i] ~= nil then
  43. tmp = tmp .. tmp_password[i]
  44. end
  45. end
  46.  
  47. if tmp == "5926" then
  48. writeTask("open_door")
  49. os.reboot()
  50. else
  51. os.reboot()
  52. end
  53.  
  54. end
  55.  
  56. function writeTask(task)
  57. fs.delete("tasks")
  58. local file = fs.open("tasks", "w")
  59. file.write(task)
  60. file.close()
  61. end
  62.  
  63. function checkTasks()
  64. local file = fs.open("tasks", "r")
  65. local data = file.readAll()
  66. if (data == "open_door") then
  67. redstone.setOutput("bottom", true)
  68.  
  69. sleep(5)
  70.  
  71. redstone.setOutput("bottom", false)
  72.  
  73. writeTask("_clear_")
  74.  
  75. os.reboot()
  76. end
  77. end
  78.  
  79. function fillTable()
  80. setTable("1", "1",clickButton, 1, 2, 1, 1)
  81. setTable("2", "2",clickButton, 3, 4, 1, 1)
  82. setTable("3", "3",clickButton, 5, 6, 1, 1)
  83.  
  84. setTable("4", "4",clickButton, 1, 2, 3, 3)
  85. setTable("5", "5",clickButton, 3, 4, 3, 3)
  86. setTable("6", "6",clickButton, 5, 6, 3, 3)
  87.  
  88. setTable("7", "7",clickButton, 1, 2, 5, 5)
  89. setTable("8", "8",clickButton, 3, 4, 5, 5)
  90. setTable("9", "9",clickButton, 5, 6, 5, 5)
  91.  
  92. setTable("enviar", "enviar", checkPassword, 20, 30, 17, 19)
  93. end
  94.  
  95. function fill(text, color, bData)
  96. mon.setBackgroundColor(color)
  97. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  98. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  99. for j = bData["ymin"], bData["ymax"] do
  100. mon.setCursorPos(bData["xmin"], j)
  101. if j == yspot then
  102. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  103. if k == xspot then
  104. mon.write(text)
  105. else
  106. mon.write(" ")
  107. end
  108. end
  109. else
  110. for i = bData["xmin"], bData["xmax"] do
  111. mon.write(" ")
  112. end
  113. end
  114. end
  115. mon.setBackgroundColor(colors.black)
  116. end
  117.  
  118. function screen()
  119. local currColor
  120.  
  121. for name,data in pairs(button) do
  122. local on = data["active"]
  123. --if on == true then currColor = colors.lime else currColor = colors.red end
  124. --fill(name, currColor, data)
  125. fill(name, colors.red, data)
  126. end
  127. end
  128.  
  129. function checkxy(x, y)
  130. for name, data in pairs(button) do
  131. if y>=data["ymin"] and y <= data["ymax"] then
  132. if x>=data["xmin"] and x<= data["xmax"] then
  133. data["func"](name) -- llamamos a la funcion del boton pasandole el nombre
  134. data["active"] = not data["active"]
  135. end
  136. end
  137. end
  138. end
  139.  
  140. function heading(text)
  141. w, h = mon.getSize()
  142. mon.setCursorPos((w-string.len(text))/2+1, 1)
  143. mon.write(text)
  144. end
  145.  
  146.  
  147.  
  148.  
  149. -- MAIN PROGRAM
  150.  
  151. x, y = mon.getSize()
  152. fillTable()
  153. checkTasks()
  154. while true do
  155. mon.clear()
  156. screen()
  157. local e,side,x,y = os.pullEvent("monitor_touch")
  158. print(x..":"..y)
  159. checkxy(x,y)
  160. sleep(.1)
  161. end
Advertisement
Add Comment
Please, Sign In to add comment