Wardes

buttonApi

Apr 21st, 2021 (edited)
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local _locales = {
  2.     mon = nil
  3. }
  4.  
  5. --button class
  6. function create(text)
  7.     local this = {
  8.         x = 1,
  9.         y = 1,
  10.         w = 0,
  11.         h = 1,
  12.         text = tostring(text) or "None",
  13.         defaultColor = colors.red,
  14.         activeColor = colors.green,
  15.         align = "c",
  16.     state = false,
  17.         disable = false,
  18.         callback = nil,
  19.         textColor = colors.black
  20.     }
  21.  
  22.     if text ~= nil then
  23.         this.w = #tostring(text)
  24.     end
  25.  
  26.     --setters, return this object
  27.     function this.setText(text, resize)
  28.         this.text = tostring(text)
  29.         if resize and this.w < #this.text then
  30.             this.w = #this.text
  31.         end
  32.         return this
  33.     end
  34.  
  35.     function this.setTextColor(color)
  36.         this.textColor = color
  37.         return this
  38.     end
  39.  
  40.     function this.setAlign(align)
  41.         if align == "center" then
  42.             this.align = "c"
  43.         elseif align == "left" then
  44.             this.align = "l"
  45.         elseif align == "right" then
  46.             this.align = "r"
  47.         else
  48.             print("Incorrect slign set! ")
  49.             error()
  50.         end
  51.         return this
  52.     end
  53.  
  54.     function this.setPos(x, y)
  55.         this.x = x
  56.         this.y = y
  57.         return this
  58.     end
  59.  
  60.     function this.setSize(w, h)
  61.         this.w = w
  62.         this.h = h
  63.         return this
  64.     end
  65.  
  66.     function this.setDefaultColor(color)
  67.         this.defaultColor = color
  68.         return this
  69.     end
  70.  
  71.     function this.setActiveColoror(color)
  72.         this.activeColor = color
  73.         return this
  74.     end
  75.  
  76.   function this.setState(state)
  77.         this.state = state
  78.         return this
  79.     end
  80.  
  81.     function this.setDisable(state)
  82.         this.disable = state
  83.         return this
  84.     end
  85.  
  86.     function this.wasClicked(x,y)
  87.         if
  88.             x >= this.x and
  89.             x < this.x+this.w and
  90.             y >= this.y and
  91.             y < this.y+this.h and
  92.             not this.disable
  93.         then
  94.             return true
  95.         end
  96.         return false
  97.     end
  98.  
  99.     function this.onClick(callback)
  100.         this.callback = callback
  101.         return this
  102.     end
  103.  
  104.     function this.fireEvent()
  105.         if this.callback ~= nil then
  106.             this.callback(this.state)
  107.         end
  108.     end
  109.  
  110.     function this.draw()
  111.         if _locales.mon == nil then
  112.             print("Monitor not set!")
  113.             error()
  114.         end
  115.         local xpos = this.x+(this.w/2-#this.text/2)
  116.         local t = this.text
  117.         local bg = _locales.mon.getBackgroundColor()
  118.         local tc = _locales.mon.getTextColor()
  119.         if this.align == "l" then
  120.             xpos = this.x
  121.         end
  122.         if this.align == "r" then
  123.             xpos = this.x+this.w-#this.text
  124.         end
  125.         if #this.text > this.w then
  126.             xpos = this.x
  127.             t = string.sub(t,1,this.w-3)..".."..string.sub(t,-1)
  128.         end
  129.         _locales.mon.setTextColor(this.textColor)
  130.         local f = string.rep(" ", this.w)
  131.         if this.disable then
  132.       _locales.mon.setBackgroundColor(colors.gray)
  133.         else
  134.       mDisplayColor = this.defaultColor
  135.       if this.state then
  136.         mDisplayColor = this.activeColor
  137.       end
  138.             _locales.mon.setBackgroundColor(mDisplayColor)
  139.         end
  140.         for i = 1, this.h do
  141.             _locales.mon.setCursorPos(this.x, this.y+(i-1))
  142.             _locales.mon.write(f)
  143.         end
  144.         _locales.mon.setCursorPos(xpos,this.y+this.h/2)
  145.         _locales.mon.write(t)
  146.         _locales.mon.setBackgroundColor(bg)
  147.         _locales.mon.setTextColor(tc)
  148.     end
  149.  
  150.     function this.changeState()
  151.     this.state = not this.state
  152.         this.draw()
  153.     end
  154.  
  155.     return this
  156. end
  157.  
  158. --set Monitor handle to draw on
  159. function setMonitor(mon)
  160.     _locales.mon = mon
  161.     --MON = mon
  162. end
  163.  
  164. function clearMon()
  165.     _locales.mon.clear()
  166. end
  167.  
  168. local function isTable(element)
  169.     return type(element) == "table"
  170. end
  171.  
  172. local function isButton(element)
  173.     if isTable(element) and element.text ~= nil then
  174.         return true
  175.     end
  176.     return false
  177. end
  178.  
  179. local function mergeTables(tab1, tab2)
  180.     for i in pairs(tab2) do
  181.         tab1[#tab1+1] = tab2[i]
  182.     end
  183. end
  184.  
  185. --manage button checks
  186. function await(...)
  187.     array = {}
  188.     for i in pairs(arg) do
  189.         if i ~= "n" then
  190.             if isTable(arg[i]) and not isButton(arg[i]) then --table of buttons
  191.                 mergeTables(array, arg[i])
  192.             else --single button
  193.                 array[#array+1] = arg[i]
  194.             end
  195.         end
  196.     end
  197.  
  198.     for i in pairs(array) do
  199.             array[i].draw()
  200.     end
  201.     e, s, x, y = os.pullEvent("monitor_touch")
  202.     for i in pairs(array) do
  203.         if array[i].wasClicked(x,y) then
  204.             array[i].changeState()
  205.             array[i].fireEvent()
  206.         end
  207.     end
  208. end
  209.  
Add Comment
Please, Sign In to add comment