Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tButtons = {}
- function clear()
- tButtons = {}
- end
- function new(_sText, _nX, _nY, _nWidth, _nHeight, _nTextColour, _nOffColour, _nOnColour, _fClick, _nDelay)
- if type(_sText) ~= "string" then
- error("Argument 1: string expected, got "..type(text), 2)
- elseif type(_nX) ~= "number" then
- error("Argument 2: number expected, got "..type(_nX), 2)
- elseif type(_nY) ~= "number" then
- error("Argument 3: number expected, got "..type(_nY), 2)
- elseif type(_nWidth) ~= "number" then
- error("Argument 4: number expected, got "..type(_nWidth), 2)
- elseif type(_nHeight) ~= "number" then
- error("Argument 5: number expected, got "..type(_nHeight), 2)
- elseif type(_fClick) ~= "function" then
- error("Argument 8: function expected, got "..type(_fClick), 2)
- elseif type(_nDelay) ~= "number" then
- error("Argument 9: number expected, got "..type(_nDelay), 2)
- end
- table.insert(tButtons, {
- text = _sText,
- x = math.floor(_nX),
- endX = math.floor((_nX + _nWidth) - 1),
- y = _nY,
- endY = math.floor((_nY + _nHeight) - 1),
- w = math.floor(_nWidth),
- h = math.floor(_nHeight),
- textCol = _nTextColour,
- onCol = _nOnColour,
- offCol = _nOffColour,
- on_click = _fClick,
- delay = _nDelay,
- value = false
- })
- end
- local function draw(bText, x, y)
- local col
- for _, v in pairs(tButtons) do
- if bText == v.text and x == v.x and y == v.y then
- if v.value then
- col = v.onCol
- else
- col = v.offCol
- end
- for y = v.y, v.endY do
- paintutils.drawLine(v.x, y, v.endX, y, col)
- end
- term.setTextColour(v.textCol)
- term.setCursorPos(((v.endX + v.x - #v.text) / 2) + 1, (v.endY + v.y) / 2)
- term.write(v.text)
- end
- end
- end
- function run()
- for _, v in pairs(tButtons) do
- draw(v.text, v.x, v.y, v.uID)
- end
- while true do
- local _, p1, x, y = os.pullEvent("monitor_touch")
- for _, v in pairs(tButtons) do
- if (x >= v.x and x <= v.endX and y >= v.y and y <= v.endY) then
- parallel.waitForAll(
- function()
- v.value = true
- draw(v.text, v.x, v.y)
- sleep(v.delay)
- v.value = false
- draw(v.text, v.x, v.y)
- end,
- v.on_click
- )
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment