Advertisement
Pirnogion

OC/Button-test

Sep 17th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. local computer = require "computer"
  2. local unicode = require "unicode"
  3.  
  4. local rect = require "rectangle"
  5. local s = require "serialization"
  6.  
  7. --CONST--
  8. local STATUS_ENABLED = 0
  9. local STATUS_DISABLED = 5
  10. local STATUS_BLINK = 10
  11.  
  12. --UTILS--
  13. local function alignTextToCenterRect(rectangle, text)
  14.     local x = rectangle.sx + rectangle.width/2 - unicode.len(text)/2
  15.     local y = rectangle.sy + rectangle.height/2
  16.  
  17.     return x, y
  18. end
  19.  
  20. --Default settings
  21. local default_design = {
  22.     blink_time = 0.2,
  23.  
  24.     [STATUS_ENABLED] = {
  25.         bg = 0x00ff00,
  26.         fg = 0xffffff,
  27.         char = ' '
  28.     },
  29.  
  30.     [STATUS_DISABLED] = {
  31.         bg = 0xffffff,
  32.         fg = 0,
  33.         char = ' '
  34.     },
  35.  
  36.     [STATUS_BLINK] = {
  37.         bg = 0xff0000,
  38.         fg = 0,
  39.         char = ' '
  40.     }
  41. }
  42.  
  43. local default_text = ' '
  44.  
  45. local default_action = function()
  46.     --nothing
  47. end
  48.  
  49. --BUTTON API--
  50. local buttonAPI = {}
  51.  
  52. local function handler(button)
  53.     local event = require "event"
  54.  
  55.     while true do
  56.         local e = { event.pull() }
  57.         if ( e[2] == "touch" and e[6] == 0 ) then
  58.             if ( button.status == STATUS_ENABLED ) then
  59.                 if ( rect.PointInRect(e[4], e[5], button.rectangle) ) then
  60.                     button.action()
  61.  
  62.                     button.status = STATUS_BLINK
  63.                     button:redraw()
  64.                     os.sleep(button.design.blink_time)
  65.                     button.status = STATUS_ENABLED
  66.                     button:redraw()
  67.                 end
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. --Inizialize multithread button hadlers
  74. computer.singleThread = computer.pullSignal
  75. local mainThread = nil
  76.  
  77. local button_handers = {}
  78. local function handleButtons( timeout )
  79.     if ( mainThread == coroutine.running() ) then
  80.         local _timeout = timeout or math.huge
  81.         local event = { computer.singleThread( _timeout ) }
  82.         coroutine.resume( button_handers[1][2], button_handers[1][1], table.unpack(event) )
  83.         coroutine.resume( button_handers[2][2], button_handers[2][1], table.unpack(event) )
  84.         return table.unpack(event)
  85.     else
  86.         return coroutine.yield( timeout )
  87.     end
  88. end
  89.  
  90. function buttonAPI.create(gpu, rectangle, text, action, design, status)
  91.     button = {}
  92.  
  93.     button.gpu       = gpu
  94.     button.rectangle = rectangle
  95.     button.design    = design or default_design
  96.     button.text      = text or default_text
  97.     button.action    = action or default_action
  98.     button.status    = status or STATUS_ENABLED
  99.  
  100.     table.insert( button_handers, { [1] = button, [2] = coroutine.create( handler ) } )
  101.  
  102.     function button:redraw()
  103.         self.gpu.setBackground( self.design[self.status].bg )
  104.         self.gpu.setForeground( self.design[self.status].fg )
  105.         self.gpu.fill( self.rectangle.sx, self.rectangle.sy, self.rectangle.width, self.rectangle.height, self.design[self.status].char )
  106.  
  107.         local textX, textY = alignTextToCenterRect(self.rectangle, self.text)
  108.         self.gpu.set( textX, textY, self.text )
  109.     end
  110.  
  111.     return button
  112. end
  113.  
  114. function buttonAPI.handle()
  115.     --Start multithread
  116.     mainThread = coroutine.running()
  117.     computer.pullSignal = handleButtons
  118.  
  119.     while true do
  120.         handleButtons()
  121.     end
  122. end
  123.  
  124. return buttonAPI
  125. --pastebin -f get J5jHxja6 lib/button.lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement