Add support for key event handling in GUI
This commit is contained in:
@@ -6,7 +6,6 @@ import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.map.handler.MapHandler;
|
||||
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
@@ -137,11 +136,6 @@ public class DefaultContext implements Context {
|
||||
return engine.togglePaused();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void input(Input input) {
|
||||
gameRunner.input(input);
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.LineCap;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.Widget;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.WindingDirection;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
@@ -245,6 +246,11 @@ public class NanoVGGUI implements GUI {
|
||||
nvgScissor(context, x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
screenWidget.handleKeyEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
fillColor.free();
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.engine.gui.widget;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.Widget;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -231,6 +232,11 @@ public class ScreenWidget implements Widget {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
root.handleKeyEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
if (root != null) {
|
||||
|
||||
@@ -33,13 +33,10 @@ public class DefaultGameLogic implements GameLogic {
|
||||
camera = new DefaultCamera();
|
||||
|
||||
log.info("Initializing input model");
|
||||
var input = new GLFWInput(screen);
|
||||
input.init(context);
|
||||
input = new GLFWInput(screen).init();
|
||||
|
||||
log.info("Initializing game context");
|
||||
context.init(screen, input, camera);
|
||||
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.bartlomiejpluta.base.engine.ui.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEventHandler;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.ui.event.GLFWKeyEvent;
|
||||
import lombok.NonNull;
|
||||
@@ -11,7 +10,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static com.bartlomiejpluta.base.engine.ui.event.GLFWKeyEvent.glfwCode;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
@@ -19,27 +17,27 @@ import static org.lwjgl.glfw.GLFW.*;
|
||||
@Slf4j
|
||||
public class GLFWInput implements Input {
|
||||
private final long windowHandle;
|
||||
private final Deque<Consumer<KeyEvent>> keyEventHandlers = new LinkedList<>();
|
||||
private final Deque<KeyEventHandler> keyEventHandlers = new LinkedList<>();
|
||||
|
||||
public GLFWInput(@NonNull Screen screen) {
|
||||
this.windowHandle = screen.getID();
|
||||
}
|
||||
|
||||
public void init(Context context) {
|
||||
public GLFWInput init() {
|
||||
log.info("Registering key callback");
|
||||
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
|
||||
var event = GLFWKeyEvent.of(key, action);
|
||||
|
||||
context.handleKeyEvent(event);
|
||||
|
||||
for (var handler : keyEventHandlers) {
|
||||
if (event.isConsumed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
handler.accept(event);
|
||||
handler.handleKeyEvent(event);
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,12 +46,12 @@ public class GLFWInput implements Input {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addKeyEventHandler(Consumer<KeyEvent> handler) {
|
||||
public void addKeyEventHandler(KeyEventHandler handler) {
|
||||
keyEventHandlers.addLast(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeKeyEventHandler(Consumer<KeyEvent> handler) {
|
||||
public void removeKeyEventHandler(KeyEventHandler handler) {
|
||||
keyEventHandlers.remove(handler);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user