Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local buttons = {}
- local isOn = false
- local mon = term
- local function visualize()
- while true do
- mon.clear()
- for _, b in pairs(buttons) do
- if not b.isActivated then
- paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.deacColor)
- elseif not b.isPressed then
- paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.baseColor)
- else
- paintutils.drawFilledBox(b.xmin, b.xmax, b.ymin, b.ymax, b.pressedColor)
- end
- end
- sleep(0)
- end
- end
- local function act()
- while true do
- event, side, x, y = os.pullEvent("monitor_touch")
- for _, b in pairs(buttons) do
- if b:isInside(x,y) then
- b:pressButton()
- end
- end
- end
- end
- function addButton(id, xmin, xmax, ymin, ymax, func, baseColor, deacColor, pressedColor)
- baseColor = baseColor or colors.red
- deacColor = deacColor or colors.lightGray
- pressedColor = pressedColor or colors.green
- local b = {}
- b.id = id
- b.xmin = xmin
- b.xmax = xmax
- b.ymin = ymin
- b.ymax = ymax
- b.func = func
- b.baseColor = baseColor
- b.deacColor = deacColor
- b.pressedColor = pressedColor
- b.isActivated = true
- b.isPressed = false
- b.colorButtonPress = function(self)
- self.isPressed = true
- sleep(0.1)
- self.isPressed = false
- end
- b.pressButton = function(self)
- parrallel.waitForAll(self.colorButtonPress, self.func)
- end
- b.isInside = function(self, x, y)
- return x >= self.xmin and x <= self.xmax and y >= ymin and y <= ymax
- end
- buttons[id] = b
- return b
- end
- function removeButton(id)
- buttons[id] = nil
- end
- function removeAll()
- buttons = {}
- end
- function setMonitor(monitor)
- mon = monitor
- term.redirect(mon)
- end
- local function run( routine )
- parrallel.waitForAny(visualize, act, routine)
- end
Advertisement
Add Comment
Please, Sign In to add comment