Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local API = {}
- local button = {}
- local component = require("component")
- local gpu = component.gpu
- function draw(x, y, width, height, colorB, colorF, text)
- local oldBackground = gpu.setBackground(colorB)
- local textLen = string.len(text)
- if(textLen > (width - 2)) then
- text = string.sub(text , 1, width)
- end
- gpu.fill(x, y, width, height, " ")
- local oldForeground = gpu.setForeground(colorF)
- gpu.set(x + math.floor(width / 2) - (textLen / 2), y + math.floor(height / 2), text)
- gpu.setBackground(oldBackground)
- gpu.setForeground(oldForeground)
- end
- function toggleColor(buttonName)
- local foreground
- local background
- if button[buttonName]["state"] then
- foreground = button[buttonName]["textC1"]
- background = button[buttonName]["color1"]
- else
- foreground = button[buttonName]["textC2"]
- background = button[buttonName]["color2"]
- end
- draw(button[buttonName]["x"], button[buttonName]["y"], button[buttonName]["width"], button[buttonName]["height"], background, foreground, buttonName)
- end
- function API.add(name, callback, x, y, width, height, color1, color2, textC1, textC2, toggle, state)
- button[name] = {}
- button[name]["callback"] = callback
- button[name]["x"] = x
- button[name]["y"] = y
- button[name]["width"] = width
- button[name]["height"] = height
- button[name]["color1"] = color1
- button[name]["color2"] = color2
- button[name]["textC1"] = textC1
- button[name]["textC2"] = textC2
- button[name]["active"] = state
- button[name]["toggle"] = toggle
- draw(x, y, width, height, color1, textC1, name);
- end
- function API.getButtonState(name)
- return button[name]["state"]
- end
- -- Check for any button presses
- function API.touch(x, y)
- for name, data in pairs(button) do
- local btnX = button[name]["x"]
- local btnY = button[name]["y"]
- local btnWidth = button[name]["width"]
- local btnHeight = button[name]["height"]
- if x < btnX then goto continue end
- if y < btnY then goto continue end
- if x > (btnX + btnWidth) then goto continue end
- if y > (btnY + btnHeight) then goto continue end
- if button[name]["callback"](name) then
- if button[name]["toggle"] then
- toggleColor(name)
- button[name]["state"] = not button[name]["state"];
- end
- end
- break;
- ::continue::
- end
- end
- return API;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement