LayZee

ComputerCraft Farmingpolles Controller

Aug 21st, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.01 KB | None | 0 0
  1. --Monitor connection and size
  2. local monitor, monitorWidth, monitorHeight
  3.  
  4. --Get connected monitor and its width + height
  5. function getMonitor ()
  6.   for _, side in ipairs(rs.getSides()) do
  7.     if peripheral.getType(side) == "monitor" then
  8.       print("Connected to monitor on ", side, " side")
  9.       monitor = peripheral.wrap(side)
  10.       monitorWidth, monitorHeight = monitor.getSize()
  11.       monitor.clear()
  12.       monitor.setCursorPos(1, 1)
  13.      
  14.       return
  15.     end
  16.   end
  17. end
  18.  
  19. getMonitor()
  20.  
  21. --Button dimensions
  22. local buttonWidth = 12
  23. local buttonHeight = 3
  24.  
  25. --Colors
  26. local guiColors = {
  27.   ['background'] = colors.white,
  28.   ['headerText'] = colors.black,
  29.   ['buttonText'] = colors.white,
  30.   ['buttonShadow'] = colors.gray,
  31.   ['inactiveButton'] = colors.red,
  32.   ['activeButton'] = colors.lime
  33. }
  34.  
  35. --Button actions--
  36. function drawHeader (text)
  37.   --Set background color
  38.   monitor.setBackgroundColor(guiColors['background'])
  39.  
  40.   --Set text color
  41.   monitor.setTextColor(guiColors['headerText'])
  42.  
  43.   --Reset cursor
  44.   monitor.setCursorPos(1, 1)
  45.  
  46.   --Delete current header
  47.   for i = 1, monitorWidth do
  48.     monitor.write(" ")
  49.   end
  50.  
  51.   --Set cursor
  52.   monitor.setCursorPos(17, 1)
  53.   --Write new header
  54.   monitor.write(text)
  55. end
  56.  
  57. function toggleSignal (side, color)
  58.   --Current bundled cable signal
  59.   outputColors = rs.getBundledOutput(side)
  60.  
  61.   --Test if color is currently on
  62.   if colors.test(outputColors, color) then
  63.     --If on, turn off
  64.     outputColors = colors.subtract(outputColors, color)
  65.   else
  66.     --If off, turn on
  67.     outputColors = colors.combine(outputColors, color)
  68.   end
  69.  
  70.   --Set new bundled cable outpout signal
  71.   rs.setBundledOutput(side, outputColors)
  72. end
  73.  
  74. --Define buttons
  75. local buttons = {
  76.   [1] = {
  77.     ['label'] = "Hvidt lys",
  78.     ['action'] = function()
  79.       toggleSignal("top", colors.white)
  80.     end,
  81.     ['inaction'] = function()
  82.       toggleSignal("top", colors.white)
  83.     end,
  84.     ['row'] = 2,
  85.     ['column'] = 1,
  86.     ['active'] = false,
  87.     ['toggleable'] = true
  88.   },
  89.   [2] = {
  90.     ['label'] = "Orange lys",
  91.     ['action'] = function()
  92.       toggleSignal("top", colors.orange)
  93.     end,
  94.     ['inaction'] = function()
  95.       toggleSignal("top", colors.orange)
  96.     end,
  97.     ['row'] = 2,
  98.     ['column'] = 3,
  99.     ['active'] = false,
  100.     ['toggleable'] = false
  101.   }--[[,
  102.   [3] = {
  103.     ['label'] = "Label 3",
  104.     ['action'] = function()
  105.       drawHeader("Button 3 pressed")
  106.     end,
  107.     ['row'] = 1,
  108.     ['column'] = 3,
  109.     ['active'] = false,
  110.     ['toggleable'] = false
  111.   },
  112.   [4] = {
  113.     ['label'] = "Label 4",
  114.     ['action'] = function()
  115.       drawHeader("Button 4 pressed")
  116.     end,
  117.     ['row'] = 2,
  118.     ['column'] = 1,
  119.     ['active'] = false,
  120.     ['toggleable'] = false
  121.   },
  122.   [5] = {
  123.     ['label'] = "Label 5",
  124.     ['action'] = function()
  125.       drawHeader("Button 5 pressed")
  126.     end,
  127.     ['row'] = 2,
  128.     ['column'] = 2,
  129.     ['active'] = false,
  130.     ['toggleable'] = false
  131.   },
  132.   [6] = {
  133.     ['label'] = "Label 6",
  134.     ['action'] = function()
  135.       drawHeader("Button 6 pressed")
  136.     end,
  137.     ['row'] = 2,
  138.     ['column'] = 3,
  139.     ['active'] = false,
  140.     ['toggleable'] = false
  141.   },
  142.   [7] = {
  143.     ['label'] = "Label 7",
  144.     ['action'] = function()
  145.       drawHeader("Button 7 pressed")
  146.     end,
  147.     ['row'] = 3,
  148.     ['column'] = 1,
  149.     ['active'] = false,
  150.     ['toggleable'] = false
  151.   },
  152.   [8] = {
  153.     ['label'] = "Label 8",
  154.     ['action'] = function()
  155.       drawHeader("Button 8 pressed")
  156.     end,
  157.     ['row'] = 3,
  158.     ['column'] = 2,
  159.     ['active'] = false,
  160.     ['toggleable'] = false
  161.   },
  162.   [9] = {
  163.     ['label'] = "Label 9",
  164.     ['action'] = function()
  165.       drawHeader("Button 9 pressed")
  166.     end,
  167.     ['row'] = 3,
  168.     ['column'] = 3,
  169.     ['active'] = false,
  170.     ['toggleable'] = false
  171.   },
  172.   [10] = {
  173.     ['label'] = "Label 10",
  174.     ['action'] = function()
  175.       drawHeader("Button 10 pressed")
  176.     end,
  177.     ['row'] = 4,
  178.     ['column'] = 1,
  179.     ['active'] = false,
  180.     ['toggleable'] = false
  181.   },
  182.   [11] = {
  183.     ['label'] = "Label 11",
  184.     ['action'] = function()
  185.       drawHeader("Button 11 pressed")
  186.     end,
  187.     ['row'] = 4,
  188.     ['column'] = 2,
  189.     ['active'] = false,
  190.     ['toggleable'] = false
  191.   },
  192.   [12] = {
  193.     ['label'] = "Label 12",
  194.     ['action'] = function()
  195.       drawHeader("Button 12 pressed")
  196.     end,
  197.     ['row'] = 4,
  198.     ['column'] = 3,
  199.     ['active'] = false,
  200.     ['toggleable'] = false
  201.   },
  202.   [13] = {
  203.     ['label'] = "Label 13",
  204.     ['action'] = function()
  205.       drawHeader("Button 13 pressed")
  206.     end,
  207.     ['row'] = 5,
  208.     ['column'] = 1,
  209.     ['active'] = false,
  210.     ['toggleable'] = false
  211.   },
  212.   [14] = {
  213.     ['label'] = "Label 14",
  214.     ['action'] = function()
  215.       drawHeader("Button 14 pressed")
  216.     end,
  217.     ['row'] = 5,
  218.     ['column'] = 2,
  219.     ['active'] = false,
  220.     ['toggleable'] = false
  221.   },
  222.   [15] = {
  223.     ['label'] = "Label 15",
  224.     ['action'] = function()
  225.       drawHeader("Button 15 pressed")
  226.     end,
  227.     ['row'] = 5,
  228.     ['column'] = 3,
  229.     ['active'] = false,
  230.     ['toggleable'] = false
  231.   }
  232.   ]]--
  233. }
  234.  
  235. --Define button coordinates
  236. local columns = {
  237.   [1] = {
  238.     ['x1'] = 3,
  239.     ['x2'] = 15
  240.   },
  241.   [2] = {
  242.     ['x1'] = 19,
  243.     ['x2'] = 31
  244.   },
  245.   [3] = {
  246.     ['x1'] = 35,
  247.     ['x2'] = 47
  248.   }
  249. }
  250.  
  251. local rows = {
  252.   [1] = {
  253.     ['y1'] = 2,
  254.     ['y2'] = 5
  255.   },
  256.   [2] = {
  257.     ['y1'] = 7,
  258.     ['y2'] = 10
  259.   },
  260.   [3] = {
  261.     ['y1'] = 12,
  262.     ['y2'] = 15
  263.   },
  264.   [4] = {
  265.     ['y1'] = 17,
  266.     ['y2'] = 20
  267.   },
  268.   [5] = {
  269.     ['y1'] = 22,
  270.     ['y2'] = 25
  271.   }
  272. }
  273.  
  274. --Draw a filled box for e.g. a button or shadow
  275. function drawBox (x, y, width, height, color)
  276.   monitor.setBackgroundColor(color)
  277.   monitor.setTextColor(color)
  278.  
  279.   for i = 0, height - 1 do
  280.     monitor.setCursorPos(x, y + i)
  281.  
  282.     for j = 1, width do
  283.       monitor.write(" ")
  284.     end
  285.   end
  286. end
  287.  
  288. function drawBackground (color)
  289.   monitor.setBackgroundColor(guiColors['background'])
  290.   monitor.clear()
  291. end
  292.  
  293. function drawLabel (x, y, label, color)
  294.   monitor.setTextColor(color)
  295.   monitor.setCursorPos(x, y)
  296.   monitor.write(label)
  297. end
  298.  
  299. function drawInactiveButton (x, y, width, height, label)
  300.   --Draw button shadow
  301.   drawBox(
  302.     x + 1, y + 1, width, height,
  303.     guiColors['buttonShadow']
  304.   )
  305.  
  306.   --Draw button box
  307.   drawBox(
  308.     x, y, width, height,
  309.     guiColors['inactiveButton']
  310.   )
  311.  
  312.   --Draw button label
  313.   drawLabel(
  314.     (x + 1), (y + 1), label,
  315.     guiColors['buttonText']
  316.   )
  317. end
  318.  
  319. function drawActiveButton (x, y, width, height, label)
  320.   --Clear button area
  321.   drawBox(
  322.     x, y, width + 1, height + 1,
  323.     guiColors['background']
  324.   )
  325.  
  326.   --Draw indented button box
  327.   drawBox(
  328.     x + 1, y + 1, width, height,
  329.     guiColors['activeButton']
  330.   )
  331.  
  332.   --Draw button label
  333.   drawLabel(
  334.     x + 2, y + 2, label,
  335.     guiColors['buttonText']
  336.   )
  337. end
  338.  
  339. function renderButtons ()
  340.   for index, button in ipairs(buttons) do
  341.     x1 = columns[button['column']]['x1']
  342.     x2 = columns[button['column']]['x2']
  343.     y1 = rows[button['row']]['y1']
  344.     y2 = rows[button['row']]['y2']
  345.  
  346.     width = x2 - x1
  347.     height = y2 - y1
  348.  
  349.     if button['active'] then
  350.       drawActiveButton(x1, y1, width, height, button['label'])
  351.     else
  352.       drawInactiveButton(x1, y1, width, height, button['label'])
  353.     end
  354.   end
  355. end
  356.  
  357. function pushButton (button)
  358.   x1 = columns[button['column']]['x1']
  359.   x2 = columns[button['column']]['x2']
  360.   y1 = rows[button['row']]['y1']
  361.   y2 = rows[button['row']]['y2']
  362.  
  363.   width = x2 - x1
  364.   height = y2 - y1
  365.  
  366.   drawActiveButton(x1, y1, width, height, button['label'])
  367.  
  368.   --Activate button state
  369.   button['active'] = true
  370.   buttonAction(button)
  371.  
  372.   sleep(0.2)
  373.  
  374.   --Deactivate button state
  375.   button['active'] = false
  376.   buttonAction(button)
  377.  
  378.   drawInactiveButton(x1, y1, width, height, button['label'])
  379. end
  380.  
  381. function buttonAction (button)
  382.   --Check if button was just activated
  383.   if button['active'] then
  384.     if button['action'] then
  385.       --Call button activation function
  386.       button['action']()
  387.     end
  388.   else
  389.     if button['inaction'] then
  390.       --Call button deactivation function
  391.       button['inaction']()
  392.     end
  393.   end
  394. end
  395.  
  396. function touchButton (x, y)
  397.   for index, button in ipairs(buttons) do
  398.     x1 = columns[button['column']]['x1']
  399.     x2 = columns[button['column']]['x2']
  400.     y1 = rows[button['row']]['y1']
  401.     y2 = rows[button['row']]['y2']
  402.  
  403.     width = x2 - x1
  404.     height = y2 - y1
  405.  
  406.     if x >= x1 and x <= x2 and y >= y1 and y<= y2 then
  407.       if button['toggleable'] then
  408.         --Toggle button active state
  409.         button['active'] = not button['active']
  410.  
  411.         --Redraw all buttons
  412.         renderButtons()
  413.  
  414.         --Do action/inaction
  415.         buttonAction(button)
  416.       else
  417.         pushButton(button)
  418.       end
  419.  
  420.       return
  421.     end
  422.   end
  423. end
  424.  
  425. function touchLoop()
  426.   while true do
  427.     local event, side, x, y = os.pullEvent("monitor_touch")
  428.  
  429.     touchButton(x, y)
  430.   end
  431. end
  432.  
  433. --Main program
  434. drawBackground()
  435. print("Rendering...")
  436. renderButtons()
  437. print("Complete!")
  438.  
  439. touchLoop()
Advertisement
Add Comment
Please, Sign In to add comment