Reputation: 1183
Using Libgdx, I'm attempting to find a way to check if the mouse is hovering over a given TextField object.
Buttons in libgdx give the ability to check if the mouse is hovering over it with the #isOver() method. Unfortunately, TextFields do not have this.
You can, however, add a new inputListener where it can invoke events if the TextField if the mouse is hovering, etc. but doesn't allow checking if it is being hovered over.
How would I go about implementing an #isOver() method for TextFields? Thank you.
Upvotes: 3
Views: 1793
Reputation: 13571
You can use hit() method from the Actor class to check if mouse is in TextField (that extending Actor) bounds.
To get mouse position you should use getX() and getY() methods from Gdx.input and then translate them to the TextField's stage's coordinates by using screenToStageCoordinates():
TextField textField;
...
//in render() method
Vector2 mouseScreenPosition = new Vector2(Gdx.input.getX(), Gdx.input.getY());
Vector2 mouseLocalPosition = textField.screenToLocalCoordinates(mouseScreenPosition);
if(textField.hit(mouseLocalPosition.x, mouseLocalPosition.y, false) != null) {
//the mouse is over the TextField
}
Upvotes: 3