Added support for text input

This commit is contained in:
Tefek 2020-09-03 13:35:35 +02:00
parent 2d741f5742
commit 27c79917c5
2 changed files with 44 additions and 0 deletions

View File

@ -2,14 +2,18 @@ package cz.tefek.pluto.engine.input;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import javax.annotation.concurrent.ThreadSafe;
import cz.tefek.pluto.engine.display.Display; import cz.tefek.pluto.engine.display.Display;
@ThreadSafe
public class InputBus public class InputBus
{ {
private static ThreadLocal<KeyboardInputCallback> keyboard = new ThreadLocal<>(); private static ThreadLocal<KeyboardInputCallback> keyboard = new ThreadLocal<>();
private static ThreadLocal<MouseButtonCallback> mouseButton = new ThreadLocal<>(); private static ThreadLocal<MouseButtonCallback> mouseButton = new ThreadLocal<>();
private static ThreadLocal<CursorPositionCallback> cursorPosition = new ThreadLocal<>(); private static ThreadLocal<CursorPositionCallback> cursorPosition = new ThreadLocal<>();
private static ThreadLocal<ScrollInputCallback> scroll = new ThreadLocal<>(); private static ThreadLocal<ScrollInputCallback> scroll = new ThreadLocal<>();
private static ThreadLocal<KeyboardCharInput> charInput = new ThreadLocal<>();
public static void init(Display display) public static void init(Display display)
{ {
@ -25,18 +29,27 @@ public class InputBus
scroll.set(new ScrollInputCallback()); scroll.set(new ScrollInputCallback());
GLFW.glfwSetScrollCallback(display.getWindowPointer(), scroll.get()); GLFW.glfwSetScrollCallback(display.getWindowPointer(), scroll.get());
charInput.set(new KeyboardCharInput());
GLFW.glfwSetCharCallback(display.getWindowPointer(), charInput.get());
} }
public static void destroy() public static void destroy()
{ {
scroll.get().free(); scroll.get().free();
scroll.remove(); scroll.remove();
mouseButton.get().free(); mouseButton.get().free();
mouseButton.remove(); mouseButton.remove();
keyboard.get().free(); keyboard.get().free();
keyboard.remove(); keyboard.remove();
cursorPosition.get().free(); cursorPosition.get().free();
cursorPosition.remove(); cursorPosition.remove();
charInput.get().free();
charInput.remove();
} }
public static KeyboardInputCallback keyboard() public static KeyboardInputCallback keyboard()
@ -65,6 +78,7 @@ public class InputBus
mouseButton.get().reset(); mouseButton.get().reset();
scroll.get().reset(); scroll.get().reset();
cursorPosition.get().reset(); cursorPosition.get().reset();
charInput.get().reset();
} }
public static class Mouse public static class Mouse
@ -134,5 +148,10 @@ public class InputBus
{ {
return keyboard.get().isKeyDown(key); return keyboard.get().isKeyDown(key);
} }
public static String getTypedText()
{
return charInput.get().getTypedText();
}
} }
} }

View File

@ -0,0 +1,25 @@
package cz.tefek.pluto.engine.input;
import org.lwjgl.glfw.GLFWCharCallback;
public class KeyboardCharInput extends GLFWCharCallback
{
private StringBuilder typedText = new StringBuilder();
@Override
public void invoke(long window, int codepoint)
{
this.typedText.appendCodePoint(codepoint);
}
public String getTypedText()
{
return this.typedText.toString();
}
public void reset()
{
this.typedText.setLength(0);
}
}