Ignore non-existing GLFW key and action input codes instead throwing an exception
This commit is contained in:
@@ -3,7 +3,6 @@ package com.bartlomiejpluta.base.engine.ui.event;
|
|||||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -25,8 +24,15 @@ public class GLFWKeyEvent implements KeyEvent {
|
|||||||
consumed = true;
|
consumed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KeyEvent of(int key, int action) {
|
public static KeyEvent of(int keyCode, int actionCode) {
|
||||||
return new GLFWKeyEvent(parseKey(key), parseAction(action));
|
var key = parseKey(keyCode);
|
||||||
|
var action = parseAction(actionCode);
|
||||||
|
|
||||||
|
if (key == null && action == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GLFWKeyEvent(key, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int glfwCode(Key key) {
|
public static int glfwCode(Key key) {
|
||||||
@@ -276,7 +282,7 @@ public class GLFWKeyEvent implements KeyEvent {
|
|||||||
case GLFW_KEY_RIGHT_CONTROL -> Key.KEY_RIGHT_CONTROL;
|
case GLFW_KEY_RIGHT_CONTROL -> Key.KEY_RIGHT_CONTROL;
|
||||||
case GLFW_KEY_RIGHT_ALT -> Key.KEY_RIGHT_ALT;
|
case GLFW_KEY_RIGHT_ALT -> Key.KEY_RIGHT_ALT;
|
||||||
case GLFW_KEY_RIGHT_SUPER -> Key.KEY_RIGHT_SUPER;
|
case GLFW_KEY_RIGHT_SUPER -> Key.KEY_RIGHT_SUPER;
|
||||||
default -> throw new AppException("Unknown key code: %d", code);
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +291,7 @@ public class GLFWKeyEvent implements KeyEvent {
|
|||||||
case GLFW_PRESS -> KeyAction.PRESS;
|
case GLFW_PRESS -> KeyAction.PRESS;
|
||||||
case GLFW_RELEASE -> KeyAction.RELEASE;
|
case GLFW_RELEASE -> KeyAction.RELEASE;
|
||||||
case GLFW_REPEAT -> KeyAction.REPEAT;
|
case GLFW_REPEAT -> KeyAction.REPEAT;
|
||||||
default -> throw new AppException("Unknown key code: %d", code);
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ public class GLFWInput implements Input {
|
|||||||
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
|
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
|
||||||
var event = GLFWKeyEvent.of(key, action);
|
var event = GLFWKeyEvent.of(key, action);
|
||||||
|
|
||||||
|
if (event == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Use iterator to support removal from loop inside
|
// Use iterator to support removal from loop inside
|
||||||
for (var iterator = keyEventHandlers.iterator(); iterator.hasNext(); ) {
|
for (var iterator = keyEventHandlers.iterator(); iterator.hasNext(); ) {
|
||||||
if (event.isConsumed()) {
|
if (event.isConsumed()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user