事件多次被解雇 - LWJGL

时间:2016-08-15 10:12:00

标签: java opengl events mouse lwjgl

我正在制作一个3D游戏引擎,我已经按照自己的方式处理触发的事件并将它们传递给每个GuiComponent,直到它返回true(处理),或者它通过它们全部,在这种情况下,它处理事件。

问题是,事件不止一次。我已经确定我没有创建eventHandler类的多个实例。我可能Mouse中的LWJGL类可能会多次触发?如果没有,那么当前的设置是什么?感谢

事件处理程序:

public abstract interface EventHandler {
    public abstract boolean handleEvent(Event event);
}

事件:

public class Event {

    public enum events {
        MOUSEPRESSED, MOUSEDRAGGED, MOUSEMOVED
    }

    private events event;

    public float x, y;

    public Event(events event, float x, float y) {
        this.x = x;
        this.y = DisplayManager.HEIGHT - y;
        this.event = event;
    }

    public Event setPos(float x, float y) {
        this.x = x;
        this.y = DisplayManager.HEIGHT - y;
        return this;
    }

    public void setEvent(events event) {
        this.event = event;
    }

    public boolean equals(events event) {
        return event.equals(this.event);
    }
}

GuiButton:

public class GuiButton extends GuiComponent implements EventHandler {

    @Override
    public boolean passEvent(Event event) {
        if (!visible) return false;
        if (event.equals(Event.events.MOUSEPRESSED) && super.passEvent(event)) {
            texture = texture2;
            return handleEvent(event);
        } else texture = texture1;
        return false;
    }

    @Override
    public boolean handleEvent(Event event) {
        return false;
    }
}

GuiPanel:

public class GuiPanel extends GuiComponent {

    protected List<GuiComponent> components = new ArrayList<GuiComponent>();

    @Override
    public boolean passEvent(Event event) {
        for (GuiComponent c : components) {
            if (c.passEvent(event)) return true;
        }
        return false;
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        for (GuiComponent c : components) c.setVisible(visible);
    }

    @Override
    public void update() {
        for (GuiComponent c : components) {
            if (c.isVisible()) c.update();
        }
    }

GuiComponent:

public abstract class GuiComponent extends Rectangle {

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isVisible() {
        return visible;
    }

    public boolean passEvent(Event event) {
        if (this.contains(event.x, event.y)) return true;
        return false;
    }

    public abstract void update();
}

组件创建:

    main = new GuiPanel(0, 0, 100, DisplayManager.HEIGHT, "null", loader);
    guiCompoents.add(main);

    entityPanel = new GuiPanel(30, 100, 100, 300, "null", loader);

    b2 = new GuiButton(100, 100, 100, 30, "Entitiesn", "Entitiesh2", loader) {
        public boolean handleEvent(Event event) {
            if (event.equals(Event.events.MOUSEPRESSED)) {
                System.out.println("button2 recieved event");
                createEntity(tree1);
                return true;
            }
            return false;
        }
    };
    entityPanel.add(b2);
    entityPanel.setVisible(false);

    b1 = new GuiButton(30, 100, 100, 30, "Entitiesn", "Entitiess", loader) {
        public boolean handleEvent(Event event) {
            if (event.equals(Event.events.MOUSEPRESSED)) {
                System.out.println("button1 recieved event");
                entityPanel.setVisible(!entityPanel.isVisible());
                return true;
            }
            return false;
        }
    };
    main.add(b1);

    main.add(entityPanel);

编辑: 好的,经过进一步搜索,我尝试了下面我正在检测事件发生的地方:

if (Mouse.getDX() != 0 || Mouse.getDY() != 0) {
        event.setEvent(Event.events.MOUSEMOVED);
        event.setPos(Mouse.getX(), Mouse.getY());
        passEvent(event);
    }

事件多次被触发。 Mouse类内置于LWJGL,因此这似乎是问题所在。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

好的,所以在搜索之后,我遇到了这个线程here,它解决了这个问题。我不完全确定原因,我将查看Mouse类的文档,但这似乎有效并且只触发事件一次,它给了我一个发布的事件,这很棒。希望这有助于将来的其他人!在互联网上的任何地方都没有关于这个问题。