androidlibgdxscene2dtouchpad

How to restrict touchpad knob movement along horizontal axis only


I want my touchpad knob in LibGDX to be able to move right or left, but not up or down. Here is my code:

Drawable touchBackground = touchpadSkin.getDrawable("touchBackground");
touchKnob = touchpadSkin.getDrawable("touchKnob");
touchpadStyle.background = touchBackground;
touchpadStyle.knob = touchKnob;
touchKnob.setMinHeight(80);
touchKnob.setMinWidth(30);

touchpad = new Touchpad(0.1f, touchpadStyle);
touchpad.setBounds(10, 100, 130, 130);
touchpad.getResetOnTouchUp();

ScrollPane scrollPane=new ScrollPane();
touchpad.setPosition(70,70);
touchpad.setOriginX(200);

stage.addActor(touchpad);
Gdx.input.setInputProcessor(stage);

Solution

  • 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.