Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require "computer"
- local unicode = require "unicode"
- local rect = require "rectangle"
- local s = require "serialization"
- --CONST--
- local STATUS_ENABLED = 0
- local STATUS_DISABLED = 5
- local STATUS_BLINK = 10
- --UTILS--
- local function alignTextToCenterRect(rectangle, text)
- local x = rectangle.sx + rectangle.width/2 - unicode.len(text)/2
- local y = rectangle.sy + rectangle.height/2
- return x, y
- end
- --Default settings
- local default_design = {
- blink_time = 0.2,
- [STATUS_ENABLED] = {
- bg = 0x00ff00,
- fg = 0xffffff,
- char = ' '
- },
- [STATUS_DISABLED] = {
- bg = 0xffffff,
- fg = 0,
- char = ' '
- },
- [STATUS_BLINK] = {
- bg = 0xff0000,
- fg = 0,
- char = ' '
- }
- }
- local default_text = ' '
- local default_action = function()
- --nothing
- end
- --BUTTON API--
- local buttonAPI = {}
- local function handler(button)
- local event = require "event"
- while true do
- local e = { event.pull() }
- if ( e[2] == "touch" and e[6] == 0 ) then
- if ( button.status == STATUS_ENABLED ) then
- if ( rect.PointInRect(e[4], e[5], button.rectangle) ) then
- button.action()
- button.status = STATUS_BLINK
- button:redraw()
- os.sleep(button.design.blink_time)
- button.status = STATUS_ENABLED
- button:redraw()
- end
- end
- end
- end
- end
- --Inizialize multithread button hadlers
- computer.singleThread = computer.pullSignal
- local mainThread = nil
- local button_handers = {}
- local function handleButtons( timeout )
- if ( mainThread == coroutine.running() ) then
- local _timeout = timeout or math.huge
- local event = { computer.singleThread( _timeout ) }
- coroutine.resume( button_handers[1][2], button_handers[1][1], table.unpack(event) )
- coroutine.resume( button_handers[2][2], button_handers[2][1], table.unpack(event) )
- return table.unpack(event)
- else
- return coroutine.yield( timeout )
- end
- end
- function buttonAPI.create(gpu, rectangle, text, action, design, status)
- button = {}
- button.gpu = gpu
- button.rectangle = rectangle
- button.design = design or default_design
- button.text = text or default_text
- button.action = action or default_action
- button.status = status or STATUS_ENABLED
- table.insert( button_handers, { [1] = button, [2] = coroutine.create( handler ) } )
- function button:redraw()
- self.gpu.setBackground( self.design[self.status].bg )
- self.gpu.setForeground( self.design[self.status].fg )
- self.gpu.fill( self.rectangle.sx, self.rectangle.sy, self.rectangle.width, self.rectangle.height, self.design[self.status].char )
- local textX, textY = alignTextToCenterRect(self.rectangle, self.text)
- self.gpu.set( textX, textY, self.text )
- end
- return button
- end
- function buttonAPI.handle()
- --Start multithread
- mainThread = coroutine.running()
- computer.pullSignal = handleButtons
- while true do
- handleButtons()
- end
- end
- return buttonAPI
- --pastebin -f get J5jHxja6 lib/button.lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement