Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I'm trying to create a input class that implements a InputHandler & GestureListener in order to support both Android & Desktop. The problem is that not all the methods are being called properly.

Here is the input class definition & a couple of the methods:

public class InputHandler implements GestureListener, InputProcessor{  
...
public InputHandler(OrthographicCamera camera, Map m, Player play, Vector2 maxPos) {  
...
@Override
public boolean zoom(float originalDistance, float currentDistance) {
    //this.zoom = true;
    this.zoomRatio = originalDistance / currentDistance;
    cam.zoom = cam.zoom * zoomRatio;
    Gdx.app.log("GestureDetector", "Zoom - ratio: " + zoomRatio);
    return true;
}

@Override
public boolean touchDown(int x, int y, int pointerNum, int button) {
    booleanConditions[TOUCH_EVENT] = true;
    this.inputButton = button;
    this.inputFingerNum = pointerNum;   
    this.lastTouchEventLoc.set(x,y);
    this.currentCursorPos.set(x,y);

    if(pointerNum == 1) {
        //this.fingerOne = true;
        this.fOnePosition.set(x, y);
    }
    else if(pointerNum == 2) {
        //this.fingerTwo = true;
        this.fTwoPosition.set(x,y);
    }       

    Gdx.app.log("GestureDetector", "touch down at " + x + ", " + y + ", pointer: " + pointerNum);       
    return true;
}

The touchDown event always occurs but I can never trigger Zoom (or pan among others...). The following is where I register and create the input handler in the "Game Screen".

public class GameScreen implements Screen {  
...  
   this.inputHandler = new InputHandler(this.cam, this.map, this.player, this.map.maxCamPos);
   Gdx.input.setInputProcessor(this.inputHandler);  

Anyone have any ideas why zoom, pan, etc... are not triggering?

Thanks!

share|improve this question
    
Any ideas why it's not working? What have you tried? –  Anko Dec 15 '12 at 11:47
1  
Have you tried changing if ... else if ... to simply if .... if .... This is in your touchDown event. –  Brendan May 14 '13 at 21:31
add comment

2 Answers

I'm new in Java and libgdx world, so I'm not sure is this the best solution, but I had the same problem and I solved it a bit differently, and I would say simpler way. The thing is that you will never run your app on more than one device at the same time and that's why you'll never need multiple input types at the same time.

So, you still have to create two different classes. One should implement InputProcessor interface, for desktop devices, and other GestureListener for touch devices:

public class DesktopController implements InputProcessor{
    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

...

and other something like:

public class TouchController implements GestureListener{

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }
...

But, you'll never use them both - just check on what device app is currently running and set appropriate input processor:

if(Gdx.app.getType() == ApplicationType.Desktop) {
    Gdx.input.setInputProcessor(new DesktopController(this));
}else{
    Gdx.input.setInputProcessor(new GestureDetector(new TouchController(this)));
}

This works well for me.

share|improve this answer
add comment

After doing some investigation I switched the way I setup the project. I now have a controller class following a similar design as: http://www.badlogicgames.com/wordpress/?p=2668 where Mario describes his game architecture. Go to the controller section to see how he implemented his ControllerManager as mine is a near clone.

I use the controller in the following way:

public class GameScreen implements Screen {
...
    private ControllerManager controllerManager;
    ...
    public void show() {
        InputMultiplexer inputMulti = new InputMultiplexer();
        this.controllerManager = new ControllerManager(inputMulti);

        if(Gdx.app.getType() == ApplicationType.Desktop) {
            DeskController deskController = new DeskController(this.cam, this.map, this.player, this.map.maxCamPos);
            this.controllerManager.addController(deskController);
        }
        else {
            AndroidController androidController = new AndroidController(this.cam, this.map, this.player, this.map.maxCamPos);
            this.controllerManager.addController(androidController);
        }
        Gdx.input.setInputProcessor(controllerManager.getInputMultiplexer());

My deskController class is a standard inputProcessor, and androidController is a Gesture Detector.

Hope this helps someone else who is struggling with getting inputs from multiple devices.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.