Fix invalid key event handling in GLFWInput | make GUI does not extend KeyEventHandler interface anymore
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.gui;
|
||||
|
||||
import com.bartlomiejpluta.base.api.input.KeyEventHandler;
|
||||
import com.bartlomiejpluta.base.internal.gc.Disposable;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
|
||||
public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
||||
public interface GUI extends Renderable, Disposable {
|
||||
int ALIGN_LEFT = 1 << 0;
|
||||
int ALIGN_CENTER = 1 << 1;
|
||||
int ALIGN_RIGHT = 1 << 2;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.gui;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.input.Input;
|
||||
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.lib.gui.BaseWidget;
|
||||
@@ -10,15 +12,18 @@ import java.util.LinkedList;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public final class WindowManager extends BaseWidget {
|
||||
private final Input input;
|
||||
private final Deque<Window> windows = new LinkedList<>();
|
||||
|
||||
private DisplayMode displayMode;
|
||||
|
||||
public WindowManager() {
|
||||
this(DisplayMode.DISPLAY_STACK);
|
||||
public WindowManager(Context context) {
|
||||
this(context, DisplayMode.DISPLAY_STACK);
|
||||
}
|
||||
|
||||
public WindowManager(DisplayMode displayMode) {
|
||||
public WindowManager(Context context, DisplayMode displayMode) {
|
||||
this.input = context.getInput();
|
||||
|
||||
super.setSizeMode(SizeMode.RELATIVE, SizeMode.RELATIVE);
|
||||
super.setSize(1f, 1f);
|
||||
this.displayMode = displayMode;
|
||||
@@ -56,6 +61,10 @@ public final class WindowManager extends BaseWidget {
|
||||
public void open(Window window) {
|
||||
requireNonNull(window, "Window cannot be null");
|
||||
|
||||
if (windows.isEmpty()) {
|
||||
input.addKeyEventHandler(this);
|
||||
}
|
||||
|
||||
windows.addLast(window);
|
||||
window.setParent(this);
|
||||
window.onOpen(this);
|
||||
@@ -77,6 +86,10 @@ public final class WindowManager extends BaseWidget {
|
||||
var window = windows.removeLast();
|
||||
window.setParent(null);
|
||||
window.onClose(this);
|
||||
|
||||
if (windows.isEmpty()) {
|
||||
input.removeKeyEventHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.bartlomiejpluta.base.engine.gui.render;
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||
@@ -418,11 +417,6 @@ public class NanoVGGUI implements GUI {
|
||||
nvgResetScissor(nvg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
screenWidget.handleKeyEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
log.info("Disposing GUI color buffers");
|
||||
|
||||
@@ -234,7 +234,7 @@ public class ScreenWidget implements Widget {
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
root.handleKeyEvent(event);
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,6 @@ public class GLFWInput implements Input {
|
||||
this.windowHandle = screen.getID();
|
||||
}
|
||||
|
||||
@SuppressWarnings("ForLoopReplaceableByForEach")
|
||||
public GLFWInput init() {
|
||||
log.info("Registering key callback");
|
||||
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
|
||||
@@ -34,7 +33,8 @@ public class GLFWInput implements Input {
|
||||
}
|
||||
|
||||
// Use iterator to support removal from loop inside
|
||||
for (var iterator = keyEventHandlers.iterator(); iterator.hasNext(); ) {
|
||||
// We need also to iterate in FILO order (a stack-based model)
|
||||
for (var iterator = keyEventHandlers.descendingIterator(); iterator.hasNext(); ) {
|
||||
if (event.isConsumed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user