多次触发事件 - LWJGL
Event fired multiple times - LWJGL
我正在制作一个 3D 游戏引擎,我已经用自己的方式处理触发的事件并将它们传递给每个 GuiComponent
直到它返回 true(已处理),或者它通过它们全部,在这种情况下它会处理事件。
问题是,触发的事件不止一个。我已经确定我没有创建 eventHandler
class 的多个实例。我虽然 LWJGL
中的 Mouse
class 可能会触发多次?如果不是,当前设置有什么问题?谢谢
事件处理程序:
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);
}
}
图形按钮:
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;
}
}
界面面板:
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();
}
}
图形组件:
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
class 内置于 LWJGL
中,所以这似乎是问题所在。有办法解决这个问题吗?
好的,在四处搜索之后,我发现了这个线程 here,它解决了这个问题。我不完全确定为什么,我将查看 Mouse
class 的文档,但这似乎有效并且只触发一次事件,它给了我一个已发布的事件,这太棒了。希望这对将来的其他人有帮助!互联网上的任何地方都没有关于此问题的任何内容。
我正在制作一个 3D 游戏引擎,我已经用自己的方式处理触发的事件并将它们传递给每个 GuiComponent
直到它返回 true(已处理),或者它通过它们全部,在这种情况下它会处理事件。
问题是,触发的事件不止一个。我已经确定我没有创建 eventHandler
class 的多个实例。我虽然 LWJGL
中的 Mouse
class 可能会触发多次?如果不是,当前设置有什么问题?谢谢
事件处理程序:
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);
}
}
图形按钮:
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;
}
}
界面面板:
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();
}
}
图形组件:
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
class 内置于 LWJGL
中,所以这似乎是问题所在。有办法解决这个问题吗?
好的,在四处搜索之后,我发现了这个线程 here,它解决了这个问题。我不完全确定为什么,我将查看 Mouse
class 的文档,但这似乎有效并且只触发一次事件,它给了我一个已发布的事件,这太棒了。希望这对将来的其他人有帮助!互联网上的任何地方都没有关于此问题的任何内容。