Question

I've set an InputListener on a Table with mouseMoved overridden. I'd like for the input to be received when the mouse is moved anywhere on the screen, but right now it only receives input when the mouse moves over the any of the widgets inside the table (see red box in screenshot).

font = new BitmapFont( );
style = new Label.LabelStyle( font, Color.WHITE );

xLabel = new Label( "x: ", style );
yLabel = new Label( "y: ", style );
xNum = new Label( "0", style );
yNum = new Label( "0", style );

this.setFillParent( true );
System.out.println( this.getWidth() + ", " + this.getHeight() );

this.addListener(new InputListener() {
    @Override
    public boolean mouseMoved(InputEvent event, float x, float y) {
        xNum.setText( String.valueOf( x ) );
        yNum.setText( String.valueOf( y ) );
        return true;
    }
});

this.left().top();
this.add(xLabel).padLeft( 5 );
this.add( xNum );
this.row();
this.add( yLabel ).padLeft( 5 );
this.add( yNum );

this.debug();

enter image description here

Was it helpful?

Solution

Most libgdx Actor's handle inputs by default,Tabledoes not.
The reason: In the constructor of
TablesetTouchable(Touchable.childrenOnly);is called, which disables touchable for this widget, but enables it for its children. This is why input is only received over theTable`s widgets.

setTouchable( Touchable.enabled ); solves this problem

OTHER TIPS

Thats the regular function if you bind an InputListener to an widget. Sure it's bound to the Widget and nothing more. And so it's bound to the size of the widget.

If you want to listen to all events you need to write an InputProcessor and handle the events yourself. Use the InputProcessor Interface and add the processor to the gdx.

Gdx.input.setInputProcessor(yourProcessor);

The Stage is already an input processor and fires all events to the actors that are at that position. You can use a Stage for a good eventhandling for example to get to know if some tile or some character is clicked. But if you like more complex things you create your own InputProcessor. But if you like to you can add an Listener to the Stage and get all move events. This meight be a solution too. stage.addListener(listener);

  1. Also take a look at Scene2D at the listener and event section for more clarification.
  2. Wiki InputProcessor

Else resize your Table to fit the screen size and you get all movements of the mouse. But i wouldnt recommend that. Everything you need to know for that you can find at the git wiki from the Table layout

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top