Mathilde411

button

Jul 29th, 2020
1,595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. local buttons = {}
  2.  
  3. local isOn = false
  4. local mon = term
  5.  
  6. local function visualize()
  7.     while true do
  8.         mon.clear()
  9.         for _, b in pairs(buttons) do
  10.             if not b.isActivated then
  11.                 paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.deacColor)
  12.             elseif not b.isPressed then
  13.                 paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.baseColor)
  14.             else
  15.                 paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.pressedColor)
  16.             end
  17.         end
  18.         sleep(0)
  19.     end
  20. end
  21.  
  22. local function act()
  23.     while true do
  24.         event, side, x, y = os.pullEvent("monitor_touch")
  25.         for _, b in pairs(buttons) do
  26.             if b:isInside(x,y) then
  27.                 b:pressButton()
  28.             end
  29.         end
  30.     end
  31. end
  32.  
  33. function addButton(id, xmin, xmax, ymin, ymax, func, baseColor, deacColor, pressedColor)
  34.     baseColor = baseColor or colors.red
  35.     deacColor = deacColor or colors.lightGray
  36.     pressedColor = pressedColor or colors.green
  37.  
  38.     local b = {}
  39.     b.id = id
  40.     b.xmin = xmin
  41.     b.xmax = xmax
  42.     b.ymin = ymin
  43.     b.ymax = ymax
  44.     b.func = func
  45.     b.baseColor = baseColor
  46.     b.deacColor = deacColor
  47.     b.pressedColor = pressedColor
  48.     b.isActivated = true
  49.     b.isPressed = false
  50.  
  51.     b.colorButtonPress = function(self)
  52.         self.isPressed = true
  53.         sleep(0.1)
  54.         self.isPressed = false
  55.     end
  56.  
  57.     b.pressButton = function(self)
  58.         parrallel.waitForAll(self.colorButtonPress, self.func)
  59.     end
  60.  
  61.     b.isInside = function(self, x, y)
  62.         return x >= self.xmin and x <= self.xmax and y >= ymin and y <= ymax
  63.     end
  64.  
  65.     buttons[id] = b
  66.  
  67.     return b
  68. end
  69.  
  70. function removeButton(id)
  71.     buttons[id] = nil
  72. end
  73.  
  74. function removeAll()
  75.     buttons = {}
  76. end
  77.  
  78. function setMonitor(monitor)
  79.     mon = monitor
  80.     term.redirect(mon)
  81. end
  82.  
  83. local function run( routine )
  84.     parrallel.waitForAny(visualize, act, routine)
  85. end
Advertisement
Add Comment
Please, Sign In to add comment