I believe there is no convenient solution. I can think of only one possible way to achieve such behavior - adding an InputListener
to the touchPad
and correct InputEvent
coordinateds before other listeners are notified:
final Touchpad touchpad = ...;
// insert the listener before other listeners
// to correct InputEvent coordinates before they are notified
touchpad.getListeners().insert(0, new InputListener() {
private Vector2 tmpVec = new Vector2();
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (touchpad.isTouched()) return false;
restrictAlongX(event);
return true;
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
restrictAlongX(event);
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
restrictAlongX(event);
}
private void restrictAlongX(InputEvent inputEvent) {
// convert local centerY to the stage coordinate system
tmpVec.set(0f, touchpad.getHeight() / 2);
touchpad.localToStageCoordinates(tmpVec);
// set stageY to the touchpad centerY
inputEvent.setStageY(tmpVec.y);
}
});
Of course this doesn't look pretty and maybe someone will suggest a cleaner solution. You should be aware that it changes InputEvent
coordinates, and the same InputEvent
will be used to notify Actors
after the touchpad
. But I think that's acceptable in most cases, and other than that, this should work.