Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c = peripheral.wrap('bottom')
- m = peripheral.wrap('top')
- local controllerX, controllerY
- buttons = {}
- local currentApp = "keypad"
- displayOffset = 0
- local targetCount = 0
- local destinations
- local activeButton = 0
- function resizeMon()
- controllerX, controllerY = m.getSize()
- m.setTextScale(0.5)
- end
- function clearAll()
- empty(m)
- end
- function empty()
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setCursorPos(1,1)
- end
- function listDestinations()
- destinations={}
- for i=1,27 do
- if c.getStackInSlot(i) ~= nil then
- name = tostring(c.getStackInSlot(i).myst_book.destination)
- destinations[i]={}
- destinations[i]['name'] = name
- destinations[i]['favorite'] = false
- destinations[i]['active'] = false
- print(i.." - "..name)
- end
- end
- return destinations
- end
- function moveBook(InOrOut,bookNumber)
- local aaa = true
- if InOrOut == "in" then
- if c.pushItem('west',bookNumber,1) == 0 then
- print("There is already a book inside")
- aaa = false
- end
- elseif InOrOut == "out" then
- c.pullItem('west',1,1)
- end
- return aaa
- end
- function drawTitle(x, y, text, color_txt, color_bg, monitor)
- monitor.setBackgroundColor(color_bg)
- monitor.setTextColor(color_txt)
- monitor.setCursorPos(x,y)
- monitor.write(text)
- monitor.setCursorPos(1,1)
- monitor.setBackgroundColor(colors.black)
- end
- function drawLine(x, y, length, size, color_bar, monitor)
- for yPos = y, y+size-1 do
- monitor.setBackgroundColor(color_bar)
- monitor.setCursorPos(x, yPos)
- monitor.write(string.rep(" ", length))
- monitor.setBackgroundColor(colors.black)
- end
- end
- function drawProg(x, y, length, size, minVal, maxVal, color_bar, color_bg, monitor)
- drawLine(x, y, length, size, color_bg, monitor)
- local barSize = math.floor((minVal/maxVal)*length)
- drawLine(x, y, barSize, size, color_bar, monitor)
- end
- function drawContent(x, y, height, width, minVal, maxVal, color_bar, color_bg, monitor)
- drawGraph(x, y, height, width, color_bg, monitor)
- local a = math.floor((minVal/maxVal)*height)
- drawGraph(x, y, a, width, color_bar, monitor)
- end
- function drawGraph(x, y, height, width, color_bar, monitor)
- drawLine(x, y-height+1, width, height, color_bar, monitor)
- end
- function addButton(x,y,w,h,name,colorOn,colorOff,state,textOn,textOff,textX,textY,header)
- buttons[name] = {["x"] = x,["y"] = y,["width"] = w,["height"] = h,
- ["colorOn"] = colorOn,["colorOff"] = colorOff,["state"] = state,
- ["textOn"] = textOn,["textOff"] = textOff,
- ["textX"] = textX,["textY"] = textY,["header"] = header}
- end
- function addButtons()
- for i=0,3 do
- addButton(2,6+i*4,13,3,"buttonPos"..i,colors.lime,colors.red,false,"TARGET","TARGET",3,7+i*4,"")
- end
- addButton(2,4,13,1,"buttonUP",colors.cyan,colors.lightGray,false," / \\"," / \\",2,4,"")
- addButton(2,22,13,1,"buttonDOWN",colors.cyan,colors.lightGray,true," \\ /"," \\ /",2,22,"")
- end
- function generateGUI(monitor)
- if currentApp == "keypad" then
- drawTitle(2, 2, 'Select Target', colors.white, colors.black, m)
- if targetCount-(displayOffset*4) >= 1 then
- buttons['buttonPos0'].textOn = destinations[1+(displayOffset*4)]['name']
- buttons['buttonPos0'].textOff = buttons['buttonPos0'].textOn
- if 1+(displayOffset*4) == activeButton then
- buttons['buttonPos0'].state = true
- else
- buttons['buttonPos0'].state = false
- end
- drawButton("buttonPos0",m)
- end
- if targetCount-(displayOffset*4) >= 2 then
- buttons['buttonPos1'].textOn = destinations[2+(displayOffset*4)]['name']
- buttons['buttonPos1'].textOff = buttons['buttonPos1'].textOn
- if 2+(displayOffset*4) == activeButton then
- buttons['buttonPos1'].state = true
- else
- buttons['buttonPos1'].state = false
- end
- drawButton("buttonPos1",m)
- end
- if targetCount-(displayOffset*4) >= 3 then
- buttons['buttonPos2'].textOn = destinations[3+(displayOffset*4)]['name']
- buttons['buttonPos2'].textOff = buttons['buttonPos2'].textOn
- if 3+(displayOffset*4) == activeButton then
- buttons['buttonPos2'].state = true
- else
- buttons['buttonPos2'].state = false
- end
- drawButton("buttonPos2",m)
- end
- if targetCount-(displayOffset*4) >= 4 then
- buttons['buttonPos3'].textOn = destinations[4+(displayOffset*4)]['name']
- buttons['buttonPos3'].textOff = buttons['buttonPos3'].textOn
- if 4+(displayOffset*4) == activeButton then
- buttons['buttonPos3'].state = true
- else
- buttons['buttonPos3'].state = false
- end
- drawButton("buttonPos3",m)
- end
- if displayOffset == 0 then
- buttons['buttonUP'].state = false
- else
- buttons['buttonUP'].state = true
- end
- if displayOffset + 4 == targetCount or targetCount < 4 then
- buttons['buttonDOWN'].state = false
- else
- buttons['buttonDOWN'].state = true
- end
- drawButton("buttonUP",m)
- drawButton("buttonDOWN",m)
- else error("App not found") end
- end
- function drawButton(button,monitor)
- local color = colors.red
- local text = ""
- if buttons[button].state==true then color = buttons[button].colorOn else color = buttons[button].colorOff end
- if buttons[button].state==true then text = buttons[button].textOn else text = buttons[button].textOff end
- drawLine(buttons[button].x, buttons[button].y, buttons[button].width, buttons[button].height, color, monitor)
- if buttons[button].header == "" then else
- drawTitle(buttons[button].x, buttons[button].y - 1, buttons[button].header, colors.white, colors.black, monitor) end
- drawTitle(buttons[button].textX, buttons[button].textY, text, colors.white, color, monitor)
- end
- function setupTargets()
- moveBook('out',0)
- destinations = listDestinations()
- targetCount = table.getn(destinations)
- end
- function touchAreas(x,y,side)
- if x >= buttons["buttonPos0"].x and x < buttons["buttonPos0"].x + buttons["buttonPos0"].width and
- y >= buttons["buttonPos0"].y and y < buttons["buttonPos0"].y + buttons["buttonPos0"].height then
- if buttons["buttonPos0"].textOn ~= "TARGET" then
- if activeButton == 0 then
- if moveBook('in',1+(displayOffset*4)) then
- activeButton = 1+(displayOffset*4)
- end
- elseif activeButton == 1+(displayOffset*4) then
- moveBook('out',1+(displayOffset*4))
- activeButton = 0
- end
- end
- elseif x >= buttons["buttonPos1"].x and x < buttons["buttonPos1"].x + buttons["buttonPos1"].width and
- y >= buttons["buttonPos1"].y and y < buttons["buttonPos1"].y + buttons["buttonPos1"].height then
- if buttons["buttonPos1"].textOn ~= "TARGET" then
- if activeButton == 0 then
- if moveBook('in',2+(displayOffset*4)) then
- activeButton = 2+(displayOffset*4)
- end
- elseif activeButton == 2+(displayOffset*4) then
- moveBook('out',2+(displayOffset*4))
- activeButton = 0
- end
- end
- elseif x >= buttons["buttonPos2"].x and x < buttons["buttonPos2"].x + buttons["buttonPos2"].width and
- y >= buttons["buttonPos2"].y and y < buttons["buttonPos2"].y + buttons["buttonPos2"].height then
- if buttons["buttonPos2"].textOn ~= "TARGET" then
- if activeButton == 0 then
- if moveBook('in',3+(displayOffset*4)) then
- activeButton = 3+(displayOffset*4)
- end
- elseif activeButton == 3+(displayOffset*4) then
- moveBook('out',3+(displayOffset*4))
- activeButton = 0
- end
- end
- elseif x >= buttons["buttonPos3"].x and x < buttons["buttonPos3"].x + buttons["buttonPos3"].width and
- y >= buttons["buttonPos3"].y and y < buttons["buttonPos3"].y + buttons["buttonPos3"].height then
- if buttons["buttonPos3"].textOn ~= "TARGET" then
- if activeButton == 0 then
- if moveBook('in',4+(displayOffset*4)) then
- activeButton = 4+(displayOffset*4)
- end
- elseif activeButton == 4+(displayOffset*4) then
- moveBook('out',4+(displayOffset*4))
- activeButton = 0
- end
- end
- elseif x >= buttons["buttonUP"].x and x < buttons["buttonUP"].x + buttons["buttonUP"].width and
- y >= buttons["buttonUP"].y and y < buttons["buttonUP"].y + buttons["buttonUP"].height then
- if displayOffset ~= 0 then displayOffset = displayOffset - 1 end
- elseif x >= buttons["buttonDOWN"].x and x < buttons["buttonDOWN"].x + buttons["buttonDOWN"].width and
- y >= buttons["buttonDOWN"].y and y < buttons["buttonDOWN"].y + buttons["buttonDOWN"].height then
- if 4 + displayOffset < targetCount then
- displayOffset = displayOffset + 1
- end
- else
- end
- clearAll()
- end
- function waitForInput()
- parallel.waitForAny(wait,getTouch)
- end
- function getTouch()
- event, side, xPos, yPos = os.pullEvent("monitor_touch")
- touchAreas(xPos, yPos, side)
- end
- function wait()
- sleep(1)
- end
- function init()
- clearAll()
- resizeMon()
- addButtons()
- clearAll()
- setupTargets()
- while true do
- generateGUI(m)
- waitForInput()
- clearAll()
- end
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment