Guest User

Untitled

a guest
Apr 30th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. m = peripheral.wrap("top")
  2. x,y = m.getSize()
  3. rednet.open("bottom")
  4.  
  5. --Table to hold turtle information
  6. -- Label = id, xmin, xmax, ymin, ymax, page, active
  7. local turtle = {
  8.     ["Quarry 1"] = { id = 19, xmin = 2, xmax = 11, ymin = 4, ymax = 5, page = 1, active = false },
  9.     ["Quarry 2"] = { id = 18, xmin = 2, xmax = 11, ymin = 6, ymax = 7, page = 1, active = false },
  10.     ["Branch 1"] = { id = 22, xmin = 2, xmax = 11, ymin = 8, ymax = 9, page = 1, active = false },
  11.     ["Branch 2"] = { id = 20, xmin = 2, xmax = 11, ymin = 10, ymax = 11, page = 1, active = false },
  12.     ["Branch 3"] = { id = 29, xmin = 2, xmax = 11, ymin = 4, ymax = 5, page = 2, active = false },
  13.     ["Branch 4"] = { id = 30, xmin = 2, xmax = 11, ymin = 6, ymax = 7, page = 2, active = false },
  14.     ["Branch 5"] = { id = 31, xmin = 2, xmax = 11, ymin = 8, ymax = 9, page = 2, active = false },
  15.     ["Branch 6"] = { id = 32, xmin = 2, xmax = 11, ymin = 10, ymax = 11, page = 2, active = false },
  16.  
  17. }
  18.  
  19. --Table to hold GUI. (Buttons)
  20. local menu = {
  21. --   Name,  xmin, xmax, ymin, ymax, active
  22.     ["Prev"] = { xmin = 1, xmax = 5, ymin = 12, ymax = 13, active = false },
  23.     ["Next"] = { xmin = 9, xmax = 13, ymin = 12, ymax = 13, active = false },
  24.    
  25. }
  26.  
  27. --Function to add commas to thousandth place.
  28. -- Not used yet. Utilized when turtles send updates.
  29. function comma(num)
  30.       local a,b,c = string.match(num, '^([^%d]*%d)(%d*)(.?%d*)$')
  31.       return a..(b:reverse():gsub('%d%d%d','%1,'):reverse())..c
  32. end
  33. -- Function to write to specified points
  34. -- using arguments.
  35. function writeTo(x,y,bColor,tColor,text)
  36.    local bColor = bColor or colors.black
  37.    local tColor = tColor or colors.white
  38.    m.setCursorPos(x,y)
  39.    m.setBackgroundColor(bColor)
  40.    m.setTextColor(tColor)
  41.    m.write(text)
  42.    m.setTextColor(colors.white)
  43.    m.setBackgroundColor(colors.black)
  44. end
  45.  
  46. -- Centers text on y line.
  47. function centerPrint(text, tColor, bColor, y)
  48.     local mx,my = m.getSize()
  49.     local xPos = math.floor((mx/2) - (#text/2))
  50.     local tColor = tColor or colors.white
  51.     local bColor = bColor or colors.black
  52.       m.setTextColor(tColor)
  53.       m.setBackgroundColor(bColor)
  54.       m.setCursorPos(xPos,y)
  55.       m.write(text)
  56.       m.setBackgroundColor(colors.black)
  57.       m.setTextColor(colors.white)
  58.       m.setCursorPos(1,y+1)
  59. end
  60.  
  61. -- Determines if rednet is enabled or not.
  62. function rEnabled()
  63.     local rColor
  64.       if rednet.isOpen("bottom") then
  65.          rColor = colors.lime
  66.       else
  67.          rColor = colors.red
  68.          rednet.open("bottom")
  69.       end
  70.     writeTo(24,12,rColor,colors.white,"Rednet")
  71. end
  72.  
  73.  
  74. -- Draws main menu.
  75. function drawMain()
  76.     m.clear()
  77.     local mx,my = m.getSize()
  78.     centerPrint("Random List", colors.lime, colors.black, 1)
  79.     centerPrint(string.rep("=",x+2), colors.lime, colors.black,2)
  80.     writeTo(13,4,colors.black,colors.lime,"+")
  81.     m.write(string.rep("-",15))
  82.     writeTo(x,4,colors.black,colors.lime,"+")
  83.     m.setCursorPos(13,5)
  84.      for i = 5,10 do
  85.         m.write("|"..string.rep(" ",15).."|")
  86.         m.setCursorPos(13,i)
  87.      end
  88.     writeTo(13,10,colors.black,colors.lime,"+")
  89.     m.write(string.rep("-",15))
  90.     writeTo(x,10,colors.black,colors.lime,"+")
  91.     writeTo(14,5,colors.black,colors.lime,"Fuel:")
  92.     writeTo(14,6,colors.black,colors.gray, string.rep("-",15))
  93.     writeTo(14,7,colors.black,colors.lime,"Status:")
  94.     writeTo(14,8,colors.black,colors.gray, string.rep("-",15))
  95.     writeTo(14,9,colors.black,colors.lime,"ID:")
  96.     rEnabled()
  97.       for k,v in pairs(menu) do
  98.           writeTo(v.xmin, v.ymin, colors.black, colors.lime,k)
  99.       end
  100. end
  101.  
  102. -- Draws the buttons for turtles and the buttons
  103. function pageOne()
  104.   m.clear()
  105.   drawMain()
  106.     for k,v in pairs(turtle) do
  107.         if v.page == 1 then
  108.             writeTo(v.xmin,v.ymin,colors.black,colors.purple,k)
  109.         end
  110.     end
  111. end
  112.  
  113. --Draws page 2 of turtle info
  114. function pageTwo()
  115.   m.clear()
  116.   drawMain()
  117.     for k,v in pairs(turtle) do
  118.         if v.page == 2 then
  119.             writeTo(v.xmin, v.ymin, colors.black, colors.purple,k)
  120.         end
  121.     end
  122. end
  123.  
  124.  
  125. --Check for valid click
  126. function checkTouch()
  127.  local e,side,x,y = os.pullEvent("monitor_touch")
  128.   for k,v in pairs(turtle) do
  129.      if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
  130.             for i = 1,2 do
  131.                 v.active = not v.active
  132.                 toggleButton()
  133.                 sleep(0.2)
  134.             end
  135.             print(k)
  136.             print(x..":"..y)
  137.      end
  138.   end
  139.   --determines where you clicked for next/prev menu
  140.   for k,v in pairs(menu) do
  141.      if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
  142.           for i = 1,2 do
  143.               v.active = not v.active
  144.               toggleButton()
  145.               sleep(0.2)
  146.           end
  147.           print(k)
  148.           print(x..":"..y)
  149.             if k == "Next" then
  150.                pageTwo()
  151.                print(k)
  152.             elseif k == "Prev" then
  153.                pageOne()
  154.                print(k)
  155.             end
  156.      end
  157.   end
  158. end
  159.  
  160. --Toggles button colors
  161. function toggleButton()
  162.    local bColor
  163.    local tColor
  164.       for k,v in pairs(turtle) do
  165.         local on = v.active
  166.             if on then
  167.                 bColor = colors.purple
  168.                 tColor = colors.lime
  169.             else
  170.                 bColor = colors.black
  171.                 tColor = colors.purple
  172.             end
  173.             writeTo(v.xmin,v.ymin,bColor,tColor,k)
  174.      end
  175.      for k,v in pairs(menu) do
  176.          local on = v.active
  177.              if on then
  178.                  bColor = colors.lime
  179.                  tColor = colors.purple
  180.              else
  181.                  bColor = colors.black
  182.                  tColor = colors.lime
  183.              end
  184.              writeTo(v.xmin,v.ymin,bColor,tColor,k)
  185.     end
  186. end
  187.  
  188. while true do
  189.     drawMain()
  190.     pageOne()
  191.     checkTouch()
  192. end
Advertisement
Add Comment
Please, Sign In to add comment