Guest User

s

a guest
Aug 5th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. os.loadAPI("buttons")
  2. m = peripheral.wrap("top")
  3. rednet.open("bottom")
  4. bookNames = {}
  5.  
  6. stargateName = "STARGATE SG-1"
  7. stargateNameSize = 1
  8. stargateNameColor = colors.black
  9.  
  10. x,y = m.getSize()
  11. col = 3  --How many columns for buttons
  12. row = 3  --How many rows for buttons
  13. yHead = 1 --Amount of space for header
  14. yDown = 3 --Spaces between buttons and bottom
  15. --!! REQUIRED FOR THE PREV.&NEXT BUTTONS !!--
  16. y = y - 0 --Make room on the bottom
  17. x = x - 0 --For making room on the sides
  18. sStart = 0 --Global var to keep track of current book
  19. rcid = 66  --Remote chest ID
  20.  
  21. function split(inputstr, sep)
  22.   if sep == nil then sep = "%s" end
  23.   local t={} ; i=1
  24.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  25.     t[i] = str
  26.     i = i + 1
  27.   end
  28.   return t
  29. end
  30.  
  31. function printTable(t)
  32.   --For testing purposes
  33.   for k,v in pairs(t) do
  34.     print("key: "..tostring(k).." value: "..tostring(v))
  35.   end
  36. end
  37.  
  38. function createButton()
  39.   local xmin,xmax,ymin,ymax
  40.   local j = sStart
  41.   for i1 = 1,col do
  42.     for i2 = 1,row do    
  43.       j = j + 1
  44.       if n[j] == nil then break end
  45.       xcol = math.floor(x/col)
  46.       if (xcol*col) > x then xcol = xcol - 1 end
  47.       yrow = math.floor((y-yHead-yDown)/row)
  48.       if (yrow*row+yHead+yDown) > y then yrow = yrow - 1 end
  49.       xmin = (xcol*i1+1)-(xcol-1)
  50.       xmax = (xcol*(i1+1)-1)-(xcol-1)
  51.       ymin = (yrow*i2+1)-(yrow-1)+yHead
  52.       ymax = (yrow*(i2+1)-1)-(yrow-1)+yHead
  53.       buttons.setTable(n[j],moveItem,keys[n[j]],xmin,xmax,ymin,ymax)
  54.     end
  55.   end
  56. end
  57.  
  58. function prev(i)
  59.   sStart = sStart - row * col
  60.   refresh()
  61. end
  62.  
  63. function next(i)
  64.   sStart = sStart + row * col
  65.   refresh()
  66. end  
  67.  
  68. function getBookNames()
  69.   message = "name/"
  70.   rednet.send(rcid,message)
  71.   event, id, message = os.pullEvent("rednet_message")
  72.   return split(message,"/")
  73. end
  74.  
  75. function moveItem(slot,name)
  76.   message = "move/"..slot
  77.   rednet.send(rcid,message)
  78.   --event, id, message = os.pullEvent("rednet_message")
  79. end
  80.  
  81. function getClick()
  82.   cont = false
  83.   while not cont do
  84.     event,p1,p2,p3 = os.pullEvent("monitor_touch")
  85.     cont = buttons.checkxy(p2,p3)
  86.   end
  87. end
  88.  
  89. function createBottom()
  90.   xcol = math.floor(x/col)
  91.   if xcol*col > x then xcol = xcol - 1 end
  92.   if sStart > 0 then
  93.     buttons.setTable("< Prev",prev,0,2,xcol-1,y-1,y-1)
  94.   end
  95.   if #n > sStart+col*row then
  96.     buttons.setTable("Next >",next,0,xcol+1,xcol*2-1,y-1,y-1)
  97.   end  
  98. end
  99.  
  100. function starter()
  101.   n = getBookNames()
  102.   keys = {}
  103.   for k,v in pairs(n) do
  104.     keys[v] = k
  105.   end
  106.   table.sort(n, function(a,b) return string.lower(a)<string.lower(b) end)
  107.   sleep(0.5)
  108.   refresh()
  109. end
  110.    
  111. function refresh()
  112.   buttons.clearTable()
  113.   buttons.loadingScreen("Loading",1)
  114.   buttons.heading(stargateName,stargateNameColor,stargateNameSize)
  115.   createButton()
  116.   createBottom()
  117.   buttons.screen()
  118. end
  119.  
  120. starter()
  121. while true do
  122.   getClick()
  123. end
Advertisement
Add Comment
Please, Sign In to add comment