Search code examples
javaandroidlibgdxtouch-event

ClickListener touchDragged pointer always zero


while working on the listener class and implementing handling of multiple touch gesture I came across a possible bug.

The code implementation

public class MyListener extends ClickListener {
    private List<Pointer> pointers = new ArrayList<Pointer>();

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointerIndex, int button) {
        System.out.println("Listener: touch down" + pointerIndex);
        pointers.add(new ListenerPointer(x, y, button));
        event.handle();
        return super.touchDown(event, x, y, pointerIndex, button);
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointerIndex) {
        System.out.println("Listener: dragged " + pointerIndex);

        // Update the current point the user is dragging.
        for (ListenerPointer pointer : pointers) {
            if (pointer.getPointerIndex() == pointerIndex) {
                pointer.update(x, y);
                event.handle();
            }
        }
    }
}

While touching down on the screen with a new finger, and still holding the old finger on the screen the indexPointer increases. Resulting in the following log:

Listener: touch down0
Listener: touch down1

If I then move both fingers across the screen, it will only fire the touchDragged event with the pointerIndex always zero. Even though the touchDown gesture said it has a pointerIndex of 1. The log of the touchDragged is always:

Listener: dragged 0
Listener: dragged 0

I think it might be a bug in the code of LibGDX, since such a simple piece of code could not really go wrong.


Solution

  • I thought that I had carefully read the documentation of touchDragged, but what a fool I am. It states the following:

    Called when a mouse button or a finger touch is moved anywhere, but only if touchDown previously returned true for the mouse button or touch.

    I guess that super.touchDown(event, x, y, pointerIndex, button) only return true if the pointerIndex is 0. Explaining the reason why the touchDragged events did not fire for pointerIndex > 0.

    The simple fix is to make touchDown always return true