Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- EyeDeck's button API
- Handles drawing buttons to a screen, and mapping
- raw (x,y) input coords to drawn buttons.
- --]]
- button_registry = {}
- --monitor to write to, top left (x,y), width and height
- --text color, background color, button name (optional)
- function drawButton(m,x,y,w,h,txtcolor,bgcolor,text,name)
- local old_term = term.redirect(m)
- local old_bg = term.getBackgroundColor()
- local old_tx = term.getTextColor()
- local old_posx, old_posy = term.getCursorPos()
- paintutils.drawLine(x+1, y, x+w-2, y, colors.lightGray)
- paintutils.drawLine(x+1, y+h-1, x+w-2, y+h-1, colors.gray)
- paintutils.drawLine(x, y+1, x, y+h-2, colors.gray)
- paintutils.drawLine(x+w-1, y+1, x+w-1, y+h-2, colors.lightGray)
- paintutils.drawFilledBox(x+1, y+1, x+w-2, y+h-2, bgcolor)
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- term.setCursorPos(x+math.floor((w-string.len(text))/2),y+math.floor((h-1)/2))
- term.write(text)
- term.setCursorPos(old_posx, old_posy)
- term.setTextColor(old_tx)
- term.setBackgroundColor(old_bg)
- term.redirect(old_term)
- if name ~= nil then
- table.insert(button_registry,{x=x+1,y=y+1,x2=x+w-2,y2=y+h-2,name=name})
- end
- end
- function getButton(x,y)
- for _,v in pairs(button_registry) do
- if x >= v.x and x <= v.x2 and y >= v.y and y <= v.y2 then
- return v.name
- end
- end
- return false
- end
- function removeButton(name)
- for i,v in pairs(button_registry) do
- if v.name == name then
- table.remove(button_registry, i)
- return true
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement