Advertisement
gelatine87

button

May 7th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | Gaming | 0 0
  1. -- Function to check if a peripheral is connected and return it, or raise an error with a custom error message.
  2. local function checkPeripheral(name, errorMessage)
  3.    local peripheral = peripheral.find(name)
  4.    assert(peripheral, errorMessage)
  5.    return peripheral
  6. end
  7.  
  8. -- Custom colors for button states
  9. colorButtonOn = colors.lime
  10. colorButtonOff = colors.gray
  11.  
  12. -- Initialize the monitor peripheral
  13. mon = checkPeripheral("monitor", "Monitor not found.")
  14. button = {}  -- Table to store button data
  15.  
  16. -- Function to clear the button table and clear the monitor screen.
  17. function clearTable()
  18.    button = {}
  19.    mon.clear()
  20. end
  21.  
  22. -- Function to add a button to the button table with its associated function and coordinates.
  23. function setTable(name, func, xmin, xmax, ymin, ymax)
  24.    button[name] = {}
  25.    button[name]["func"] = func
  26.    button[name]["active"] = false
  27.    button[name]["xmin"] = xmin
  28.    button[name]["ymin"] = ymin
  29.    button[name]["xmax"] = xmax
  30.    button[name]["ymax"] = ymax
  31. end
  32.  
  33. -- Function to fill a button area with text and color.
  34. function fill(text, color, bData)
  35.    mon.setBackgroundColor(color)
  36.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
  37.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) / 2) + 1
  38.    for j = bData["ymin"], bData["ymax"] do
  39.       mon.setCursorPos(bData["xmin"], j)
  40.       if j == yspot then
  41.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
  42.             if k == xspot then
  43.                mon.write(text)
  44.             else
  45.                mon.write(" ")
  46.             end
  47.          end
  48.       else
  49.          for i = bData["xmin"], bData["xmax"] do
  50.             mon.write(" ")
  51.          end
  52.       end
  53.    end
  54.    mon.setBackgroundColor(colors.black)
  55. end
  56.  
  57. -- Function to update the screen with button states.
  58. function screen()
  59.    local currColor
  60.    for name, data in pairs(button) do
  61.       local on = data["active"]
  62.       if on == true then
  63.          currColor = colorButtonOn
  64.       else
  65.          currColor = colorButtonOff
  66.       end
  67.       fill(name, currColor, data)
  68.    end
  69. end
  70.  
  71. -- Function to toggle a button's state and update the screen.
  72. function toggleButton(name)
  73.    for buttonName, data in pairs(button) do
  74.        if buttonName ~= name and data["active"] then
  75.            data["active"] = false
  76.        end
  77.    end
  78.    button[name]["active"] = true
  79.    screen()
  80. end
  81.  
  82. -- Function to temporarily toggle a button's state (for visual feedback) and update the screen.
  83. function flash(name)
  84.    toggleButton(name)
  85.    screen()
  86.    sleep(0.25)
  87.    toggleButton(name)
  88.    screen()
  89. end
  90.  
  91. -- Function to check if a click falls within any button's coordinates and execute its associated function.
  92. function checkxy(x, y)
  93.    for name, data in pairs(button) do
  94.       if y >= data["ymin"] and y <= data["ymax"] then
  95.          if x >= data["xmin"] and x <= data["xmax"] then
  96.             if data["func"] then
  97.                data["func"]()
  98.             end
  99.             return true
  100.          end
  101.       end
  102.    end
  103.    return false
  104. end
  105.  
  106. -- Function to draw a colored box on the monitor.
  107. function drawBox(xStart, yStart, xEnd, yEnd, color)
  108.    local w, h = mon.getSize()
  109.    xStart = math.max(1, xStart)
  110.    xEnd = math.min(w, xEnd)
  111.    yStart = math.max(1, yStart)
  112.    yEnd = math.min(w, yEnd)
  113.    for x = xStart, xEnd do
  114.        mon.setBackgroundColor(color)
  115.        mon.setCursorPos(x, yStart)
  116.        mon.write(" ")
  117.        mon.setCursorPos(x, yEnd)
  118.        mon.write(" ")
  119.    end
  120.    for y = yStart + 1, yEnd - 1 do
  121.        mon.setBackgroundColor(color)
  122.        mon.setCursorPos(xStart, y)
  123.        mon.write(" ")
  124.        mon.setCursorPos(xEnd, y)
  125.        mon.write(" ")
  126.    end
  127. end
  128.  
  129. -- Function to draw button boxes with a specified color.
  130. function drawButtonBoxes(color)
  131.    drawBox(2, 2, 78, 33, color)
  132. end
  133.  
  134. -- Function to display centered text on the monitor.
  135. function heading(text, y, bgColor)
  136.    local w, h = mon.getSize()
  137.    mon.setBackgroundColor(colors.black)
  138.    mon.setCursorPos((w - string.len(text)) / 2 + 1, y)
  139.    mon.write(text)
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement