siulsalas

monitor_test

Jan 19th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. -- https://pastebin.com/D8yWJ7rh
  2. peripheral.wrap("top").setTextScale(3)
  3. function newManager(side)
  4.   manager = {
  5.     buttons = {},
  6.     labels = {},
  7.     monitor = peripheral.wrap(side),
  8.     newButton = function(self, label)
  9.       print("creating button ''"..label.."''")
  10.       self.labels[#self.labels + 1] = label
  11.       self.buttons[label] = {
  12.         label = label,
  13.         text = "[ ] "..label,
  14.         active = false,
  15.         row = 0
  16.       }
  17.     end,
  18.     draw = function(self)
  19.       print("starting draw()")
  20.       self.monitor.clear()
  21.       print("cleared monitor")
  22.       self.monitor.setCursorPos(1,2)
  23.       print("set cursor position")
  24.       local yPos = 2
  25.       print("starting loop: "..#self.labels)
  26.       print(self.buttons["Iron"].label)
  27.       for i = 1, #self.labels do
  28.         local button = self.buttons[self.labels[i]]
  29.         print("working on button "..button.label)
  30.         self.monitor.write(button.text)
  31.         button.row =  yPos
  32.         yPos = yPos + 1
  33.         self.monitor.setCursorPos(1,yPos)
  34.       end
  35.     end,
  36.     run = function(self)
  37.       while true do
  38.         local event, side, x, y = os.pullEvent( "monitor_touch" )
  39.         print(event)
  40.         print(side)
  41.         for i = 1, #self.labels do
  42.           local button = self.buttons[self.labels[i]]
  43.           if button.row == y then
  44.             print("found row!")
  45.             button.active = not button.active
  46.             break
  47.           end
  48.         end
  49.         for i = 1, #self.labels do
  50.           local button = self.buttons[self.labels[i]]
  51.           if button.active then
  52.             button.text =  "[X] "..button.label
  53.           else
  54.             button.text =  "[ ] "..button.label
  55.           end
  56.         end
  57.         self:draw()
  58.       end
  59.     end
  60.   }
  61.   return manager
  62. end
  63.  
  64.  
  65. local mgr = newManager("top")
  66. mgr:newButton("Iron")
  67. mgr:newButton("Dye")
  68. mgr:newButton("Coal")
  69. mgr:draw()
  70. mgr:run()
Add Comment
Please, Sign In to add comment