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?
2 Answers
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
)
-
1
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:
- 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 newClickListener
to use to recieve any input from the button.
When you use this you can add method likeenter
to see whenever the mouse is hovering over the buttton, or when the player is touching the button. - 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 theInputProcessor
and implement the needed methods. 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 ofInputProcessor
and apply it toGdx.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
-
\$\begingroup\$ Thanks for your answer, it sent me on the right path, but the solution was even easier ;) \$\endgroup\$ Jun 26, 2014 at 19:48