Added input handlers by @Valoeghese
parent
321c7ba1e9
commit
23453b4dbc
|
@ -0,0 +1,21 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public final class InitialPressHandler implements KeyListener
|
||||
{
|
||||
public InitialPressHandler(KeyCallback callback)
|
||||
{ this.callback = callback; }
|
||||
|
||||
private boolean activatedPreviously = false;
|
||||
private final KeyCallback callback;
|
||||
|
||||
@Override
|
||||
public void listen(boolean active)
|
||||
{
|
||||
if (!activatedPreviously && active)
|
||||
{ callback.onCallback(); }
|
||||
activatedPreviously = active;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public class Input
|
||||
{
|
||||
private static final Map<Keybind, List<KeyCallback>> CALLBACKS = new HashMap<>();
|
||||
private static final Map<Keybind, List<KeyListener>> LISTENERS = new HashMap<>();
|
||||
|
||||
public static void addPressCallback(Keybind key, KeyCallback callback)
|
||||
{ CALLBACKS.computeIfAbsent(key, listener -> new ArrayList<>()).add(callback); }
|
||||
|
||||
public static void addListener(Keybind key, KeyListener callback)
|
||||
{ LISTENERS.computeIfAbsent(key, listener -> new ArrayList<>()).add(callback); }
|
||||
|
||||
public static void invokeAllListeners()
|
||||
{
|
||||
CALLBACKS.forEach((keybind, listeners) ->
|
||||
{
|
||||
if (keybind.isActive())
|
||||
{ listeners.forEach(callback -> callback.onCallback()); }
|
||||
});
|
||||
LISTENERS.forEach((keybind, listeners) ->
|
||||
{
|
||||
listeners.forEach(listener -> listener.listen(keybind.isActive()));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public interface KeyCallback
|
||||
{
|
||||
public void onCallback();
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWKeyCallback;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public class KeyCallbackHandler extends GLFWKeyCallback
|
||||
{
|
||||
private KeyCallbackHandler()
|
||||
{}
|
||||
|
||||
private static final KeyCallbackHandler INSTANCE = new KeyCallbackHandler();
|
||||
|
||||
public static void trackWindow(long window)
|
||||
{ GLFW.glfwSetKeyCallback(window, INSTANCE); }
|
||||
|
||||
public static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
|
||||
|
||||
@Override
|
||||
public void invoke(long window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
try
|
||||
{
|
||||
keys[key] = action != GLFW.GLFW_RELEASE;
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException e)
|
||||
{
|
||||
// Probably just changing the volume
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public interface KeyListener
|
||||
{
|
||||
public void listen(boolean active);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public final class Keybind
|
||||
{
|
||||
public int value;
|
||||
public boolean mouse;
|
||||
public static final Keybind MOVE_UP = new Keybind(GLFW.GLFW_KEY_W, false);
|
||||
public static final Keybind MOVE_DOWN = new Keybind(GLFW.GLFW_KEY_S, false);
|
||||
public static final Keybind MOVE_LEFT = new Keybind(GLFW.GLFW_KEY_A, false);
|
||||
public static final Keybind MOVE_RIGHT = new Keybind(GLFW.GLFW_KEY_D, false);
|
||||
public static final Keybind USE = new Keybind(GLFW.GLFW_MOUSE_BUTTON_1, true);
|
||||
public static final Keybind SELECT_0 = new Keybind(GLFW.GLFW_KEY_1, false);
|
||||
public static final Keybind SELECT_1 = new Keybind(GLFW.GLFW_KEY_2, false);
|
||||
public static final Keybind SELECT_2 = new Keybind(GLFW.GLFW_KEY_3, false);
|
||||
public static final Keybind EXIT = new Keybind(GLFW.GLFW_KEY_ESCAPE, false);
|
||||
|
||||
public Keybind(int initValue, boolean isMouse)
|
||||
{
|
||||
this.value = initValue;
|
||||
this.mouse = isMouse;
|
||||
}
|
||||
|
||||
public boolean isActive()
|
||||
{ return mouse ? MouseCallbackHandler.buttons[value] : KeyCallbackHandler.keys[value]; }
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.github.halotroop.litecraft.input;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWMouseButtonCallback;
|
||||
|
||||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public class MouseCallbackHandler extends GLFWMouseButtonCallback
|
||||
{
|
||||
private MouseCallbackHandler()
|
||||
{}
|
||||
|
||||
private static final MouseCallbackHandler INSTANCE = new MouseCallbackHandler();
|
||||
|
||||
public static void trackWindow(long window)
|
||||
{ GLFW.glfwSetMouseButtonCallback(window, INSTANCE); }
|
||||
|
||||
public static boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
||||
|
||||
@Override
|
||||
public void invoke(long window, int button, int action, int mods)
|
||||
{ buttons[button] = action != GLFW.GLFW_RELEASE; }
|
||||
}
|
Loading…
Reference in New Issue