mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-25 14:34:42 +00:00
Improve Keyboard, add window icons
This commit is contained in:
1 parent
699a36d1e9
commit
dce65ecdb5
4 files changed
+104
-39
No files matched your search
@@ -43,6 +43,10 @@ class EventQueue {
|
||||
return true;
|
||||
}
|
||||
|
||||
int getEventCount() {
|
||||
return eventCount;
|
||||
}
|
||||
|
||||
int getMaxEvents() {
|
||||
return maxEvents;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjglx.LWJGLException;
|
||||
import org.lwjglx.Sys;
|
||||
@@ -156,26 +157,37 @@ public class Keyboard {
|
||||
public static final int KEY_POWER = 0xDE;
|
||||
public static final int KEY_SLEEP = 0xDF;
|
||||
|
||||
private static EventQueue queue = new EventQueue(32);
|
||||
// private static int maxEvents = 32;
|
||||
public static final int keyCount;
|
||||
|
||||
// private static int eventCount = 0;
|
||||
// private static int currentEventPos = -1;
|
||||
// private static int nextEventPos = 0;
|
||||
private static EventQueue queue = new EventQueue(128);
|
||||
|
||||
private enum KeyState {
|
||||
PRESS(true),
|
||||
RELEASE(false),
|
||||
REPEAT(true);
|
||||
public final boolean isPressed;
|
||||
|
||||
KeyState(boolean isPressed) {
|
||||
this.isPressed = isPressed;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean doRepeatEvents = true;
|
||||
|
||||
private static int[] keyEvents = new int[queue.getMaxEvents()];
|
||||
private static boolean[] keyEventStates = new boolean[queue.getMaxEvents()];
|
||||
private static KeyState[] keyEventStates = new KeyState[queue.getMaxEvents()];
|
||||
private static long[] nanoTimeEvents = new long[queue.getMaxEvents()];
|
||||
private static char[] keyEventChars = new char[256];
|
||||
|
||||
public static final int KEYBOARD_SIZE = 256;
|
||||
|
||||
private static final String[] keyName = new String[KEYBOARD_SIZE];
|
||||
private static final Map<String, Integer> keyMap = new HashMap<String, Integer>(253);
|
||||
private static final Map<String, Integer> keyMap = new HashMap<>(253);
|
||||
|
||||
static {
|
||||
// Use reflection to find out key names
|
||||
Field[] fields = Keyboard.class.getFields();
|
||||
int keyCounter = 0;
|
||||
try {
|
||||
for (Field field : fields) {
|
||||
if (Modifier.isStatic(field.getModifiers())
|
||||
@@ -190,27 +202,46 @@ public class Keyboard {
|
||||
String name = field.getName().substring(4);
|
||||
keyName[key] = name;
|
||||
keyMap.put(name, key);
|
||||
keyCounter++;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
keyCount = keyCounter;
|
||||
}
|
||||
|
||||
public static void addKeyEvent(int key, boolean pressed) {
|
||||
// eventCount++;
|
||||
// if (eventCount > maxEvents) eventCount = maxEvents;
|
||||
|
||||
public static void addGlfwKeyEvent(long window, int key, int scancode, int action, int mods) {
|
||||
final KeyState state;
|
||||
switch (action) {
|
||||
case GLFW.GLFW_PRESS:
|
||||
state = KeyState.PRESS;
|
||||
break;
|
||||
case GLFW.GLFW_RELEASE:
|
||||
state = KeyState.RELEASE;
|
||||
break;
|
||||
case GLFW.GLFW_REPEAT:
|
||||
state = KeyState.REPEAT;
|
||||
if (!doRepeatEvents) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
state = KeyState.RELEASE;
|
||||
break;
|
||||
}
|
||||
keyEvents[queue.getNextPos()] = KeyCodes.toLwjglKey(key);
|
||||
keyEventStates[queue.getNextPos()] = pressed;
|
||||
|
||||
keyEventStates[queue.getNextPos()] = state;
|
||||
nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime();
|
||||
|
||||
queue.add();
|
||||
/*nextEventPos++;
|
||||
if (nextEventPos == maxEvents) nextEventPos = 0;
|
||||
}
|
||||
|
||||
if (currentEventPos == nextEventPos) currentEventPos++;
|
||||
if (currentEventPos == maxEvents) currentEventPos = 0;*/
|
||||
public static void addKeyEvent(int key, boolean pressed) {
|
||||
keyEvents[queue.getNextPos()] = KeyCodes.toLwjglKey(key);
|
||||
keyEventStates[queue.getNextPos()] = pressed ? KeyState.PRESS : KeyState.RELEASE;
|
||||
nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime();
|
||||
|
||||
queue.add();
|
||||
}
|
||||
|
||||
public static void addCharEvent(int key, char c) {
|
||||
@@ -225,28 +256,31 @@ public class Keyboard {
|
||||
}
|
||||
|
||||
public static void poll() {
|
||||
// TODO
|
||||
GLFW.glfwPollEvents();
|
||||
}
|
||||
|
||||
public static void enableRepeatEvents(boolean enable) {
|
||||
// TODO
|
||||
System.out.println("TODO: Implement Keyboad.enableRepeatEvents(boolean)");
|
||||
doRepeatEvents = enable;
|
||||
}
|
||||
|
||||
public static boolean areRepeatEventsEnabled() {
|
||||
return doRepeatEvents;
|
||||
}
|
||||
|
||||
public static int getKeyCount() {
|
||||
return keyCount;
|
||||
}
|
||||
|
||||
public static int getNumKeyboardEvents() {
|
||||
return queue.getEventCount();
|
||||
}
|
||||
|
||||
public static boolean isRepeatEvent() {
|
||||
// TODO
|
||||
return false;
|
||||
return keyEventStates[queue.getCurrentPos()] == KeyState.REPEAT;
|
||||
}
|
||||
|
||||
public static boolean next() {
|
||||
return queue.next();
|
||||
/*if (eventCount == 0) return false;
|
||||
|
||||
eventCount--;
|
||||
currentEventPos++;
|
||||
if (currentEventPos == maxEvents) currentEventPos = 0;
|
||||
|
||||
return true;*/
|
||||
}
|
||||
|
||||
public static int getEventKey() {
|
||||
@@ -258,7 +292,7 @@ public class Keyboard {
|
||||
}
|
||||
|
||||
public static boolean getEventKeyState() {
|
||||
return keyEventStates[queue.getCurrentPos()];
|
||||
return keyEventStates[queue.getCurrentPos()].isPressed;
|
||||
}
|
||||
|
||||
public static long getEventNanoseconds() {
|
||||
@@ -266,13 +300,21 @@ public class Keyboard {
|
||||
}
|
||||
|
||||
public static String getKeyName(int key) {
|
||||
return keyName[key];
|
||||
if (key >= 0 && key < keyName.length) {
|
||||
return keyName[key];
|
||||
} else {
|
||||
return "Key " + key;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getKeyIndex(java.lang.String keyName) {
|
||||
Integer ret = keyMap.get(keyName);
|
||||
if (ret == null) return KEY_NONE;
|
||||
else return ret;
|
||||
if (ret == null) {
|
||||
if (keyName.matches("Key -?[0-9]+]")) {
|
||||
return Integer.parseInt(StringUtils.removeStart(keyName, "Key "));
|
||||
}
|
||||
return KEY_NONE;
|
||||
} else return ret;
|
||||
}
|
||||
|
||||
public static boolean isCreated() {
|
||||
|
||||
@@ -18,7 +18,7 @@ public class Mouse {
|
||||
private static int x = 0;
|
||||
private static int y = 0;
|
||||
|
||||
private static EventQueue queue = new EventQueue(32);
|
||||
private static EventQueue queue = new EventQueue(128);
|
||||
|
||||
private static int[] buttonEvents = new int[queue.getMaxEvents()];
|
||||
private static boolean[] buttonEventStates = new boolean[queue.getMaxEvents()];
|
||||
|
||||
@@ -6,6 +6,7 @@ import static org.lwjgl.opengl.GL11.GL_FALSE;
|
||||
import static org.lwjgl.opengl.GL11.GL_TRUE;
|
||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
@@ -52,6 +53,7 @@ public class Display {
|
||||
private static boolean latestResized = false;
|
||||
private static int latestWidth = 0;
|
||||
private static int latestHeight = 0;
|
||||
private static ByteBuffer[] savedIcons;
|
||||
|
||||
static {
|
||||
Sys.initialize(); // init using dummy sys method
|
||||
@@ -106,9 +108,7 @@ public class Display {
|
||||
public void invoke(long window, int key, int scancode, int action, int mods) {
|
||||
latestEventKey = key;
|
||||
|
||||
if (action == GLFW_RELEASE || action == GLFW.GLFW_PRESS) {
|
||||
Keyboard.addKeyEvent(key, action == GLFW.GLFW_PRESS ? true : false);
|
||||
}
|
||||
Keyboard.addGlfwKeyEvent(window, key, scancode, action, mods);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -228,6 +228,11 @@ public class Display {
|
||||
glfwMakeContextCurrent(Window.handle);
|
||||
GL.createCapabilities();
|
||||
|
||||
if (savedIcons != null) {
|
||||
setIcon(savedIcons);
|
||||
savedIcons = null;
|
||||
}
|
||||
|
||||
glfwSwapInterval(1);
|
||||
glfwShowWindow(Window.handle);
|
||||
|
||||
@@ -378,8 +383,22 @@ public class Display {
|
||||
}
|
||||
|
||||
public static int setIcon(java.nio.ByteBuffer[] icons) {
|
||||
// TODO
|
||||
System.out.println("TODO: Implement Display.setIcon(ByteBuffer[])");
|
||||
if (getWindow() == 0) {
|
||||
savedIcons = icons;
|
||||
return 0;
|
||||
}
|
||||
GLFWImage.Buffer glfwImages = GLFWImage.calloc(icons.length);
|
||||
for (int icon = 0; icon < icons.length; icon++) {
|
||||
ByteBuffer iconBytes = org.lwjgl.BufferUtils.createByteBuffer(icons[icon].capacity());
|
||||
iconBytes.put(icons[icon]);
|
||||
int dimension = (int) Math.sqrt(iconBytes.limit() / 4D);
|
||||
if (dimension * dimension * 4 != iconBytes.limit()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
glfwImages.put(icon, GLFWImage.create().set(dimension, dimension, iconBytes));
|
||||
}
|
||||
GLFW.glfwSetWindowIcon(getWindow(), glfwImages);
|
||||
glfwImages.free();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user