feat(gui): update StageInputHandler
package nullengine.client.gui.internal.impl;
-import nullengine.client.gui.Scene;
import nullengine.client.gui.Stage;
import nullengine.client.rendering.display.Window;
import nullengine.client.rendering.display.callback.*;
@@ -19,43 +18,27 @@ public final class StageInputHandler {
public StageInputHandler(Stage stage, Window window) {
this.stage = stage;
this.window = window;
+ initCallbacks();
}
- public void enable() {
- if (cursorCallback == null) cursorCallback = (window, xpos, ypos) -> {
- Scene scene = stage.getScene();
- if (scene != null) {
- scene.processCursor(xpos, ypos);
+ private void initCallbacks() {
+ cursorCallback = (window, xpos, ypos) -> stage.scene().ifPresent(scene ->
+ scene.processCursor(xpos, ypos));
+ mouseCallback = (window, button, action, mods) -> stage.scene().ifPresent(scene ->
+ scene.processMouse(button, mods, action == Action.PRESS));
+ keyCallback = (window, key, scancode, action, mods) -> stage.scene().ifPresent(scene ->
+ scene.processKey(key, mods, action != Action.RELEASE));
+ scrollCallback = (window, xoffset, yoffset) -> stage.scene().ifPresent(scene ->
+ scene.processScroll(xoffset, yoffset));
+ charModsCallback = (window, character, mods) -> stage.scene().ifPresent(scene ->
+ scene.processCharMods(character, mods));
}
- };
+
+ public void enable() {
window.addCursorCallback(cursorCallback);
- if (mouseCallback == null) mouseCallback = (window, button, action, mods) -> {
- Scene scene = stage.getScene();
- if (scene != null) {
- scene.processMouse(button, mods, action == Action.PRESS);
- }
- };
window.addMouseCallback(mouseCallback);
- if (keyCallback == null) keyCallback = (window, key, scancode, action, mods) -> {
- Scene scene = stage.getScene();
- if (scene != null) {
- scene.processKey(key, mods, action != Action.RELEASE);
- }
- };
window.addKeyCallback(keyCallback);
- if (scrollCallback == null) scrollCallback = (window, xoffset, yoffset) -> {
- Scene scene = stage.getScene();
- if (scene != null) {
- scene.processScroll(xoffset, yoffset);
- }
- };
window.addCursorCallback(cursorCallback);
- if (charModsCallback == null) charModsCallback = (window, character, mods) -> {
- Scene scene = stage.getScene();
- if (scene != null) {
- scene.processCharMods(character, mods);
- }
- };
window.addCharModsCallback(charModsCallback);
}
