Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Format:
- buttons = {
- button = { --Defines button prototype--
- x=int,
- y=int,
- width=int,
- height=int,
- color=int color,
- textColor=int color,
- text=string,
- listeners={function,...},
- active=boolean,
- side=string,
- display=Table term (eg. peripheral.wrap("left") or term.native())
- },
- ...
- }
- Functions:
- API:
- button.button --original button to be duplicated
- button.setDisplay(string side) --Sets default display for all buttons (useful if you don't care about multi-display)
- button.draw(win) --Uses win to reassert all content behind buttons, and redraws all buttons
- button.act(table event) --Event can be made a table using {os.pullEvent()}.
- --This function takes an event table and executes all callbacks on all active buttons that were clicked
- --Callbacks are given callback(button,event)
- Button Object:
- button:new(x,y,w,h,color,text,textColor)
- button:setDisplay(string side) --"native" uses the computer's local display
- button:setText(text)
- button:setPosition(x,y) NOTE: changing position does not remove the old button from the display
- button:setSize(w,h) NOTE: changing size does not remove the old button from the display
- button:setColor(color)
- button:addListener(func,[name])
- button:removeListener(name)
- button:clearListeners()
- button:setActive(isActive)
- ]]
- button = { --Defines button prototype--
- x=1,
- y=1,
- width=1,
- height=1,
- color=colors.red,
- textColor=colors.white,
- text="",
- listeners={},
- active=true,
- side="native",
- display=term.native()
- }
- buttons = {}
- function setDisplay(side)
- button.side = side
- if side=="native" then
- button.display = term.native()
- else
- button.display = peripheral.wrap(side)
- end
- end
- function button:new(x,y,w,h,color,text,textColor) --NOTE:false can be used to inherit values in most contexts--
- local newButton = {}
- setmetatable(newButton, self)
- self.__index = self
- --Setup variables with given arguments
- if x then newButton.x=x end
- if y then newButton.y=y end
- if w then newButton.width=w end
- if h then newButton.height=h end
- if color then newButton.color=color end
- if text then newButton.text=text end
- if textColor then newButton.textColor=textColor end
- newButton.listeners = {}
- table.insert(buttons,newButton)
- return newButton --Button is already configured, but this object allows dynamic configuration--
- end
- function button:setDisplay(side)
- self.side = side
- if side=="native" then
- self.display = term.native()
- else
- self.display = peripheral.wrap(side)
- end
- end
- function button:setText(text,draw)
- self.text=text
- if draw~=false then self:draw() end
- end
- function button:setPos(x,y,draw)
- if x then self.x=x end
- if y then self.y=y end
- if draw~=false then self:draw() end
- end
- function button:setSize(w,h,draw)
- if w then self.width = w end
- if h then self.height = h end
- if draw~=false then self:draw() end
- end
- function button:setColor(color,draw)
- if color then self.color = color end
- if draw~=false then self:draw() end
- end
- function button:addListener(func,name) --Name must be specified in order to later remove the listener
- if name then
- if func then self.listeners[name] = func end
- else
- if func then table.insert(self.listeners,func) end
- end
- end
- function button:removeListener(name)
- if name then self.listeners[name] = nil end
- end
- function button:clearListeners()
- self.listeners={}
- end
- function button:setActive(isActive) --nil used to toggle--
- if type(isActive)~=boolean then
- self.active = isActive
- else
- self.active = not self.active
- end
- end
- function button:draw() --If all other elements are drawn in a child window,
- --it can be passed as an argument to get rid of stray buttons
- --bif win then win.redraw() end
- local currentColor = self.display.getBackgroundColor()
- local currentText = self.display.getTextColor()
- local oldTerm = term.redirect(self.display)
- term.setBackgroundColor(self.color)
- term.setTextColor(self.textColor)
- paintutils.drawFilledBox(self.x,self.y,self.x+self.width-1,self.y+self.height-1,self.color)
- term.setCursorPos((self.x)+math.floor(self.width/2-#self.text/2),(self.y-1)+math.ceil(self.height/2))
- term.write(self.text)
- term.setTextColor(currentText)
- term.setBackgroundColor(currentColor)
- term.redirect(oldTerm)
- end
- function draw(win) --Optional window to clear unused buttons
- if win then win.redraw() end
- for k,v in ipairs(buttons) do
- if v.active then
- v:draw()
- end
- end
- end
- function act(event)
- local display
- if type(event[2])=="number" then
- display = "native"
- elseif type(event[2])=="string" then
- display = event[2]
- end
- for _,v in ipairs(buttons) do
- 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
- for _,func in ipairs(v.listeners) do
- func(v,event)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement