Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local buttons = {
- [1] = {
- x = 3,
- y = 3,
- on_state = "Turn off reactor",
- off_state = "Turn on reactor",
- current_state = true, -- Initially the button is off
- onClick = function(state) -- Turn the reactor on or off
- if state then -- Currently on, turning off
- term.setCursorPos(1,1)
- term.write("Turning off the reactor")
- sleep(1)
- -- The current state is off: turn off the reactor
- else
- term.setCursorPos(1,1)
- term.write("Turning on the reactor")
- sleep(1)
- -- The current state is off: turn on the reactor
- end
- end
- }
- }
- local function drawButton(button)
- term.setCursorPos(button.x, button.y)
- if button.current_state then -- If it’s on, our button is green
- term.setBackgroundColor(colors.green)
- else -- Otherwise it’s red
- term.setBackgroundColor(colors.red)
- end
- term.setTextColor(colors.white)
- local button_text -- We’ll store what text we want to write here
- if button.current_state then
- button_text = button.on_state
- else
- button_text = button.off_state
- end
- local button_length = #button.on_state -- How long is our button?
- if #button.off_state > #button.on_state then -- We want the longest it ever gets
- button_length = #button.off_state
- end
- term.write(string.rep(" ", 2 + button_length)) -- Give it a little space on each side
- term.setCursorPos(button.x + 1 + button_length / 2 -
- #button_text / 2, button.y) -- Nothing too complicated here: just centering the text inside of the button
- term.write(button_text) -- Draw our text
- end
- local function checkClick(x, y)
- for index, button in pairs(buttons) do
- local button_length = #button.on_state + 2
- if #button.off_state > #button.on_state then
- button_length = #button.off_state + 2
- end
- if button.y == y and
- x >= button.x and
- x <= button.x + button_length then
- -- Yep, we clicked this button
- button.onClick(button.current_state)
- button.current_state = not button.current_state
- end
- end
- end
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- for index, button in pairs(buttons) do drawButton(button) end
- local e = {os.pullEvent("mouse_click")} -- Change to "monitor_touch" if you’re working with a monitor
- checkClick(e[3], e[4])
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement