Kingdaro

Untitled

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