Advertisement
fatboychummy

Basic Button Implementation

Apr 27th, 2025 (edited)
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. --- Used to store all registered buttons
  2. local buttons = {}
  3.  
  4. --- Creates a new button object, with position {x,y}, size {width,height}, text "text", and the given colors.
  5. --- The callback value is a function that you pass to this function, and it will be called when the button is pressed.
  6. local function newButton(x, y, width, height, text, textColor, bgColor, callback)
  7.   --- Our button object. This stores all the relevant data about the button.
  8.   local button = {
  9.     x = x,
  10.     y = y,
  11.     width = width,
  12.     height = height,
  13.     text = text,
  14.     textColor = textColor,
  15.     bgColor = bgColor,
  16.     callback = callback
  17.   }
  18.  
  19.   table.insert(buttons, button) -- insert our new button into the list
  20.  
  21.   return button -- So we can edit the button later, if needed.
  22. end
  23.  
  24. --- Draws all buttons.
  25. local function drawButtons(_term)
  26.   -- Either use the inputted terminal object, or just draw directly to the terminal if not provided.
  27.   local term = _term or term
  28.  
  29.   for _, button in ipairs(buttons) do -- For each button ...
  30.     --- Used to draw the background of the button.
  31.     local blank_space = (' '):rep(button.width)
  32.    
  33.     -- Set the relevant colors for the button.
  34.     term.setBackgroundColor(button.bgColor)
  35.     term.setTextColor(button.textColor)
  36.    
  37.     for y = button.y, button.y + button.height - 1 do -- For each y position on the button
  38.       term.setCursorPos(button.x, y) -- Set the cursor at the 'start' of each line on the button
  39.       term.write(blank_space) -- draw the background of the button
  40.     end
  41.  
  42.     -- Write the text onto the button. For this, we will center the text.
  43.  
  44.     -- 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'.
  45.     -- For y, we just center it once.
  46.     local x_middle = math.ceil((button.x + button.width / 2) - (#button.text / 2))
  47.     local y_middle = math.ceil(button.y + button.height / 2)
  48.     term.setCursorPos(x_middle, y_middle)
  49.     term.write(button.text)
  50.   end
  51. end
  52.  
  53. --- Checks whether the given input click (x, y) is within the bounds of (x1, y1) and (x2, y2)
  54. ---@return boolean inBounds Whether the value is within bounds.
  55. local function withinBounds(x1, y1, x2, y2, x, y)
  56.   return x >= x1 and x <= x2
  57.      and y >= y1 and y <= y2
  58. end
  59.  
  60. --- Test which button was clicked given an x,y position. Returns the first in the list, make sure your buttons don't overlap!
  61. local function testButtons(x, y)
  62.   for _, button in ipairs(buttons) do -- for each button ...
  63.     -- If the click is within the bounds set by the button...
  64.     if withinBounds(button.x, button.y, button.x + button.width - 1, button.y + button.height - 1, x, y) then
  65.       return button -- Return the button!
  66.     end
  67.   end
  68.  
  69.   -- If we make it here, no button was clicked! Nothing is returned.
  70. end
  71.  
  72. --- Checks if a button was pressed given an x,y click position, then runs the button that was pressed.
  73. local function runButton(x, y)
  74.   local buttonPressed = testButtons(x, y) -- determine which button was clicked
  75.  
  76.   if buttonPressed then -- If a button was clicked
  77.     buttonPressed.callback(buttonPressed) -- Call the callback of the button, passing itself as a parameter.
  78.   end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement