Jump to content




Button api click detection


  • You cannot reply to this topic
4 replies to this topic

#1 Kouksi44

  • Members
  • 89 posts
  • LocationMunich, Germany

Posted 18 August 2014 - 08: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

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 August 2014 - 09: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 by Bomb Bloke, 18 August 2014 - 09:58 PM.


#3 Kouksi44

  • Members
  • 89 posts
  • LocationMunich, Germany

Posted 18 August 2014 - 10: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" ?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 August 2014 - 10: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 by Bomb Bloke, 18 August 2014 - 10:43 PM.


#5 Kouksi44

  • Members
  • 89 posts
  • LocationMunich, Germany

Posted 19 August 2014 - 08:36 AM

Yup thats it :) Thank you very much that dhould do it ! :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users