This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kouksi44's profile picture

Button api click detection

Started by Kouksi44, 18 August 2014 - 07:24 PM
Kouksi44 #1
Posted 18 August 2014 - 09:24 PM
Hey Forum,

I´ve been writing on an own button api for my future projects :)/>

I tried to make this button api object oriented as a came across this topic the last days.

Now I came to a point where I really don´t know how to go on.

In my api theres a general function that checks if a click was made on a button or not.
This function loops through a table where I save a buttons that are currently active.

But what should this function return so that my main program can toggle the button to fire the action set on the button.

I know I could easily just fire the action of the button inside by detect function but the user should be able to use one button for several purposes !

This is the code of the detect function :

function detectClick(posX,posY)
for i=1,#button_list do
if
  posX<=button_list[i][2]+#button_list[i][1] and
  posX>= button_list[i][2] and
  posY <= button_list[i][3]+button_list[i][4] and
  posY >=button_list[i][3]
   then
	return button_list[i][1]
  
end
end
end

Currently the function returns simply the name of the button that was triggered, but I failed to get the function from the button object just by tha name of the button.

I hope someone has an idea :)/>

Kouksi44
Bomb Bloke #2
Posted 18 August 2014 - 10:57 PM
I'd just return the name. In the main program, stick all the relevant functions in a table against the button names.

Eg:

local funcs = {}

function funcs["buttonName"]
  -- Do stuff
end

local clickedButton = yourAPI.detectClick(whatever, whatever)

if clickedButton and funcs[clickedButton] then funcs[clickedButton]() end
Edited on 18 August 2014 - 08:58 PM
Kouksi44 #3
Posted 18 August 2014 - 11:18 PM
Oh yeah thank yo that should work :)/>

And by the way: Is

function funcs["buttonName"]
  -- Do stuff
end

With these brackets another possibility to declare functions?

So the function is stored at the index "buttonName" ?
Bomb Bloke #4
Posted 18 August 2014 - 11:43 PM
Yeah, you just keep dumping functions in there, one for each button you want to attach a function to:

function funcs["buttonName1"]()  -- Forgot the extra brackets before...
  -- Do stuff
end

function funcs["buttonName2"]()
  --Do more stuff
end

function funcs.buttonName3()  -- Same thing, different syntax.
  -- Moo.
end

-- etc

Is that what you're asking…?
Edited on 18 August 2014 - 09:43 PM
Kouksi44 #5
Posted 19 August 2014 - 09:36 AM
Yup thats it :)/> Thank you very much that dhould do it ! :)/>