computercraft Button API

Started by elektrobom1, May 23, 2021, 01:42 PM

Previous topic - Next topic

elektrobom1

hi im elektrobom1 and i made a button API for CC:T monitors
to start you need to get the API to do that just run: pastebin run DutDjgR1
to use this API in your code just put
b = require("button")
at the top of your code. This will add the API into your code

how to use:
this API can get bit confusing so im gonna do my best to explain!. if you dont understand message me on discord !
discord: 9551#0001

this API provides 1 command!
and that is button()
how to use it ?:
note: this API will return true when you press a button and false if you press but not the button!
b.button(<true/var>,<pos1>,<pos2>,<length>,<height>)
 i will try to explain every part of this!
true/var = you put true if you want to use this API with a single button ! for example i want it to write hi
when i press a button but just 1 button. 

if b.button(true,<pos1>,<pos2>,<length>,<height>) == true then
print("hi")
end

pos1 = a horizontal position of starting of your text that you wanna use as button
pos2 = a vertical possition of your text that you wanna use as button
so basically if i have a monitor (m)  then im gonna make something like this

m.setCursorPos(2,3)
m.write("mycoolbutton")

then you are gonna use the number you set as position of the text as your pos1 and pos2
in this case:

if b.button(true,2,3,<length>,<height>) == true then
print("hi")
end

length = a length of your text or how long do you want your button to be
so if out button is named: mycoolbutton thats 12 characters
so lenght of the button should be set to 12

if b.button(true,2,3,12,<height>) == true then
print("hi")
end

height = how much layers does your button have ? for example if you want your button
to be over 2 lines then the are gonna set height to 2   
height and length always starts in the left top corner
so if we want out button to be 2 layers big then we are going to do:

m.setCursorPos(2,3)
m.write("mycool")
m.setCursorPos(2,4)
m.write("button")

if b.button(true,2,3,6,2) == true then
print("hi")
end
--we set the cursor to 2-3. because we always start with the top left corner!


var = you can use variable instead of the true at the begining. this is
absolutely nessesary of you want to use multiple buttons
basically what this does is that it uses the data from the variable in the API
so if you wanna have button detection in your loop do something like this
on the top of your loop:

x = {os.pullEvent("monitor_touch")}

you should have something like this

while true do
x = {os.pullEvent("monitor_touch")}
--my cool code
end

then when you want to use multiple buttons instead of writing true just put in your variable.
in this case its called: x

so for example i want to have 2 buttons one that disables redstone and one that enables.
(this is gonna be entire code!)

local b = require "button"
local m = peripheral.wrap("right") -- attaches a monitor on the right side!
local redstoneside = "left"
m.setCursorPos(1,1)
m.write("on")
m.setCursorPos(1,2)
m.write("off")
while true do
  local x = {os.pullEvent("monitor_touch")}
  if b.button(x,1,1,2,1) == true then
    rs.setOutput(redstoneside,true)
 end
  if b.button(x,1,2,3,1) == true then
    rs.setOutput(redstoneside,false)
 end
end

this should explain everything about the API
as i said if you have any questions message me on discord