Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Used to store all registered buttons
- local buttons = {}
- --- Creates a new button object, with position {x,y}, size {width,height}, text "text", and the given colors.
- --- The callback value is a function that you pass to this function, and it will be called when the button is pressed.
- local function newButton(x, y, width, height, text, textColor, bgColor, callback)
- --- Our button object. This stores all the relevant data about the button.
- local button = {
- x = x,
- y = y,
- width = width,
- height = height,
- text = text,
- textColor = textColor,
- bgColor = bgColor,
- callback = callback
- }
- table.insert(buttons, button) -- insert our new button into the list
- return button -- So we can edit the button later, if needed.
- end
- --- Draws all buttons.
- local function drawButtons(_term)
- -- Either use the inputted terminal object, or just draw directly to the terminal if not provided.
- local term = _term or term
- for _, button in ipairs(buttons) do -- For each button ...
- --- Used to draw the background of the button.
- local blank_space = (' '):rep(button.width)
- -- Set the relevant colors for the button.
- term.setBackgroundColor(button.bgColor)
- term.setTextColor(button.textColor)
- for y = button.y, button.y + button.height - 1 do -- For each y position on the button
- term.setCursorPos(button.x, y) -- Set the cursor at the 'start' of each line on the button
- term.write(blank_space) -- draw the background of the button
- end
- -- Write the text onto the button. For this, we will center the text.
- -- Note for the x_middle, we also subtract half of the length of the text. This centers the text horizontally, since we start writing 'at the left'.
- -- For y, we just center it once.
- local x_middle = math.ceil((button.x + button.width / 2) - (#button.text / 2))
- local y_middle = math.ceil(button.y + button.height / 2)
- term.setCursorPos(x_middle, y_middle)
- term.write(button.text)
- end
- end
- --- Checks whether the given input click (x, y) is within the bounds of (x1, y1) and (x2, y2)
- ---@return boolean inBounds Whether the value is within bounds.
- local function withinBounds(x1, y1, x2, y2, x, y)
- return x >= x1 and x <= x2
- and y >= y1 and y <= y2
- end
- --- Test which button was clicked given an x,y position. Returns the first in the list, make sure your buttons don't overlap!
- local function testButtons(x, y)
- for _, button in ipairs(buttons) do -- for each button ...
- -- If the click is within the bounds set by the button...
- if withinBounds(button.x, button.y, button.x + button.width - 1, button.y + button.height - 1, x, y) then
- return button -- Return the button!
- end
- end
- -- If we make it here, no button was clicked! Nothing is returned.
- end
- --- Checks if a button was pressed given an x,y click position, then runs the button that was pressed.
- local function runButton(x, y)
- local buttonPressed = testButtons(x, y) -- determine which button was clicked
- if buttonPressed then -- If a button was clicked
- buttonPressed.callback(buttonPressed) -- Call the callback of the button, passing itself as a parameter.
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement