Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- m = peripheral.wrap("top")
- x,y = m.getSize()
- rednet.open("bottom")
- --Table to hold turtle information
- -- Label = id, xmin, xmax, ymin, ymax, page, active
- -- organize pages as tables instead of page numbers for each button
- -- the first page is the current page
- -- pages are changed by making the last page the first page
- -- using table.remove and table.insert
- local turtle = {
- {
- ["Quarry 1"] = { id = 19, xmin = 2, xmax = 11, ymin = 4, ymax = 5, },
- ["Quarry 2"] = { id = 18, xmin = 2, xmax = 11, ymin = 6, ymax = 7, },
- ["Branch 1"] = { id = 22, xmin = 2, xmax = 11, ymin = 8, ymax = 9, },
- ["Branch 2"] = { id = 20, xmin = 2, xmax = 11, ymin = 10, ymax = 11, },
- }, {
- ["Branch 3"] = { id = 29, xmin = 2, xmax = 11, ymin = 4, ymax = 5, },
- ["Branch 4"] = { id = 30, xmin = 2, xmax = 11, ymin = 6, ymax = 7, },
- ["Branch 5"] = { id = 31, xmin = 2, xmax = 11, ymin = 8, ymax = 9, },
- ["Branch 6"] = { id = 32, xmin = 2, xmax = 11, ymin = 10, ymax = 11, },
- }
- }
- --Table to hold GUI. (Buttons)
- local menu = {
- -- Name, xmin, xmax, ymin, ymax, active
- ["Prev"] = { xmin = 1, xmax = 5, ymin = 12, ymax = 13, },
- ["Next"] = { xmin = 9, xmax = 13, ymin = 12, ymax = 13, },
- }
- --Function to add commas to thousandth place.
- -- Not used yet. Utilized when turtles send updates.
- function comma(num)
- local a,b,c = string.match(num, '^([^%d]*%d)(%d*)(.?%d*)$')
- return a..(b:reverse():gsub('%d%d%d','%1,'):reverse())..c
- end
- -- Function to write to specified points
- -- using arguments.
- function writeTo(x,y,bColor,tColor,text)
- local bColor = bColor or colors.black
- local tColor = tColor or colors.white
- m.setCursorPos(x,y)
- m.setBackgroundColor(bColor)
- m.setTextColor(tColor)
- m.write(text)
- m.setTextColor(colors.white)
- m.setBackgroundColor(colors.black)
- end
- -- Centers text on y line.
- function centerPrint(text, tColor, bColor, y)
- local mx,my = m.getSize()
- local xPos = math.floor((mx/2) - (#text/2))
- local tColor = tColor or colors.white
- local bColor = bColor or colors.black
- m.setTextColor(tColor)
- m.setBackgroundColor(bColor)
- m.setCursorPos(xPos,y)
- m.write(text)
- m.setBackgroundColor(colors.black)
- m.setTextColor(colors.white)
- m.setCursorPos(1,y+1)
- end
- -- Determines if rednet is enabled or not.
- function rEnabled()
- local rColor
- if rednet.isOpen("bottom") then
- rColor = colors.lime
- else
- rColor = colors.red
- rednet.open("bottom")
- end
- writeTo(24,12,rColor,colors.white,"Rednet")
- end
- -- Draws main menu.
- function drawMain()
- m.clear()
- local mx,my = m.getSize()
- -- the title
- centerPrint("Random List", colors.lime, colors.black, 1)
- centerPrint(string.rep("=",x+2), colors.lime, colors.black,2)
- -- the frame
- writeTo(13,4,colors.black,colors.lime,"+")
- m.write(string.rep("-",15))
- writeTo(x,4,colors.black,colors.lime,"+")
- m.setCursorPos(13,5)
- for i = 5,10 do
- m.write("|"..string.rep(" ",15).."|")
- m.setCursorPos(13,i)
- end
- writeTo(13,10,colors.black,colors.lime,"+")
- m.write(string.rep("-",15))
- writeTo(x,10,colors.black,colors.lime,"+")
- -- frame info
- writeTo(14,5,colors.black,colors.lime,"Fuel:")
- writeTo(14,6,colors.black,colors.gray, string.rep("-",15))
- writeTo(14,7,colors.black,colors.lime,"Status:")
- writeTo(14,8,colors.black,colors.gray, string.rep("-",15))
- writeTo(14,9,colors.black,colors.lime,"ID:")
- rEnabled()
- -- draw next/prev buttons
- for k,v in pairs(menu) do
- writeTo(v.xmin, v.ymin, colors.black, colors.lime,k)
- end
- end
- -- Draws the buttons for turtles and the buttons
- function turtleButtons()
- m.clear()
- drawMain()
- -- draw the first page
- for k,v in pairs(turtle[1]) do
- writeTo(v.xmin,v.ymin,colors.black,colors.purple,k)
- end
- end
- --Check for valid click
- function checkTouch()
- local e,side,x,y = os.pullEvent("monitor_touch")
- for k,v in pairs(turtle[1]) do
- if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
- -- bad logic here
- -- just redraw the button twice
- --[[
- for i = 1,2 do
- v.active = not v.active
- toggleButton()
- sleep(0.2)
- end
- ]]
- writeTo(v.xmin, v.ymin, colors.purple, colors.lime, k)
- sleep(0.2)
- writeTo(v.xmin, v.ymin, colors.black, colors.lime, k)
- -- break out because if we find a button, no need to search for any others
- break
- end
- end
- --determines where you clicked for next/prev menu
- for k,v in pairs(menu) do
- if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
- --[[
- for i = 1,2 do
- v.active = not v.active
- toggleButton()
- sleep(0.2)
- end
- ]]
- writeTo(v.xmin, v.ymin, colors.lime, colors.purple, k)
- sleep(0.2)
- writeTo(v.xmin, v.ymin, colors.black, colors.lime, k)
- if k == 'Next' then
- -- throw the first page at the end
- table.insert(turtle, table.remove(turtle, 1))
- elseif k == 'Prev' then
- -- throw the last page at the start
- table.insert(turtle, 1, table.remove(turtle, #turtle))
- end
- -- again, no need to check for others
- break
- end
- end
- end
- --Toggles button colors
- --[[
- function toggleButton()
- local bColor
- local tColor
- for k,v in pairs(turtle) do
- local on = v.active
- if on then
- bColor = colors.purple
- tColor = colors.lime
- else
- bColor = colors.black
- tColor = colors.purple
- end
- writeTo(v.xmin,v.ymin,bColor,tColor,k)
- end
- for k,v in pairs(menu) do
- local on = v.active
- if on then
- bColor = colors.lime
- tColor = colors.purple
- else
- bColor = colors.black
- tColor = colors.lime
- end
- writeTo(v.xmin,v.ymin,bColor,tColor,k)
- end
- end
- ]]
- while true do
- drawMain()
- turtleButtons()
- checkTouch()
- end
Advertisement
Add Comment
Please, Sign In to add comment