Advertisement
Guest User

grinders

a guest
Mar 31st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2.  
  3. buttons = {}
  4.  
  5. function print()
  6.   mon.clear()
  7.   mon.setTextScale(1)
  8.  
  9.   mon.setCursorPos(22, 2)
  10.   mon.write("Grinders")
  11.  
  12.   mon.setCursorPos(9, 5)
  13.   mon.write("Left Grinder")
  14.  
  15.   mon.setCursorPos(30, 5)
  16.   mon.write("Right Grinder")
  17.  
  18.   printbutton("leftLight")
  19.   printbutton("rightLight")
  20. end
  21.  
  22. function newbutton(name, text, func, xmin, xmax, ymin, ymax)
  23.   buttons[name] = {}
  24.   buttons[name]["text"] = text
  25.   buttons[name]["func"] = func
  26.   buttons[name]["xmin"] = xmin
  27.   buttons[name]["xmax"] = xmax
  28.   buttons[name]["ymin"] = ymin
  29.   buttons[name]["ymax"] = ymax
  30.   buttons[name]["active"] = true
  31. end
  32.  
  33. function printbutton(name)
  34.   if (buttons[name]["active"]) then
  35.     mon.setBackgroundColor(colors.lime)
  36.   else
  37.     mon.setBackgroundColor(colors.red)
  38.   end
  39.  
  40.   for i = buttons[name]["ymin"], buttons[name]["ymax"], 1 do
  41.     mon.setCursorPos(buttons[name]["xmin"], i)
  42.  
  43.     for i = buttons[name]["xmin"], buttons[name]["xmax"], 1 do
  44.       mon.write(" ")
  45.     end
  46.   end
  47.  
  48.   xstart = buttons[name]["xmin"] + 1 + math.floor((buttons[name]["xmax"] - buttons[name]["xmin"] - string.len(buttons[name]["text"])) / 2)
  49.   ystart = buttons[name]["ymin"] + math.floor((buttons[name]["ymax"] - buttons[name]["ymin"]) / 2)
  50.  
  51.   mon.setCursorPos(xstart, ystart)
  52.   mon.write(buttons[name]["text"])
  53.  
  54.   mon.setBackgroundColor(colors.black)
  55. end
  56.  
  57. function getClick()
  58.   event, side, x, y = os.pullEvent("monitor_touch")
  59.  
  60.   for name, data in pairs(buttons) do
  61.     if y <= data["ymax"] and y >= data["ymin"] then
  62.       if x <= data["xmax"] and y >= data["xmin"] then
  63.         data["func"]()
  64.       end
  65.     end
  66.   end
  67.  
  68.   print()
  69. end
  70.  
  71. function lLight()
  72.   buttons["leftLight"]["active"] = not buttons["leftLight"]["active"]
  73. end
  74.  
  75. function rLight()
  76.   buttons["rightLight"]["active"] = not buttons["rightLight"]["active"]
  77. end
  78.  
  79. function update()
  80.   if buttons["leftLight"]["active"] == true and buttons["rightLight"]["active"] == true then
  81.     rs.setOutput("left", true)
  82.     rs.setOutput("back", true)
  83.   elseif buttons["leftLight"]["active"] == true and buttons["rightLight"]["active"] == false then
  84.     rs.setOutput("back", true)
  85.     rs.setOutput("left", false)
  86.   elseif buttons["rightLight"]["active"] == true and buttons["leftLight"]["active"] == false then
  87.     rs.setOutput("back", false)
  88.     rs.setOutput("left", true)
  89.   else
  90.     rs.setOutput("left", false)
  91.     rs.setOutput("back", false)
  92.   end
  93. end
  94.  
  95. buttons = {}
  96.  
  97. newbutton("leftLight", "Light", lLight, 9, 21, 7, 9)
  98. newbutton("rightLight", "Light", rLight, 30, 42, 7, 9)
  99.  
  100. while true do
  101.   print()
  102.   update()
  103.   getClick()
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement