Search code examples
javaopengleventsmouselwjgl

Event fired multiple times - LWJGL


I'm making a 3D game engine, and i've made my own way of handing events fired and passing them to each GuiComponent until its either returned true (handled), or it gets through them all, in which case it disposes the event.

The issue is, the events are firing more than one. I have made sure i'm not creating multiple instances of the eventHandler class. I though perhaps the Mouse class in LWJGL fires multiple times? If not, what is wring with the current setup? Thanks

EventHandler:

public abstract interface EventHandler {
    public abstract boolean handleEvent(Event 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();
}

Component creation:

    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);

EDIT: Okay, upon further searching, I tried the following where i'm detecting events firing:

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

And the event is fired multiple times. The Mouse class is inbuilt into LWJGL, so this seems to be the problem. Is there a way to fix this?


Solution

  • Okay, so after searching around, I came across this thread here, which fixes the issue. I'm not entirely sure why, I shall take a look at the documentation for the Mouse class, but this seems to work and only fires the event once, and it gives me a released event, which is awesome. Hope this helps someone else in the future! There was nothing about this issue specifically anywhere on the internet.