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.

I want to be able to listen for a right mouse click onto a TextButton. Is this possible without removing the default ClickListener of the TextButton? If yes, how? Or should I use a different listener?

share|improve this question

2 Answers 2

There are three main ways (that I know of) to obtaining input in LibGDX.
The first is as you said, changing the ClickListener, the second will be setting the setting the current screen as an implementation of InputProcessor , and the third will be obtaining the mouse click through a new class, or a sub-class to get the input.
I'll elaborate on each:

  1. The first way: Changing the ClickListener. Pretty straight forward, lets say you have a text button named onOffSwitch, if you'd want to recieve input on it, you'd write the following code:
    onOffSwitch.addListener(new ClickListener(){ });
    This will create a new ClickListener to use to recieve any input from the button.
    When you use this you can add method like enter to see whenever the mouse is hovering over the buttton, or when the player is touching the button.
  2. The second way: Setting a new InputProcessor, this will basically create an inputprocessor which will be your entire screen.
    I don't recommend using this since it requires more than one methhod and is often not needed, all that's needed is a little digging around on how to get the listeners you need.
    To implement this way, you need to make your class implement the InputProcessor and implement the needed methods.
  3. The third and last way, which I assume is the one you'll use is the following:
    To use this way you'll create a new instance of InputProcessor and apply it to Gdx.input:

    class AwesomeGameClass {
    public void init() {
        Gdx.input.setInputProcessor(new InputProcessor() {
            @Override
            public boolean TouchDown(int x, int y, int pointer, int button) {
                if (button == Input.Buttons.LEFT) {
                    //check if the x and y coordinates are the same as your text button (using TextButton.getPrefWidth() & TextButton.getPrefHeight()) and if so then invoke the onMouseDown method.
                    return true;
                }
                return false
            }
        });
    }
    private void onMouseDown() {
    }
    

    }

The code I got from here, and you can see what else there is there that I might have missed. HERE Except that you can read up on materials in the LibGDX github, which is located HERE Or look at the data concerning TextButtons in HERE

share|improve this answer
    
Thanks for your answer, it sent me on the right path, but the solution was even easier ;) –  StrongJoshua Jun 26 at 19:48
    
Thanks for the answer, gave me some new information. –  Aon GoltzCrank Jun 26 at 19:53
up vote 5 down vote accepted

This is actually really simple. All you have to do is add another ClickListener which listens for Right Clicks (the default only listens to left clicks). To do this all you have to do is this:

someButton.addListener(new ClickListener(Buttons.RIGHT)
{
    @Override
    public void clicked(InputEvent event, float x, float y)
    {
        //do whatever
    }
});

Note that this also works for middle clicks (Buttons.MIDDLE)

share|improve this answer

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.