getting the id of ui.newButton

I want to have one handler function for many buttons:

local btnSelected = function( event )  
 print("phase: " .. event.phase)  
 if (event.phase == "ended") then  
 print("button id: ")  
 print(event.id)  
 end  
end  
  
local level1Btn = ui.newButton{  
 default = "image.png",  
 over = "image.png",  
 id = "level1",  
 onRelease = btnSelected  
}  

The id that gets printed is this: userdata: 0xc1a3c
Instead of “level1”. Why? [import]uid: 52127 topic_id: 10199 reply_id: 310199[/import]

Because the onRelease/onPress handlers forward the touch-event, which according to the doc has the following id attribute:

event.id is a unique identifier of the chosen touch so that you can distinguish between multiple touches across events (i.e. the different events sent across multiple listener calls). The id uniquely identifies a given finger touching the screen as that touch changes state, generating new touch events.

The onEvent handler, however, manufactures its own event structure and there the event.id is a copy of your params.id.

There is also no way to get to the target-object from the event that is passed thru onRelease/onPress/onEvent, which is very inconvenient. Furthermore, the event structures are very different,

No idea what the deeper philosophical reasons are between these inconsistent event-models…

I ended-up adding an event.target attribute in the touch event-handler to all onRelease/onPress/onEvent handlers such that I can easily get to all the target, i.e. the button, attributes, like id/_id.

-FrankS.
[import]uid: 8093 topic_id: 10199 reply_id: 37235[/import]

It seems that not being able to determine which object generated an event is quite a shortcoming in Corona.

I don’t fully understand your solution though. Can you explain what I need to do to my code? [import]uid: 52127 topic_id: 10199 reply_id: 37237[/import]

The easiest solution is to use the onEvent handler instead with the associated “release” phase, like:

[lua]local btnSelected = function( event )
print("phase: " … event.phase)
if (event.phase == “release”) then
print("button id: ")
print(event.id)
end
end

local level1Btn = ui.newButton{
default = “image.png”,
over = “image.png”,
id = “level1”,
onEvent = btnSelected
}[/lua]

That should give you your “id” in the btnSelected handler.

See the newButtonHandler() source code in ui.lua for more details.

-FrankS.
[import]uid: 8093 topic_id: 10199 reply_id: 37238[/import]

That doesn’t work, because if you use “onEvent”, the phase is always equal to “press”. But I specifically want a “release” listener, because the user can press a button but then move off of it and decide to not “release” the button. [import]uid: 52127 topic_id: 10199 reply_id: 37309[/import]

Not sure what you’re referring to, but the code that I posted prints out:

[lua]phase: press

phase: release
button id:
level1[/lua]

onEvent gives you two event.phase values: press & release

if you only want to act on “release” in onEvent, use an if-statement like you did to print the id, and simply ignore the “press”.

You cannot get to button._id thru the onRelease handler, unless you use globals or close over the button reference or change the ui.lua code.

-FrankS.

[import]uid: 8093 topic_id: 10199 reply_id: 37312[/import]

Are you sure? I copied and pasted your code, and all I get is this:

phase: press  

Are there different versions of ui.lua floating around? I can never tell if I have the most updated versions of all these different libraries. Looks like I have 1.5 [import]uid: 52127 topic_id: 10199 reply_id: 37313[/import]

A single main.lua with:

[lua]local ui = require(“ui”)

local btnSelected = function( event )
print("phase: " … event.phase)
if (event.phase == “release”) then
print("button id: ")
print(event.id)
end
end

local level1Btn = ui.newButton{
default = “image.png”,
over = “image.png”,
id = “level1”,
onEvent = btnSelected
}[/lua]

yields after a button click:

[lua]phase: press

phase: release
button id:
level1[/lua]

… with ui.lua v1.5

-FrankS.

[import]uid: 8093 topic_id: 10199 reply_id: 37330[/import]

You’re right, it does work in a plain main.lua file. Turns out that simply inserting the button into a scrollView changes the event handler. If you do this:

local ScrollView = require("scrollView")  
...  
local scrollView = ScrollView.new{ top=topBoundary, bottom=bottomBoundary };  
  
scrollView:insert(level1Btn);  

it doesn’t work anymore (you get the “press” event but not the “release”. Do you happen to know the workaround for that? [import]uid: 52127 topic_id: 10199 reply_id: 37336[/import]

Not sure as the ScrollView has its own touch handler, which takes over by claiming the focus.

It should pass on the events, though, when you’re not scrolling.

If you push your button while being careful not to scroll, do you still get only the press and not the release?

-FS.
[import]uid: 8093 topic_id: 10199 reply_id: 37340[/import]

Yes, no matter what, even if it doesn’t scroll at all, the release event is not received. Ugh. I can’t believe I’ve spent so many hours simply trying to handle a button release… [import]uid: 52127 topic_id: 10199 reply_id: 37342[/import]