Advertisement
1ng0

button test

Sep 13th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. os.loadAPI("button")
  2. m = peripheral.wrap("left")
  3. m.clear()
  4. rednet.open("bottom")
  5. local page = 1
  6. local pages = 0
  7. local names = {}
  8. local turtles = {}
  9. local remove = false
  10.  
  11. function fillTurtles()
  12.    turtles[1] = 6
  13. --   turtles[2] = 114
  14. --   turtles[3] = 117
  15. end
  16.  
  17. function fillTable()
  18.    m.clear()
  19.    button.clearTable()
  20.    local totalrows = 0
  21.    local numNames = 0
  22.    local col = 2
  23.    local row = 12
  24.    local countRow = 1
  25.    local currName = 0
  26.    local npp = 12 --names per page
  27.    for turt, data in pairs(names) do
  28.       for i,j in pairs(data) do
  29.          totalrows = totalrows+1
  30.       end
  31.    end
  32.    pages = math.ceil(totalrows/npp)
  33.    print(totalrows)
  34.    for turt, data in pairs(names) do
  35.       currName = 0
  36.       for slot, name in pairs(data) do
  37.        currName = currName + 1
  38.        if currName > npp*(page-1) and currName < npp*page+1 then
  39.          row = 4+(countRow)
  40.          names[turt][slot] = string.sub(name, 0, 17)
  41.          button.setTable(string.sub(name, 0, 17), runStuff, turt..":"..slot, col, col+17 , row, row)
  42.          if col == 21 then
  43.            col = 1
  44.            countRow = countRow + 1
  45.          else
  46.            col = col+19
  47.          end
  48.        end
  49.       end
  50.    end
  51.    --button.setTable("Next Page", nextPage, "", 21, 38, 1, 1)
  52.    --button.setTable("Prev Page", prevPage, "", 2, 19, 1, 1)
  53.    button.setTable("Refresh", checkNames, "", 2, 16, 1, 1)
  54.    --button.setTable("Remove Book", removeIt, "", 2, 16, 19, 19)
  55.    --button.label(15,3, "Page: "..tostring(page).." of "..tostring(pages))
  56.    button.screen()
  57. end      
  58.  
  59. function nextPage()
  60.    if page+1 <= pages then
  61.       page = page+1
  62.    end
  63.    fillTable()
  64.    sleep(0.25)
  65. end
  66.  
  67. function prevPage()
  68.    if page-1 >= 1 then page = page-1 end
  69.    fillTable()
  70.    sleep(0.25)
  71. end  
  72.                            
  73. function getNames()
  74.    names = {}
  75.    for x, y in pairs(turtles) do
  76.       names[y] = {}
  77.       rednet.send(y, "getNames")
  78.       local id, msg, dist = rednet.receive(2)
  79. --      print(msg)
  80.       names[y] = textutils.unserialize(msg)
  81.    end
  82. end
  83.  
  84. function removeIt()
  85.    remove = not remove
  86. --   print(remove)
  87.    button.toggleButton("Remove Book")
  88. end
  89.  
  90. function runStuff(info)
  91.    if remove == true then
  92.       removeBook(info)
  93.    else
  94.       openPortal(info)
  95.    end      
  96. end
  97.  
  98. function removeBook(info)
  99.    local turt, slot = string.match(info, "(%d+):(%d+)")
  100.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  101.    data = "remove"..tostring(slot)
  102.    rednet.send(tonumber(turt), data)
  103.    rednet.receive()
  104.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  105.    remove=false
  106.    button.toggleButton("Remove Book")
  107. --   sleep(1)
  108.    checkNames()
  109. end  
  110.  
  111. function openPortal(info)
  112.    local turt,slot = string.match(info, "(%d+):(%d+)")
  113. --   print(names[tonumber(turt)][tonumber(slot)])
  114.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  115.    print(names[tonumber(turt)][tonumber(slot)])
  116.    data = "books"..tostring(slot)
  117.    rednet.send(tonumber(turt), data)
  118.    rednet.receive()
  119.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  120. end
  121.  
  122. function checkNames()
  123.    button.flash("Refresh")
  124.    for num, turt in pairs(turtles) do
  125.      rednet.send(turt, "checkSlots")
  126.      msg = ""
  127.      while msg ~= "done" do
  128.        id, msg, dist = rednet.receive()
  129.        if msg == "getName" then
  130.           m.clear()
  131.           m.setCursorPos(5, 12)
  132.           m.write("New book detected.")
  133.           m.setCursorPos(5, 14)
  134.           m.write("Please enter the name")
  135.           m.setCursorPos(5, 16)
  136.           m.write("On the computer")
  137.           m.setCursorPos(5, 18)
  138.           m.write("<<----")
  139.           term.clear()
  140.           term.write("Please enter a name for the new book: ")
  141.           name = read()
  142.           rednet.send(id, name)
  143.        end
  144.      end
  145.    end
  146.    getNames()
  147.    fillTable()
  148. end
  149.  
  150. function getClick()
  151.    event, side, x,y = os.pullEvent()
  152.    if event == "monitor_touch" then
  153.       button.checkxy(x,y)
  154.    elseif event == "redstone" then
  155.       print("redstone")
  156.       sleep(5)
  157.       checkNames()      
  158.    end
  159. end
  160.  
  161. fillTurtles()
  162. fillTable()
  163. checkNames()
  164.  
  165.  
  166. while true do
  167.    getClick()
  168. --   checkNames()
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement