Tuesday, 29 January 2013

#3.5 Tutorial (LibGDX & MTX) Test_04_InputIntent 1


Sometimes we need to detect drag direction of user (without removing finger off the screen or vice-versa), Also we may need to detect which area of the screen being touched at the moment. The solution is InputIntent Class, extremely easy to use.





















HOW TO SET UP INPUTINTENT

So simple, There are two way of detectiog touches, registering ActorGestureListener or by implementing InputProcessor.

(Below a screen shot) for set up of ActorGestureListener of stage. First I constructed the InputIntent, then I set necessary methods to make calculations

touchDown
inputIntent.setTouchInitials(x, y);  // Set initial position of finger, mouse, etc...

pan (Pan is dragging, I closed method in screenshot, because it was too long)
inputIntent.setTouchCurrents(x, y); // Set current position of finger, mouse, etc...

touchUp
inputIntent.reset(); // Reset when finger, mouse is off the screen
































If you implementing InputProcesser everything is similar. (I have used ActorGestureListener in my test codes), this is just to show who uses InputProcessor

WARNING 1!
AbstractScene already registers input processor  Gdx.input.setInputProcessor(stage);

If you want to use multiple processors for stage and current screen which implements input processor, you have to overrite your inputprocessor settings by using inputmultiplexer. Than construct your inputintent in the constructor.

        InputMultiplexer plex = new InputMultiplexer();
        plex.addProcessor(this);
        plex.addProcessor(getStage());
        Gdx.input.setInputProcessor(plex);
        //
        inputIntent = new InputIntent();



The rest is similar to actorgesturelistener.






HOW TO USE INPUT INTENT

It is so simple.

WARNING 2! 
If you use implement inputProcessor instead of using ActorGestureListener, defaultly libgdx world Y-axis opposite as you know if you dont do something about it, so DOWN means will be UP, UP means will be DOWN.

In my example I use ActorGestureListener to detect touches so everything is normal for my example.
























As you cans see the example (Up Screenshot).It is very simple, inputIntent.getDirectionIntent() will return current direction (DirectionIntent), just check for direction and do whatever you want. In my case, I turn the arrow on left and rewrite the text at the bottom due to current drag direction.

DirectionIntent could be TOUCH_IDLE, TOUCH_D_UP, TOUCH_D_DOWN, TOUCH_D_LEFT, TOUCH_D_RIGHT

You can also check the current touch area see the rest of class, observe the codes and read the documentation by hovering on methods. Everything is easy to use and understand.



No comments:

Post a comment