Search code examples
libgdxgesturespanonfling

Determining a fling from a pan in libgdx


I'm developing a game with LibGDX and I'm having trouble determining a fling from a pan.

My GestureListener:

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    //if (velocityX > 1000f) {
    // I can use this to exclude slow pans from the executing a fling
        System.out.println("Flinged - velocityX: " + velocityX + ", button: " + button);
    //}
    return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    // but there doesn't seem to be a way to have this not fire on a fling
    System.out.println("Panned - deltaX: " + deltaX);
    return false;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
    System.out.println("Pan Stop - pointer: " + pointer + ", button: " + button);
    return false;
}

The problem is that if both pan and fling fire. I understand that a fling is basically a fast pan, but I need to be able to determine between the two gestures so I can handle each one separately.

A succinct way of asking is, how do I provide unique actions for fling and pan?


Solution

  • The most reasonable solution for this in my opinion is to use longPress to set some boolean flag to 'enter the panning mode' when swiping without long press will only be about making fling.

    This is how I see this:

        //the flag for checking if pan mode entered
        boolean PAN = false;
    
        @Override
        public boolean longPress(float x, float y) {
            PAN = true;
            return false;
        }
    
        @Override
        public boolean fling(float velocityX, float velocityY, int button) {
            if(!PAN) {
                System.out.println("Flinged - velocityX: " + velocityX + ", button: " + button);
            }
            return false;
        }
    
        @Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {
            if(PAN) {
                System.out.println("Panned - deltaX: " + deltaX);
            }
            return false;
        }
    
        @Override
        public boolean panStop(float x, float y, int pointer, int button) {
            if(PAN) {
                PAN = false;
                System.out.println("Pan Stop - pointer: " + pointer + ", button: " + button); 
            }
            return false;
        }
    

    Of course if the longPress is too long for your purposes you can change it by using setLongPressSeconds(float longPressSeconds) method of the GestureDetector instance