Advertisement
Vilsol

Touchscreen v1.0

Feb 8th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. shell.run("clear")
  2.  
  3. w, h = term.getSize()
  4.  
  5. spawner_count = 6
  6.  
  7. cables = {colors.white, colors.orange, colors.magenta, colors.yellow, colors.lime, colors.pink}
  8.  
  9. sps = {}
  10. corx = {}
  11. cory = {}
  12. lents = {}
  13.  
  14. function loadSpawners()
  15.     if fs.exists("spawners") then
  16.         file = fs.open("spawners", "r")
  17.         for spr = 1, spawner_count do
  18.             line = file.readLine()
  19.             sps[spr] = tonumber(line)
  20.         end
  21.         file.close()
  22.     else
  23.         file = fs.open("spawners", "w")
  24.         for spr = 1, spawner_count do
  25.             file.writeLine("0")
  26.             sps[spr] = 0
  27.         end
  28.         file.close()
  29.     end
  30. end
  31.  
  32. function pC(text, y, tc, bc, indb)
  33.     if tc == "" then tc = colors.black end
  34.     if bc == "" then bc = colors.black end
  35.     term.setTextColor(tc)
  36.     term.setBackgroundColor(bc)
  37.     strl = math.floor(#text/2)
  38.     pos = w/2-strl
  39.     term.setCursorPos(pos, y)
  40.     term.write(text)
  41.     term.setTextColor(colors.white)
  42.     term.setBackgroundColor(colors.black)
  43.     if indb == 1 then
  44.         corx[#corx + 1] = pos
  45.         cory[#cory + 1] = y
  46.         lents[#lents + 1] = #text
  47.     end
  48. end
  49.  
  50. function iB(sx, sy, px, py, x, y)
  51.     if x >= sx and x <= sx + px then
  52.         if y >= sy and y <= sy + py then
  53.             return true;
  54.         else
  55.             return false;
  56.         end
  57.     else
  58.         return false;
  59.     end
  60. end
  61.  
  62. function draw()
  63.     corx = {}
  64.     cory = {}
  65.     lents = {}
  66.     shell.run("clear")
  67.     pC("Spawner control panel", 1, colors.cyan, colors.black, 0)
  68.     if(sps[1] == 1)then pC("Switch Enderman", 3, colors.white, colors.lime, 1) else pC("Switch Enderman", 3, colors.white, colors.red, 1) end
  69.     if(sps[2] == 1)then pC("Switch Zombie", 5, colors.white, colors.lime, 1) else pC("Switch Zombie", 5, colors.white, colors.red, 1) end
  70.     if(sps[3] == 1)then pC("Switch Skeleton", 7, colors.white, colors.lime, 1) else pC("Switch Skeleton", 7, colors.white, colors.red, 1) end
  71.     if(sps[4] == 1)then pC("Switch Cows", 9, colors.white, colors.lime, 1) else pC("Switch Cows", 9, colors.white, colors.red, 1) end
  72.     if(sps[5] == 1)then pC("Switch Pigs", 11, colors.white, colors.lime, 1) else pC("Switch Pigs", 11, colors.white, colors.red, 1) end
  73.     if(sps[6] == 1)then pC("Switch Sheeps", 13, colors.white, colors.lime, 1) else pC("Switch Sheeps", 13, colors.white, colors.red, 1) end
  74. end
  75.  
  76. function pulse(color, side)
  77.     if side == 0 then side = "bottom" end
  78.     redstone.setBundledOutput(side, color)
  79.     sleep(0.3)
  80.     redstone.setBundledOutput(side, 0)
  81. end
  82.  
  83. function rewritefile()
  84.     file = fs.open("spawners", "w")
  85.     for spr = 1, spawner_count do
  86.         file.writeLine(sps[spr])
  87.     end
  88.     file.close()
  89. end
  90.  
  91. function switchy(ideys)
  92.     pulse(cables[ideys], "bottom")
  93.     if(sps[ideys] == 1)then sps[ideys] = 0 else sps[ideys] = 1 end
  94.     rewritefile()
  95. end
  96.  
  97. function checker(clickx, clicky)
  98.     for checks = 1, #lents do
  99.         if iB(corx[checks], cory[checks], lents[checks], 0, clickx, clicky) then switchy(checks) end
  100.     end
  101. end
  102.  
  103. loadSpawners()
  104. draw()
  105.  
  106. while true do
  107.     event, button, xPos, yPos = os.pullEvent()
  108.     if event == "mouse_click" or event == "monitor_touch" then
  109.         draw()
  110.         checker(xPos, yPos)
  111.         draw()
  112.     end
  113.     sleep(0.1)
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement