Demos_

buttonapi

Dec 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.60 KB | None | 0 0
  1. --Button api for computercraft by Siarko
  2. --Usage:
  3. --
  4. -- FIRST SET MONITOR HANDLE BY
  5. -- button.setMonitor(handle)
  6. --
  7. -- new button object creation:
  8. --  myFancyButton = button.create([label])
  9. -- Methods:
  10. -- setText, setPos, setSize, setColor, setBlinkColor, setActive, onClick
  11. -- return button object so they can be chained eg:
  12. -- myButton = button.create("My button").setSize(5,5).setColor(colors.gray).onClick(function)
  13. -- setText(string, resize-bool) - sets button label; if resize then auto changes size to fit label
  14. -- setSize(int, int), setPos(int, int) - quite self explainatory ;)
  15. -- setColor(color) - sets background color, parameter color - computercraft colors api
  16. -- setBlinkColor(color) - sets background to be drawn while button click
  17. -- setActive(bool) - is button active and can be clicked? inactive - gray background
  18. -- onClick(function) - function called when button is clicked
  19. -- onClickReturn(value) - "await" function returns value instead of executing callback,
  20. --   returns nil if no button was clicked
  21. -- setAlign("left" / "center" / "right") - aligns label in button accordingly;
  22. --   if label doesn't fit, it will be cut to fit, min button width = 4
  23.  
  24. --Activate waiting for click:
  25. -- button.await(b1, b2, b3, ...)
  26. -- or button.await(buttonArray)
  27. -- b1, b2, b3, - single button objects
  28. -- buttonArray - table containing buttons eg:
  29. -- arr = {b1, b2, b3}
  30. -- "await" function draws supplied buttons and waits for click, if any button is clicked
  31. -- fires callback function set in button by "onClick()" method
  32. -- probably should be called in loop
  33.  
  34. -- If you want to do something in the background you have to run "button.await" in another thread
  35. -- like parallel.waitForAny(background, buttonAwaitFunction)
  36.  
  37. local _locales = {
  38.   mon = nil
  39. }
  40.  
  41. --button class
  42. function create(text)
  43.   local this = {
  44.     x = 1,
  45.     y = 1,
  46.     w = 0,
  47.     h = 1,
  48.     text = tostring(text) or "None",
  49.     bgcol = colors.lime,
  50.     blinkCol = colors.red,
  51.     align = "c",
  52.     active = true,
  53.     callback = nil,
  54.     ret = nil
  55.   }
  56.  
  57.   if text ~= nil then
  58.     this.w = #tostring(text)
  59.   end
  60.  
  61.   --setters, return this object
  62.   function this.setText(text, resize)
  63.     this.text = tostring(text)
  64.     if resize and this.w < #this.text then
  65.       this.w = #this.text
  66.     end
  67.     return this
  68.   end
  69.  
  70.   function this.setAlign(align)
  71.     if align == "center" then
  72.       this.align = "c"
  73.     elseif align == "left" then
  74.       this.align = "l"
  75.     elseif align == "right" then
  76.       this.align = "r"
  77.     else
  78.       print("Incorrect slign set! ")
  79.       error()
  80.     end
  81.     return this
  82.   end
  83.  
  84.   function this.setPos(x, y)
  85.     this.x = x
  86.     this.y = y
  87.     return this
  88.   end
  89.  
  90.   function this.setSize(w, h)
  91.     this.w = w
  92.     this.h = h
  93.     return this
  94.   end
  95.  
  96.   function this.setColor(color)
  97.     this.bgcol = color
  98.     return this
  99.   end
  100.  
  101.   function this.setBlinkColor(color)
  102.     this.blinkCol = color
  103.     return this
  104.   end
  105.  
  106.   function this.setActive(state)
  107.     this.active = state
  108.     return this
  109.   end
  110.  
  111.   function this.wasClicked(x,y)
  112.     if
  113.       x >= this.x and
  114.       x < this.x+this.w and
  115.       y >= this.y and
  116.       y < this.y+this.h and
  117.       this.active
  118.     then
  119.       return true
  120.     end
  121.     return false
  122.   end
  123.  
  124.   function this.onClick(callback)
  125.     this.callback = callback
  126.     return this
  127.   end
  128.  
  129.   function this.onClickReturn(value)
  130.     this.ret = value
  131.     return this
  132.   end
  133.  
  134.   function this.fireEvent()
  135.     if this.callback ~= nil then
  136.       this.callback()
  137.     end
  138.   end
  139.  
  140.   function this.drawWrapper(bgcol)
  141.     if _locales.mon == nil then
  142.       print("Monitor not set!")
  143.       error()
  144.     end
  145.     local xpos = this.x+(this.w/2-#this.text/2)
  146.     local t = this.text
  147.     local bg = _locales.mon.getBackgroundColor()
  148.     local tc = _locales.mon.getTextColor()
  149.     if this.align == "l" then
  150.       xpos = this.x
  151.     end
  152.     if this.align == "r" then
  153.       xpos = this.x+this.w-#this.text
  154.     end
  155.     if #this.text > this.w then
  156.         xpos = this.x
  157.         t = string.sub(t,1,this.w-3)..".."..string.sub(t,-1)
  158.     end
  159.     _locales.mon.setTextColor(colors.black)
  160.     local f = string.rep(" ", this.w)
  161.     if this.active then
  162.       _locales.mon.setBackgroundColor(bgcol)
  163.     else
  164.       _locales.mon.setBackgroundColor(colors.gray)
  165.     end
  166.     for i = 1, this.h do
  167.       _locales.mon.setCursorPos(this.x, this.y+(i-1))
  168.       _locales.mon.write(f)
  169.     end
  170.     _locales.mon.setCursorPos(xpos,this.y+this.h/2)
  171.     _locales.mon.write(t)
  172.     _locales.mon.setBackgroundColor(bg)
  173.     _locales.mon.setTextColor(tc)
  174.   end
  175.  
  176.   function this.draw()
  177.     this.drawWrapper(this.bgcol)
  178.   end
  179.  
  180.   function this.blink()
  181.     this.drawWrapper(this.blinkCol)
  182.     sleep(0.2)
  183.     this.draw()
  184.   end
  185.    
  186.   return this
  187. end
  188.  
  189. --set Monitor handle to draw on
  190. function setMonitor(mon)
  191.   _locales.mon = mon
  192.   --MON = mon
  193. end
  194.  
  195. function clearMon()
  196.   _locales.mon.clear()
  197. end
  198.  
  199. local function isTable(element)
  200.   return type(element) == "table"
  201. end
  202.  
  203. local function isButton(element)
  204.   if isTable(element) and element.text ~= nil then
  205.     return true
  206.   end
  207.   return false
  208. end
  209.  
  210. local function mergeTables(tab1, tab2)
  211.   for i in pairs(tab2) do
  212.     tab1[#tab1+1] = tab2[i]
  213.   end
  214. end
  215.  
  216. --manage button checks
  217. function await(...)
  218.   array = {}
  219.   for i in pairs(arg) do
  220.     if i ~= "n" then
  221.       if isTable(arg[i]) and not isButton(arg[i]) then --table of buttons
  222.         mergeTables(array, arg[i])
  223.       else --single button
  224.         array[#array+1] = arg[i]
  225.       end
  226.     end
  227.   end
  228.  
  229.   for i in pairs(array) do
  230.     array[i].draw()
  231.   end
  232.   e, s, x, y = os.pullEvent("monitor_touch")
  233.   for i in pairs(array) do
  234.     if array[i].wasClicked(x,y) then
  235.       array[i].blink()
  236.       if array[i].ret ~= nil then
  237.         return array[i].ret
  238.       end
  239.       array[i].fireEvent()
  240.     end
  241.   end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment