Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | None | 0 0
  1. storage = peripheral.wrap("left")
  2. monitor = peripheral.wrap("top")
  3. monitor.setBackgroundColor(8)
  4. monitor.clear()
  5. monitor.setTextScale(0.5)
  6. monitor.x,monitor.y = monitor.getSize()
  7.  
  8. GUIButtons = {}
  9. Button = {}
  10. GUIIndex = 1
  11.  
  12. scrollBtnWidth = 98
  13. scrollBtnHeight = 3
  14.  
  15. buttonWidth = 48
  16. buttonHeight = 3
  17. buttonsPerRow = 2
  18. buttonsPerColumn = 8
  19. GUIMax = buttonsPerRow * buttonsPerColumn
  20.  
  21.  
  22. --button object
  23. --constructor
  24. function Button.new(name,x,y,width,height,funct)
  25.   local object = {}
  26.   object.name  = name
  27.   object.x = x
  28.   object.y = y
  29.   object.width   = width
  30.   object.height  = height
  31.   object.draw    = Button.draw
  32.   object.onClick = funct
  33.   return object
  34. end
  35.  
  36. --draw function
  37. function Button.draw(self,monitor,color)
  38.   monitor.setBackgroundColor(color)
  39.   for a=0,self.height - 1,1 do
  40.     for b=self.x,self.width + self.x - 1,1 do
  41.       monitor.setCursorPos(b,self.y+a)
  42.       monitor.write(" ")
  43.     end
  44.   end
  45.   l = string.len(self.name)
  46.   textX = math.ceil((self.width - l)/2)
  47.   textY = math.floor(self.height/2)
  48.   monitor.setTextColor(1)
  49.   monitor.setCursorPos(textX+self.x,textY+self.y)
  50.   monitor.write(self.name)
  51. end
  52.  
  53. --GUI functions
  54.  
  55. function populateGUI(index, books)
  56.     GUIButtons = {}
  57.     numBooks   = getSize(books)
  58.     maxDisplayed = GUIMax
  59.     upFlag = false
  60.     --scrollUp
  61.     if index > 1 then
  62.         maxDisplayed = maxDisplayed - 1
  63.         btn = Button.new("^",2,2,scrollBtnWidth,scrollBtnHeight,scrollUp)
  64.         table.insert(GUIButtons,btn)
  65.         upFlag = true
  66.     end
  67.     --scrollDown
  68.     if numBooks - index >= maxDisplayed then
  69.         maxDisplayed = maxDisplayed - 1
  70.         btn = Button.new("v",2,monitor.y - 1 - scrollBtnHeight, scrollBtnWidth, scrollBtnHeight, scrollDown)
  71.         table.insert(GUIButtons,btn)
  72.     end
  73.     -- cap to limit
  74.     if maxDisplayed > numBooks then
  75.         maxDisplayed = numBooks
  76.     end
  77.     for button=1,maxDisplayed,1 do
  78.         btnXPos = (button & buttonsPerRow) + 1
  79.         btnYPos = math.floor((button-1) / buttonsPerRow)
  80.         btnXCoord = ((btnXPos - 1) * (buttonWidth + 1)) + 2
  81.         btnYCoord = (btnYPos * (buttonHeight+1)) + 2
  82.         --compensate for scroll up button
  83.         if upFlag then
  84.             btnYCoord = btnYCoord + scrollBtnHeight + 1
  85.         end
  86.         btn = Button.new(books[button+index]["name"], btnXCoord, btnYCoord, buttonWidth, buttonHeight, bookClicked)
  87.         table.insert(GUIButtons,btn)
  88.     end
  89.     return GUIButtons
  90. end
  91.  
  92. function renderGUI(GUIButtons)
  93.     monitor.setBackgroundColor(8)
  94.     monitor.clear()
  95.     for index,button in pairs(GUIButtons) do
  96.         button:draw(monitor,2048)
  97.     end
  98. end
  99.    
  100. --mechanical functions
  101. function grabBook(name)
  102.     books = getBooks(storage)
  103.     for index,data in pairs(books) do
  104.         if data["name"] == name then
  105.             --wait for recieving slot to be cleared if not empty
  106.             while turtle.getItemCount(1) > 0 do
  107.                 os.sleep(0.1)
  108.             end
  109.             storage.pushItemIntoSlot("north",index,1,1)
  110.         end
  111.     end
  112. end
  113.    
  114. function getBooks(storage)
  115.     storage.condenseItems()
  116.     return storage.getAllStacks()
  117. end
  118.    
  119.    
  120.    
  121.    
  122. --helper functions
  123. function getSize(tab)
  124.     len = 0
  125.     for index,data in tab do
  126.         len = len + 1
  127.     end
  128.     return len
  129. end
  130.  
  131. --event handler
  132. function eventLoop()
  133.   while true do
  134.     event,side,xm,ym = os.pullEvent("monitor_touch")
  135.     eventHandler(xm,ym)    
  136.   end
  137. end
  138.  
  139. function eventHandler(x,y)
  140.   print("x:"..x.." y:"..y)
  141.   for index,button in pairs(GUIButtons) do
  142.     if button.x <= x and button.x + button.width >= x and
  143.        button.y <= y and button.y + button.height >= y then
  144.        button:onClick()
  145.     end
  146.   end
  147. end    
  148.  
  149. --events
  150. function scrollUp(self)
  151.   print("^")
  152.   self:draw(monitor,3)
  153.   os.sleep(0.1)
  154.   GUIIndex = GUIIndex -buttonsPerRow
  155.   if GUIIndex <= 0 then
  156.     GUIIndex = 1
  157.   end
  158.   renderGUI(populateGUI(GUIIndex))
  159. end
  160.  
  161. function scrollDown(self)
  162.   print("v")
  163.   self:draw(monitor,3)
  164.   os.sleep(0.1)
  165.   GUIIndex = GUIIndex + buttonsPerRow
  166.   renderGUI(populateGUI(GUIIndex))  
  167. end
  168.  
  169. function bookClicked(self)
  170.   print("book")
  171.   print(self["name"])
  172.   grabBook(self["name"])
  173.   self:draw(monitor,3)
  174.   os.sleep(0.1)
  175.   renderGUI(populateGUI(GUIIndex))
  176. end
  177.  
  178. --reflesh loop
  179. function refresh()
  180.   while true do
  181.     books = getBooks(storage)
  182.     buttons = populateGUI(GUIIndex,books)
  183.     renderGUI(buttons)
  184.     os.sleep(0.5)
  185.   end
  186. end
  187.  
  188. --main
  189. function main()
  190. parallel.waitForAny(eventLoop,refresh)
  191. end
  192.  
  193. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement