bryceio

Computercraft ButtonAPI

May 16th, 2021 (edited)
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. --bryceio's Computercraft Button API v1.0
  2. --This API is free for anyone to use, but if you use this for any form of content creation, please credit me.
  3. --I am bryceio on most social medias (except twitter @bryceio_), so that will do.
  4. --If you have any suggestions for improving this API please let me know.
  5.  
  6. --This is the function that creates a new button to be used by the further functions.
  7. --This function returns a list with keys. You can freely edit or read these keys once created, but this is just a simple constructor for the buttons.
  8. --The inputs should be the following:
  9. --xPos: an integer representing the x posisiton of the button's top left corner.
  10. --yPos: an integer representing the x posistion of the button's bottm left corner.
  11. --label: a string that will be the text on the button.
  12. --defaultColor: the integer value of the color the button will be while inactive. I recommend using the computercraft colors api for getting a number.
  13. --pressedColor: the integer value of the color the button will be while active. Again, you may want to use the colors api.
  14. --toggleable: a boolean that determines if pressing the button should activate and then deactivate the button or should switch between active and inactive with each click.
  15. --runFucntion: a function that you want the button to run when pressed. Toggleable buttons will run the function with a parameter of false when being turned on and a parameter of true when being turned off.
  16. function createNewButton(xPos, yPos, label, defaultColor, pressedColor, toggleable, runFunction)
  17.     buttonObject = {}
  18.     buttonObject["x"] = xPos
  19.     buttonObject["y"] = yPos
  20.     buttonObject["label"] = label
  21.     buttonObject["defaultColor"] = defaultColor
  22.     buttonObject["pressedColor"] = pressedColor
  23.     buttonObject["toggleable"] = toggleable
  24.     buttonObject["function"] = runFunction
  25.     buttonObject["toggled"] = false --The current state of a button between On/Off. Used for changing button color and determining toggleable button's functionality
  26.     return buttonObject
  27. end
  28.  
  29. --This is the function that draws a list of buttons onto a given monitor.
  30. --This function does not return any value or give any function to the buttons, but merely displays the buttons.
  31. --The inputs should be the following:
  32. --monitor: a wrapped peripheral monitor (done by passing peripheral.wrap(monitorSide) or a variable that has been declared as such.
  33. --buttonList: a list containing a series of buttons constructed via the createNewButton() function.
  34. function drawButtons(monitor, buttonList)
  35.     monitor.setBackgroundColor(colors.black)
  36.     monitor.clear()
  37.     local i
  38.     for i = 1, #buttonList do
  39.         if buttonList[i]["toggled"] then
  40.             monitor.setBackgroundColor(buttonList[i]["pressedColor"])
  41.         else
  42.             monitor.setBackgroundColor(buttonList[i]["defaultColor"])
  43.         end
  44.         local xPos = buttonList[i]["x"]
  45.         local yPos = buttonList[i]["y"]
  46.         monitor.setCursorPos(xPos, yPos)
  47.         for character = 1, #buttonList[i]["label"] + 2 do
  48.             monitor.write(" ")
  49.         end
  50.         monitor.setCursorPos(xPos, yPos + 1)
  51.         monitor.write(" " .. buttonList[i]["label"] .. " ")
  52.         monitor.setCursorPos(xPos, yPos + 2)
  53.         for character = 1, #buttonList[i]["label"] + 2 do
  54.             monitor.write(" ")
  55.         end
  56.     end
  57. end
  58.  
  59. --This function will begin a loop that continues until one of two things happens.
  60. --Condition 1: A button contained within the provided button list has been clicked.
  61. --When this condition is met, the function checks the type and state of the button, then runs functions accordingly.
  62. --Condition 2: A timer of a provided length has expired.
  63. --When this condition is met, the function runs the function provided as the timeoutFunction parameter.
  64. --If you want to continually process button inputs, I suggest puttng this function within a while loop.
  65. --The inputs should be the following:
  66. --monitor: the monitor that the buttons are being displayed on.
  67. --buttonList: the a list of the buttons to check.
  68. --timeoutLength: the length in seconds of the timeout timer. Can have a precision of 0.05 seconds.
  69. --timeoutFunction: the function to run when the timeout timer expires.
  70. function  checkButtonsPressed(monitor, buttonList, timeoutLength, timeoutFunction)
  71.     local pressedCheck = false
  72.     if timeoutLength > 0 then
  73.         local timeoutTimer = os.startTimer(timeoutLength)
  74.     end
  75.     while true do
  76.         local event, par1, par2, par3 = os.pullEvent()
  77.         if event == "timer" then
  78.             if par1 == timeoutTimer then
  79.                 timeoutFunction()
  80.                 break
  81.             end
  82.         elseif event == "monitor_touch" then
  83.             local i
  84.             for i = 1, #buttonList do
  85.                 if par2 >= buttonList[i]["x"] and par2 <= buttonList[i]["x"] + #buttonList[i]["label"] + 2 and par3 >= buttonList[i]["y"] and par3 <= buttonList[i]["y"] + 2 then
  86.                     if buttonList[i]["toggleable"] then
  87.                         if buttonList[i]["toggled"] then
  88.                             buttonList[i]["toggled"] = false
  89.                             drawButtons(monitor, buttonList)
  90.                             buttonList[i]["function"](true)
  91.                             pressedCheck = true
  92.                             break
  93.                         else
  94.                             buttonList[i]["toggled"] = true
  95.                             drawButtons(monitor, buttonList)
  96.                             buttonList[i]["function"](false)
  97.                             pressedCheck = true
  98.                             break
  99.                         end
  100.                     else
  101.                         buttonList[i]["toggled"] = true
  102.                         drawButtons(monitor, buttonList)
  103.                         sleep(0.25)
  104.                         buttonList[i]["function"]()
  105.                         buttonList[i]["toggled"] = false
  106.                         drawButtons(monitor, buttonList)
  107.                         pressedCheck = true
  108.                         break
  109.                     end
  110.                 end
  111.             end
  112.             if pressedCheck then
  113.                 break
  114.             end
  115.         end
  116.     end
  117. end
Add Comment
Please, Sign In to add comment