Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureDetector in libgdx

I am new in libgdx. I have the following problem. I want to do pinch zooming for my game. For that I have GestureDetectorListener class in my GameStage.

In GameStage I have the following code for GestureDetector:

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.setCamera(camera);


GestureDetector gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, new GestureDetectorListener());
Gdx.input.setInputProcessor(gestureDetector);

class GestureDetectorListener implements GestureListener {
    float initialScale = 1;

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        initialScale = camera.zoom;
        return true;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean longPress(float x, float y) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        return true;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        float ratio = initialDistance / distance;
        camera.zoom = initialScale * ratio;
        return true;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
            Vector2 pointer1, Vector2 pointer2) {
        // TODO Auto-generated method stub
        return true;
    }

}

Also I have set InputListener for the Actors of my stage, in order to drag them. The problem is that, when I set InputProcessor to GestureDetector, dragging of my Actors is not working. I want to have a pinch zooming with camera and GestureDetector, but it will not have an influence on dragging. How can I do that?

Any help please! Thanks.

like image 818
Karmar Avatar asked Feb 12 '13 08:02

Karmar


2 Answers

CodeNoob is on the right path, but I think you may want to structure things differently.

You can use an InputMultiplexer to install both a global GestureListener and your stage's listener:

GestureDetector gd = ...
Stage myStage = ...
InputMultiplexer im = new InputMultiplexer(gd, myStage); // Order matters here!
Gdx.input.setInputProcessor(im);

The other thing to watch out for is the return values on all the boolean methods in your input listeners. Any one that returns "true" will mark the event as "handled" and the multiplexer will not pass the event on to its peer. Also, the order of the input processors in the multiplexer makes a difference (as it may hide events from the underlying inputProcessor). So, the auto-generated stubs returning 'true' in your example, will "eat" a bunch of events. If this handler is first in the multiplexer, that will hide events from the stage.

like image 134
P.T. Avatar answered Sep 20 '22 11:09

P.T.


You need to create a InputMultiplexer InputMultiplexer myInputMultiplexer = new InputMultiplexer()

Then you add your both GestureDetector and InputListener to the InputMultiplexer (myInputMultiplexer.addProcessor()).

And at the End, you have to set the multiplexer as the main InputProcessor like this: Gdx.input.setInputProcessor(myInputMultiplexer)

like image 34
CodeNoob Avatar answered Sep 21 '22 11:09

CodeNoob