Skip to content

Instantly share code, notes, and snippets.

@hugeblank
Created April 21, 2019 20:13
Show Gist options
  • Save hugeblank/33cf2d15b44e2001e10c3c1de98bd50f to your computer and use it in GitHub Desktop.
Save hugeblank/33cf2d15b44e2001e10c3c1de98bd50f to your computer and use it in GitHub Desktop.
Demo program for the interactable API I'm working on. It's pretty easy to work with!
local element = require("interactables") -- This is the backbone of this program
local text = "Click me!" -- Text to place on button
local cbuddon = element.button(5, 5, #text+2, 3) -- Create a button element
local function render(sel, lock) -- Rendering function
if lock then -- If it is locked gray it out
paintutils.drawFilledBox(5, 5, #text+6, 7, colors.gray)
elseif sel then -- Otherwise if it's selected make it bright
paintutils.drawFilledBox(5, 5, #text+6, 7, colors.lime)
else -- Otherwise make it dark
paintutils.drawFilledBox(5, 5, #text+6, 7, colors.green)
end
term.setCursorPos(6, 6) -- Set the position for writing
term.write(text)
end
local counter = 0 -- Counter for clicks
cbuddon.link(function(sel, lock) -- Link render and counter function
if sel == true then -- Add one to the counter if the button got clicked
counter = counter+1
end
if counter == 10 and not lock then -- If the counter is 10 and not locked lock it
cbuddon.lock(true)
end
end, render)
render() -- Draw the element on the screen before event pulling
while true do
local e = {os.pullEvent()} -- Pull and pack event
cbuddon.trigger(unpack(e)) -- Send it to button trigger
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment