Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- storage = peripheral.wrap("left")
- monitor = peripheral.wrap("top")
- monitor.setBackgroundColor(8)
- monitor.clear()
- monitor.setTextScale(0.5)
- monitor.x,monitor.y = monitor.getSize()
- GUIButtons = {}
- Button = {}
- GUIIndex = 1
- scrollBtnWidth = 98
- scrollBtnHeight = 3
- buttonWidth = 48
- buttonHeight = 3
- buttonsPerRow = 2
- buttonsPerColumn = 8
- GUIMax = buttonsPerRow * buttonsPerColumn
- --button object
- --constructor
- function Button.new(name,x,y,width,height,funct)
- local object = {}
- object.name = name
- object.x = x
- object.y = y
- object.width = width
- object.height = height
- object.draw = Button.draw
- object.onClick = funct
- return object
- end
- --draw function
- function Button.draw(self,monitor,color)
- monitor.setBackgroundColor(color)
- for a=0,self.height - 1,1 do
- for b=self.x,self.width + self.x - 1,1 do
- monitor.setCursorPos(b,self.y+a)
- monitor.write(" ")
- end
- end
- l = string.len(self.name)
- textX = math.ceil((self.width - l)/2)
- textY = math.floor(self.height/2)
- monitor.setTextColor(1)
- monitor.setCursorPos(textX+self.x,textY+self.y)
- monitor.write(self.name)
- end
- --GUI functions
- function populateGUI(index, books)
- GUIButtons = {}
- numBooks = getSize(books)
- maxDisplayed = GUIMax
- upFlag = false
- --scrollUp
- if index > 1 then
- maxDisplayed = maxDisplayed - 1
- btn = Button.new("^",2,2,scrollBtnWidth,scrollBtnHeight,scrollUp)
- table.insert(GUIButtons,btn)
- upFlag = true
- end
- --scrollDown
- if numBooks - index >= maxDisplayed then
- maxDisplayed = maxDisplayed - 1
- btn = Button.new("v",2,monitor.y - 1 - scrollBtnHeight, scrollBtnWidth, scrollBtnHeight, scrollDown)
- table.insert(GUIButtons,btn)
- end
- -- cap to limit
- if maxDisplayed > numBooks then
- maxDisplayed = numBooks
- end
- for button=1,maxDisplayed,1 do
- btnXPos = (button & buttonsPerRow) + 1
- btnYPos = math.floor((button-1) / buttonsPerRow)
- btnXCoord = ((btnXPos - 1) * (buttonWidth + 1)) + 2
- btnYCoord = (btnYPos * (buttonHeight+1)) + 2
- --compensate for scroll up button
- if upFlag then
- btnYCoord = btnYCoord + scrollBtnHeight + 1
- end
- btn = Button.new(books[button+index]["name"], btnXCoord, btnYCoord, buttonWidth, buttonHeight, bookClicked)
- table.insert(GUIButtons,btn)
- end
- return GUIButtons
- end
- function renderGUI(GUIButtons)
- monitor.setBackgroundColor(8)
- monitor.clear()
- for index,button in pairs(GUIButtons) do
- button:draw(monitor,2048)
- end
- end
- --mechanical functions
- function grabBook(name)
- books = getBooks(storage)
- for index,data in pairs(books) do
- if data["name"] == name then
- --wait for recieving slot to be cleared if not empty
- while turtle.getItemCount(1) > 0 do
- os.sleep(0.1)
- end
- storage.pushItemIntoSlot("north",index,1,1)
- end
- end
- end
- function getBooks(storage)
- storage.condenseItems()
- return storage.getAllStacks()
- end
- --helper functions
- function getSize(tab)
- len = 0
- for index,data in tab do
- len = len + 1
- end
- return len
- end
- --event handler
- function eventLoop()
- while true do
- event,side,xm,ym = os.pullEvent("monitor_touch")
- eventHandler(xm,ym)
- end
- end
- function eventHandler(x,y)
- print("x:"..x.." y:"..y)
- for index,button in pairs(GUIButtons) do
- if button.x <= x and button.x + button.width >= x and
- button.y <= y and button.y + button.height >= y then
- button:onClick()
- end
- end
- end
- --events
- function scrollUp(self)
- print("^")
- self:draw(monitor,3)
- os.sleep(0.1)
- GUIIndex = GUIIndex -buttonsPerRow
- if GUIIndex <= 0 then
- GUIIndex = 1
- end
- renderGUI(populateGUI(GUIIndex))
- end
- function scrollDown(self)
- print("v")
- self:draw(monitor,3)
- os.sleep(0.1)
- GUIIndex = GUIIndex + buttonsPerRow
- renderGUI(populateGUI(GUIIndex))
- end
- function bookClicked(self)
- print("book")
- print(self["name"])
- grabBook(self["name"])
- self:draw(monitor,3)
- os.sleep(0.1)
- renderGUI(populateGUI(GUIIndex))
- end
- --reflesh loop
- function refresh()
- while true do
- books = getBooks(storage)
- buttons = populateGUI(GUIIndex,books)
- renderGUI(buttons)
- os.sleep(0.5)
- end
- end
- --main
- function main()
- parallel.waitForAny(eventLoop,refresh)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement