Advertisement
superanonymous

button api

Jun 30th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. --[[
  2.  
  3.     Format:
  4.         buttons = {
  5.             button = { --Defines button prototype--
  6.                 x=int,
  7.                 y=int,
  8.                 width=int,
  9.                 height=int,
  10.                 color=int color,
  11.                 textColor=int color,
  12.                 text=string,
  13.                 listeners={function,...},
  14.                 active=boolean,
  15.                 side=string,
  16.                 display=Table term (eg. peripheral.wrap("left") or term.native())
  17.             },
  18.             ...
  19.         }
  20.  
  21.     Functions:
  22.         API:
  23.             button.button --original button to be duplicated
  24.             button.setDisplay(string side) --Sets default display for all buttons (useful if you don't care about multi-display)
  25.             button.draw(win) --Uses win to reassert all content behind buttons, and redraws all buttons
  26.             button.act(table event) --Event can be made a table using {os.pullEvent()}.
  27.                                     --This function takes an event table and executes all callbacks on all active buttons that were clicked
  28.                                     --Callbacks are given callback(button,event)
  29.         Button Object:
  30.             button:new(x,y,w,h,color,text,textColor)
  31.             button:setDisplay(string side) --"native" uses the computer's local display
  32.             button:setText(text)
  33.             button:setPosition(x,y) NOTE: changing position does not remove the old button from the display
  34.             button:setSize(w,h) NOTE: changing size does not remove the old button from the display
  35.             button:setColor(color)
  36.             button:addListener(func,[name])
  37.             button:removeListener(name)
  38.             button:clearListeners()
  39.             button:setActive(isActive)
  40.  
  41. ]]
  42.  
  43. button = { --Defines button prototype--
  44.     x=1,
  45.     y=1,
  46.     width=1,
  47.     height=1,
  48.     color=colors.red,
  49.     textColor=colors.white,
  50.     text="",
  51.     listeners={},
  52.     active=true,
  53.     side="native",
  54.     display=term.native()
  55. }
  56.  
  57. buttons = {}
  58.  
  59. function setDisplay(side)
  60.     button.side = side
  61.     if side=="native" then
  62.         button.display = term.native()
  63.     else
  64.         button.display = peripheral.wrap(side)
  65.     end
  66. end
  67.  
  68. function button:new(x,y,w,h,color,text,textColor) --NOTE:false can be used to inherit values in most contexts--
  69.     local newButton = {}
  70.     setmetatable(newButton, self)
  71.     self.__index = self
  72.     --Setup variables with given arguments
  73.     if x then newButton.x=x end
  74.     if y then newButton.y=y end
  75.     if w then newButton.width=w end
  76.     if h then newButton.height=h end
  77.     if color then newButton.color=color end
  78.     if text then newButton.text=text end
  79.     if textColor then newButton.textColor=textColor end
  80.     newButton.listeners = {}
  81.     table.insert(buttons,newButton)
  82.     return newButton --Button is already configured, but this object allows dynamic configuration--
  83. end
  84.  
  85. function button:setDisplay(side)
  86.     self.side = side
  87.     if side=="native" then
  88.         self.display = term.native()
  89.     else
  90.         self.display = peripheral.wrap(side)
  91.     end
  92. end
  93. function button:setText(text,draw)
  94.     self.text=text
  95.     if draw~=false then self:draw() end
  96. end
  97. function button:setPos(x,y,draw)
  98.     if x then self.x=x end
  99.     if y then self.y=y end
  100.     if draw~=false then self:draw() end
  101. end
  102. function button:setSize(w,h,draw)
  103.     if w then self.width = w end
  104.     if h then self.height = h end
  105.     if draw~=false then self:draw() end
  106. end
  107. function button:setColor(color,draw)
  108.     if color then self.color = color end
  109.     if draw~=false then self:draw() end
  110. end
  111. function button:addListener(func,name) --Name must be specified in order to later remove the listener
  112.     if name then
  113.         if func then self.listeners[name] = func end
  114.     else
  115.         if func then table.insert(self.listeners,func) end
  116.     end
  117. end
  118. function button:removeListener(name)
  119.     if name then self.listeners[name] = nil end
  120. end
  121. function button:clearListeners()
  122.     self.listeners={}
  123. end
  124. function button:setActive(isActive) --nil used to toggle--
  125.     if type(isActive)~=boolean then
  126.         self.active = isActive
  127.     else
  128.         self.active = not self.active
  129.     end
  130. end
  131. function button:draw() --If all other elements are drawn in a child window,
  132.                           --it can be passed as an argument to get rid of stray buttons
  133.     --bif win then win.redraw() end
  134.  
  135.     local currentColor = self.display.getBackgroundColor()
  136.     local currentText = self.display.getTextColor()
  137.  
  138.     local oldTerm = term.redirect(self.display)
  139.  
  140.     term.setBackgroundColor(self.color)
  141.     term.setTextColor(self.textColor)
  142.  
  143.     paintutils.drawFilledBox(self.x,self.y,self.x+self.width-1,self.y+self.height-1,self.color)
  144.  
  145.     term.setCursorPos((self.x)+math.floor(self.width/2-#self.text/2),(self.y-1)+math.ceil(self.height/2))
  146.     term.write(self.text)
  147.  
  148.     term.setTextColor(currentText)
  149.     term.setBackgroundColor(currentColor)
  150.  
  151.     term.redirect(oldTerm)
  152.  
  153. end
  154.  
  155. function draw(win) --Optional window to clear unused buttons
  156.     if win then win.redraw() end
  157.     for k,v in ipairs(buttons) do
  158.         if v.active then
  159.             v:draw()
  160.         end
  161.     end
  162. end
  163.  
  164. function act(event)
  165.     local display
  166.     if type(event[2])=="number" then
  167.         display = "native"
  168.     elseif type(event[2])=="string" then
  169.         display = event[2]
  170.     end
  171.     for _,v in ipairs(buttons) do
  172.         if v.side==display and v.active and event[3]>=v.x and event[3]<=v.x+v.width-1 and event[4]>=v.y and event[4]<=v.y+v.height-1 then
  173.             for _,func in ipairs(v.listeners) do
  174.                 func(v,event)
  175.             end
  176.         end
  177.     end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement