mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
* First pass reformatting code to be more readable (common still not done) * Replace google common collect calls with Java calls where possible * Remove a bunch of unnecessary parenthesis leaving clarifying ones * Add some missing @Nonnull annotations * Fix checking wrong type * Remove unnecessary continue/return statements and simplify comparision check * Pass exception instead of directly printing stack trace * Work on cleaning up common, through config so far * Work on cleaning up common, through integration so far * Work on cleaning up common, through network so far * Work on cleaning up common, through tier so far * Remove empty constructors that get created by default anyways * Work on cleaning up common, through transmitters so far * Work on cleaning up common, through utils so far * Finish cleaning up spacing for common
98 lines
2.6 KiB
Java
98 lines
2.6 KiB
Java
package mekanism.client;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.BitSet;
|
|
import java.util.List;
|
|
import net.minecraft.client.settings.KeyBinding;
|
|
import org.lwjgl.input.Keyboard;
|
|
import org.lwjgl.input.Mouse;
|
|
|
|
public abstract class MekKeyHandler {
|
|
|
|
/**
|
|
* KeyBinding instances
|
|
*/
|
|
private KeyBinding[] keyBindings;
|
|
|
|
/**
|
|
* Track which keys have been seen as pressed currently
|
|
*/
|
|
private BitSet keyDown;
|
|
|
|
/**
|
|
* Whether keys send repeated KeyDown pseudo-messages
|
|
*/
|
|
private BitSet repeatings;
|
|
|
|
/**
|
|
* Pass an array of keybindings and a repeat flag for each one
|
|
*
|
|
* @param bindings Bindings to set
|
|
*/
|
|
public MekKeyHandler(Builder bindings) {
|
|
keyBindings = bindings.getBindings();
|
|
repeatings = bindings.getRepeatFlags();
|
|
keyDown = new BitSet();
|
|
}
|
|
|
|
public static boolean getIsKeyPressed(KeyBinding keyBinding) {
|
|
try {
|
|
int keyCode = keyBinding.getKeyCode();
|
|
return keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode);
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public KeyBinding[] getKeyBindings() {
|
|
return keyBindings;
|
|
}
|
|
|
|
public void keyTick() {
|
|
for (int i = 0; i < keyBindings.length; i++) {
|
|
KeyBinding keyBinding = keyBindings[i];
|
|
boolean state = keyBinding.isPressed();
|
|
boolean lastState = keyDown.get(i);
|
|
|
|
if (state != lastState || (state && repeatings.get(i))) {
|
|
if (state) {
|
|
keyDown(keyBinding, state == lastState);
|
|
} else {
|
|
keyUp(keyBinding);
|
|
}
|
|
|
|
keyDown.set(i, state);
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract void keyDown(KeyBinding kb, boolean isRepeat);
|
|
|
|
public abstract void keyUp(KeyBinding kb);
|
|
|
|
protected static class Builder {
|
|
|
|
private List<KeyBinding> bindings = new ArrayList<>(4);
|
|
private BitSet repeatFlags = new BitSet();
|
|
|
|
/**
|
|
* Add a keybinding to the list
|
|
*
|
|
* @param k the KeyBinding to add
|
|
* @param repeatFlag true if keyDown pseudo-events continue to be sent while key is held
|
|
*/
|
|
protected Builder addBinding(KeyBinding k, boolean repeatFlag) {
|
|
repeatFlags.set(bindings.size(), repeatFlag);
|
|
bindings.add(k);
|
|
return this;
|
|
}
|
|
|
|
protected BitSet getRepeatFlags() {
|
|
return repeatFlags;
|
|
}
|
|
|
|
protected KeyBinding[] getBindings() {
|
|
return bindings.toArray(new KeyBinding[0]);
|
|
}
|
|
}
|
|
} |