Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local api = {
- ["buttons"] = {}
- }
- local function createButtonModel(side, x, y, w, h, text, textColor, colorA, colorB, toggle, callback)
- return {
- ["side"] = side,
- ["x"] = x,
- ["y"] = y,
- ["width"] = w,
- ["height"] = h,
- ["text"] = text,
- ["textColor"] = textColor,
- ["backgroundColorA"] = colorA,
- ["backgroundColorB"] = colorB,
- ["toggle"] = toggle,
- ["state"] = false,
- ["callback"] = callback,
- }
- end
- local function drawButton(canvas, button)
- local oldTextColor = canvas.getTextColor()
- local oldBackgroundColor = canvas.getBackgroundColor()
- canvas.setCursorBlink(false)
- canvas.setTextColor(button.textColor)
- if button.state then
- canvas.setBackgroundColor(button.backgroundColorB)
- else
- canvas.setBackgroundColor(button.backgroundColorA)
- end
- for y = 0, button.height - 1, 1 do
- for x = 0, button.width - 1, 1 do
- canvas.setCursorPos(button.x + x, button.y + y)
- canvas.write(" ")
- end
- end
- local x = button.x + (button.width - #button.text) / 2
- local y = button.y + (button.height - 1) / 2
- canvas.setCursorPos(x, y)
- canvas.write(button.text)
- canvas.setTextColor(oldTextColor)
- canvas.setBackgroundColor(oldBackgroundColor)
- end
- function api.createButton(side, sx, sy, ex, ey, text, textColor, colorA, colorB, toggle, callback)
- table.insert(api.buttons, createButtonModel(side, sx, sy, ex, ey, text, textColor, colorA, colorB, toggle, callback))
- end
- function api.drawButtons()
- for i, button in ipairs(api.buttons) do
- if button.side ~= nil then
- peripheral.wrap(button.side).clear()
- end
- end
- for i, button in ipairs(api.buttons) do
- if button.side == nil then
- drawButton(term, button)
- else
- drawButton(peripheral.wrap(button.side), button)
- end
- end
- end
- function api.interact(side, x, y)
- for i, button in ipairs(api.buttons) do
- local endX = button.x + button.width
- local endY = button.y + button.height
- if x >= button.x and x < endX and y >= button.y and y < endY and side == button.side then
- if button.toggle then
- button.state = not button.state
- api.drawButtons()
- button.callback(button.state)
- else
- button.state = true
- api.drawButtons()
- button.callback()
- button.state = false
- end
- end
- end
- end
- return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement