Understanding the input processor.

Started by
1 comment, last by Serapth 9 years, 6 months ago

I am somewhat lost as to how the LibGDX input processor works and how to implement it for what i want. I simple want to detect if a touch was made or released in a specific class. Something as simple as "if (touchUp) { //do stuff }".

But the input processor works differently, it just runs the code there when a touch is released. The only way i can think of doing what i want feels hacky: create globals in my class and set these to true from the InputProcessor and at the end of the update cycle in my class I put them false again.


//MyInputProcessor

@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        MyClass.touched = true;
        return false;
    }


    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        MyClass.released = true;
        return false;
    }


//MyClass


public static touched = false;
public static released = false;


@Override
public void render(float delta) {
if (touched)
  //do stuff
if (released)
  //do stuff


//end of update
touched = false;
released = false;
}

It just doesn't feel right to do it this way. If i want to do all the touchup/touchdown code inside the InputProcessor class i have to make tons of fields in MyClass global. Am i missing something? I just need something like "if (getInputProcessor().getTouchDown) { //Do stuff }".

Also, if i want to have multiple pointers for fingers (Which is highly unlikely) the above example does not work.

Advertisement

I don't have experience with Libgdx, but looking at the documentation I see InputProcessor is an interface, so I guess the intended use is to implement that interface directly in the class that needs input information. Instead of making MyInputProcessor and MyClass, just make MyClass be an InputProcessor too.

The documentation also says the methods from the interface will be called before the call to render, so if you need the touch information for the render part you'll need some variables to hold the information. It will be similar to what you wrote, but don't need to be globals or statics, they could be a private attribute on the class.

Also, I'm not sure you need "touched" and "released" booleans, use just "touched" and set it to false on touchUp event. Maybe you want to also record the state of the "touched" variable in the previous iteration, so you can detect the exact moment where it was touched/released instead of just knowing the current state.

I cover this in my LibGDX tutorial series although it was actually in the keyboard example I did it.

Basically, if I am reading you correctly, you want to poll input (on demand) as opposed to event driven?

Good news is, you can do this. Yeah you essentially would have to track all these values ( position, finger, etc ), but fortunately LibGDX already does exactly that for you. Take a look at the Input class

To get touch position on of first finger ( remember, multi touch, multiple pointers ), you simply do:

Gdx.input.getX(0); // 0 is 1st finger. 1 for second, 2 for third, etc

This returns the X coordinate of the current touch if applicable. Check isTouched or isTouch(n) before calling getX/Y.

Using the input class you can easily poll input.

This topic is closed to new replies.

Advertisement