getting the id of ui.newButton

I want to have one handler function for many buttons:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
}

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.

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?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
}

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.

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

1
2
3
4
5
phase: press
 
phase: release
button id: 
level1

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

1
phase: press

A single main.lua with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
}

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:

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

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.

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...

views:1556 update:2011/10/1 9:04:19
corona forums © 2003-2011