Advertisement
SL0RD

Untitled

Jun 13th, 2021
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 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.         textColor = colors.black
  56.     }
  57.  
  58.     if text ~= nil then
  59.         this.w = #tostring(text)
  60.     end
  61.  
  62.     --setters, return this object
  63.     function this.setText(text, resize)
  64.         this.text = tostring(text)
  65.         if resize and this.w < #this.text then
  66.             this.w = #this.text
  67.         end
  68.         return this
  69.     end
  70.  
  71.     function this.setTextColor(color)
  72.         this.textColor = color
  73.         return this
  74.     end
  75.  
  76.     function this.setAlign(align)
  77.         if align == "center" then
  78.             this.align = "c"
  79.         elseif align == "left" then
  80.             this.align = "l"
  81.         elseif align == "right" then
  82.             this.align = "r"
  83.         else
  84.             print("Incorrect slign set! ")
  85.             error()
  86.         end
  87.         return this
  88.     end
  89.  
  90.     function this.setPos(x, y)
  91.         this.x = x
  92.         this.y = y
  93.         return this
  94.     end
  95.  
  96.     function this.setSize(w, h)
  97.         this.w = w
  98.         this.h = h
  99.         return this
  100.     end
  101.  
  102.     function this.setColor(color)
  103.         this.bgcol = color
  104.         return this
  105.     end
  106.  
  107.     function this.setBlinkColor(color)
  108.         this.blinkCol = color
  109.         return this
  110.     end
  111.  
  112.     function this.setActive(state)
  113.         this.active = state
  114.         return this
  115.     end
  116.  
  117.     function this.wasClicked(x,y)
  118.         if
  119.             x >= this.x and
  120.             x < this.x+this.w and
  121.             y >= this.y and
  122.             y < this.y+this.h and
  123.             this.active
  124.         then
  125.             return true
  126.         end
  127.         return false
  128.     end
  129.  
  130.     function this.onClick(callback)
  131.         this.callback = callback
  132.         return this
  133.     end
  134.  
  135.     function this.onClickReturn(value)
  136.         this.ret = value
  137.         return this
  138.     end
  139.  
  140.     function this.fireEvent()
  141.         if this.callback ~= nil then
  142.             this.callback()
  143.         end
  144.     end
  145.  
  146.     function this.drawWrapper(bgcol)
  147.         if _locales.mon == nil then
  148.             print("Monitor not set!")
  149.             error()
  150.         end
  151.         local xpos = this.x+(this.w/2-#this.text/2)
  152.         local t = this.text
  153.         local bg = _locales.mon.getBackgroundColor()
  154.         local tc = _locales.mon.getTextColor()
  155.         if this.align == "l" then
  156.             xpos = this.x
  157.         end
  158.         if this.align == "r" then
  159.             xpos = this.x+this.w-#this.text
  160.         end
  161.         if #this.text > this.w then
  162.             xpos = this.x
  163.             t = string.sub(t,1,this.w-3)..".."..string.sub(t,-1)
  164.         end
  165.         _locales.mon.setTextColor(this.textColor)
  166.         local f = string.rep(" ", this.w)
  167.         if this.active then
  168.             _locales.mon.setBackgroundColor(bgcol)
  169.         else
  170.             _locales.mon.setBackgroundColor(colors.gray)
  171.         end
  172.         for i = 1, this.h do
  173.             _locales.mon.setCursorPos(this.x, this.y+(i-1))
  174.             _locales.mon.write(f)
  175.         end
  176.         _locales.mon.setCursorPos(xpos,this.y+this.h/2)
  177.         _locales.mon.write(t)
  178.         _locales.mon.setBackgroundColor(bg)
  179.         _locales.mon.setTextColor(tc)
  180.     end
  181.  
  182.     function this.draw()
  183.         this.drawWrapper(this.bgcol)
  184.     end
  185.  
  186.     function this.blink()
  187.         this.drawWrapper(this.blinkCol)
  188.         sleep(0.2)
  189.         this.draw()
  190.     end
  191.  
  192.     return this
  193. end
  194.  
  195. --set Monitor handle to draw on
  196. function setMonitor(mon)
  197.     _locales.mon = mon
  198.     --MON = mon
  199. end
  200.  
  201. function clearMon()
  202.     _locales.mon.clear()
  203. end
  204.  
  205. local function isTable(element)
  206.     return type(element) == "table"
  207. end
  208.  
  209. local function isButton(element)
  210.     if isTable(element) and element.text ~= nil then
  211.         return true
  212.     end
  213.     return false
  214. end
  215.  
  216. local function mergeTables(tab1, tab2)
  217.     for i in pairs(tab2) do
  218.         tab1[#tab1+1] = tab2[i]
  219.     end
  220. end
  221.  
  222. --manage button checks
  223. function await(...)
  224.     array = {}
  225.     for i in pairs(arg) do
  226.         if i ~= "n" then
  227.             if isTable(arg[i]) and not isButton(arg[i]) then --table of buttons
  228.                 mergeTables(array, arg[i])
  229.             else --single button
  230.                 array[#array+1] = arg[i]
  231.             end
  232.         end
  233.     end
  234.  
  235.     for i in pairs(array) do
  236.             array[i].draw()
  237.     end
  238.     e, s, x, y = os.pullEvent("monitor_touch")
  239.     for i in pairs(array) do
  240.         if array[i].wasClicked(x,y) then
  241.             array[i].blink()
  242.             if array[i].ret ~= nil then
  243.                 return array[i].ret
  244.             end
  245.             array[i].fireEvent()
  246.         end
  247.     end
  248. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement