LibGDX - 防止Ui Actor(Window)中的滚动事件无效,但其他事件有效

时间:2015-12-12 01:34:33

标签: java android events libgdx

与标题中一样,当您想要阻止某些事件传播时,您可以自由选择,除了一个“滚动”事件。它不行。有人可以告诉我为什么吗?

Window abc = new Window("Tools", skin);
abc.setY(5);
abc.setResizable(false);
abc.setMovable(false);
abc.setPosition(Gdx.graphics.getWidth() - 300, 0);
abc.setSize(300, Gdx.graphics.getHeight());
addActor(abc);

abc.addCaptureListener(new InputListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        return true; //it will stop event from being propagate
    }

    public boolean scrolled(InputEvent event, float x, float y, int a) {
        return true; //it wont stop event from being propagate (because method isn't even called!
    }
});

abc.addListener(new InputListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        return true; //it will stop event from being propagate
    }

    public boolean scrolled(InputEvent event, float x, float y, int a) {
        return true; //it wont stop event from being propagate (because method isn't even called!
    }
});

除此之外,addCaptureListener和addListener之间有什么区别?

1 个答案:

答案 0 :(得分:0)

LibGDX UI编码很糟糕..是谁做的?下面的代码是滚动错误的解决方法。这很丑陋,因为违反了libgdx-ui规则,但这些规则也很丑陋......

编辑(新解决方案)

abc.addListener(new InputListener() {
    public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) {
        setScrollFocus(abc);
    }

    public void exit (InputEvent event, float x, float y, int pointer, Actor fromActor) {
        setScrollFocus(null);
    }

    public boolean scrolled(InputEvent event, float x, float y, int a) {
        return true; //now it works
    }
});

(旧解决方案)

public class FixedStage extends Stage {
    Vector2 tempCoords = new Vector2();

    public FixedStage(Viewport viewport) {
        super(viewport);
    }

    @Override
    public boolean scrolled (int amount) {
        screenToStageCoordinates(tempCoords.set(Gdx.input.getX(), Gdx.input.getY()));

        Actor target = hit(tempCoords.x, tempCoords.y, true);
        if (target == null) target = getRoot();

        InputEvent event = Pools.obtain(InputEvent.class);
        event.setStage(this);
        event.setType(InputEvent.Type.scrolled);
        event.setScrollAmount(amount);
        event.setStageX(tempCoords.x);
        event.setStageY(tempCoords.y);
        target.fire(event);
        boolean handled = event.isHandled();
        Pools.free(event);
        return handled;
    }
}