Search code examples
javaandroidlibgdxgesturedetector

GestureDetector and InputListener in libgdx


I'm new to libgdx and what I'm trying to achieve here is that I want my GestureDetector to work at the same time with InputListener. I have two buttons on the left and I need them to keep reacting even if I start swiping at the same time(multitouch). I used InputMultiplexer but it doesn't work that way I need. I checked all the return values of InputListener and GestureDetector and everything that I need returns true, also GestureInputListener is a class that implements GestureDetector.GestureListener. I only used fling in it. Both GestureInputListener and InputListener work but not at the same time. Could you help me with that? links, ideas. Thanks. Code below:

inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(new GestureDetector(gestureInputListener));
Gdx.input.setInputProcessor(inputMultiplexer); 

Solution

  • then instead of using input multiplexer use code as:

    public class MyGestureDetector extends GestureDetector {
        public MyGestureDetector(GestureListener listener) {
            super(listener);
        }
    
        @Override
        public boolean touchUp(float x, float y, int pointer, int button) {
            super.touchUp(x, y, pointer, button);
    
            // Your Code Here
            return true;
        }
    }
    

    similarly other functions such as touchdown etc can be added here don't forget to call super from functions as this will make various function like fling and tap work

    Edit:

    change the implementation to

    public class MyGestureDetector extends GestureDetector {
        private Stage stage;
        public MyGestureDetector(GestureListener listener,Stage stage) {
            super(listener);
            this.stage = stage;
        }
    
    
        @Override
        public boolean keyDown(int keycode) {
            stage.keyDown(keycode);
            super.keyDown(keycode);
            return false;
        }
    
        @Override
        public boolean keyUp(int keycode) {
            stage.keyUp(keycode);
            super.keyUp(keycode);
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean keyTyped(char character) {
            // TODO Auto-generated method stub
            stage.keyTyped(character);
            super.keyTyped(character);
            return false;
        }
    
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            stage.touchDown(screenX, screenY, pointer, button);
            super.touchDown(screenX, screenY, pointer, button);
            return false;
        }
    
        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            stage.touchUp(screenX, screenY, pointer, button);
            super.touchUp(screenX, screenY, pointer, button);
            return false;
        }
    
        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            // TODO Auto-generated method stub
            stage.touchDragged(screenX, screenY, pointer);
            super.touchDragged(screenX, screenY, pointer);
    
            return false;
        }
    
        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            stage.mouseMoved(screenX, screenY);
            super.mouseMoved(screenX, screenY);
            return false;
        }
    
        @Override
        public boolean scrolled(int amount) {
            stage.scrolled(amount);
            super.scrolled(amount);
            // TODO Auto-generated method stub
            return false;
        }
    
    
    }
    

    Here stage is the scene2d Stage and there might be some error of float or int in parameters depending on the libgdx version you are using this is written in 0.9.9 nightlies

    P.S.- This implementation is customised as per your problem but it should be tried that every case to be handled by returning true or false as explained before