Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- buttons = {}
- Button={}
- Button.__index=Button
- defaultBackgroundColor=colors.black
- function Button.new(params)
- assert(params.monitor, "Please set the monitor param (peripheral wrap of monitor)")
- assert(params.inactiveColor, "Please set inactiveColor param")
- assert(params.activeColor, "Please set activeColor param")
- assert(params.textColor, "Ple ase set textColor param")
- assert(params.position.x, "Please set position.x param")
- assert(params.position.y, "Please set position.y param")
- assert(params.dimensions.height, "Please set dimensions.height param")
- assert(params.dimensions.width, "Please set dimensions.width param")
- assert(params.inactiveButtonText, "Please set the inactiveButtonText param")
- assert(params.activeButtonText, "Please set the activeButtonText param")
- assert(params.onClick, "Please set the onClick param (anonymous function)")
- local button={}
- setmetatable(button, Button)
- button.params=params
- if params.active then
- button.active=true
- else
- button.active=false
- end
- buttons[#buttons+1]=button
- return button
- end
- function Button:draw()
- local buttonColor=self.params.inactiveColor
- local buttonText=self.params.inactiveButtonText
- if self.active == true then
- buttonColor=self.params.activeColor
- buttonText=self.params.activeButtonText
- end
- self.params.monitor.setCursorPos(self.params.position.x, self.params.position.y)
- self.params.monitor.setBackgroundColor(buttonColor)
- for y=1,self.params.dimensions.height-1 do
- for x=1,self.params.dimensions.width-1 do
- self.params.monitor.setCursorPos(self.params.position.x+x, self.params.position.y+y)
- self.params.monitor.write(" ")
- end
- end
- local offset= {
- y=0
- }
- if self.params.dimensions.height%2==0 then
- offset.y=1
- end
- local textPositionY=math.floor(self.params.dimensions.height/2)-offset.y
- local textPositionX=math.floor((self.params.dimensions.width-string.len(buttonText))/2)
- self.params.monitor.setTextColor(self.params.textColor)
- self.params.monitor.setCursorPos(textPositionX+self.params.position.x, textPositionY+self.params.position.y)
- self.params.monitor.write(buttonText)
- self.params.monitor.setBackgroundColor(defaultBackgroundColor)
- end
- function Button:toggle()
- self.active=not self.active
- self:draw()
- end
- function Button:clicked(x, y)
- local inX = x >= self.params.position.x and x < (self.params.position.x+self.params.dimensions.width)
- local inY = y >= self.params.position.y and y < (self.params.position.y+self.params.dimensions.height)
- return inX and inY
- end
- function buttonClickHandler(spawned)
- local running=true
- while running do
- local event, side, x, y = os.pullEvent("monitor_touch")
- for index,button in ipairs(buttons) do
- if button:clicked(x, y) then
- local runOnClick=function()
- button.params.onClick(button)
- end
- local spawnClickHandler=function()
- buttonClickHandler(true)
- end
- parallel.waitForAny(runOnClick, spawnClickHandler)
- if spawned then
- print("done with spawned click handler. returning to main...")
- return
- end
- break
- end
- end
- end
- end
- function drawButtons()
- for index,button in ipairs(buttons) do
- button:draw()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement