mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Keybindings optimisations (#5389)
* Shift keybinds to a builder pattern to avoid the array length issue, use Bitsets for the boolean arrays, remove unused and redundant constructor * Use proper lang keys for KeyBinding creation instead of pre-localising * WIP tidy keyhandler tick method * more key handler tidying * tweak handler to use translation placeholders so languages can reconfigure the foo: bar display * add todo * move tank, bow & flamethrower keys to use string format placeholders * fix NPE on configurator changes * nullable annotations * prefix keybind lang keys with modid * make datafixers into their own thing * make a datafixer base class * remove keybind fixer, options datafixers dont work * Revert "remove keybind fixer, options datafixers dont work" This reverts commit c57f6c1b05ed6b26e7fb7774bada4ca53f6c655e. * datafix the keybinds to the new format * documentation * Remove unused import and extra dash from runClient args
This commit is contained in:
1 parent
b570ef7110
commit
31b06c0ab0
39 files changed
+572
-248
No files matched your search
@@ -1,37 +1,38 @@
|
||||
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 {
|
||||
|
||||
public KeyBinding[] keyBindings;
|
||||
public boolean[] keyDown;
|
||||
public boolean[] repeatings;
|
||||
public boolean isDummy;
|
||||
/**
|
||||
* 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
|
||||
* @param rep Repeat flags for bindings
|
||||
*/
|
||||
public MekKeyHandler(KeyBinding[] bindings, boolean[] rep) {
|
||||
keyBindings = bindings;
|
||||
repeatings = rep;
|
||||
keyDown = new boolean[keyBindings.length];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the keys into the system. You will do your own keyboard management elsewhere. No events will fire if you
|
||||
* use this method
|
||||
*
|
||||
* @param bindings Bindings to set
|
||||
*/
|
||||
public MekKeyHandler(KeyBinding[] bindings) {
|
||||
keyBindings = bindings;
|
||||
isDummy = true;
|
||||
public MekKeyHandler(Builder bindings) {
|
||||
keyBindings = bindings.getBindings();
|
||||
repeatings = bindings.getRepeatFlags();
|
||||
keyDown = new BitSet();
|
||||
}
|
||||
|
||||
public static boolean getIsKeyPressed(KeyBinding keyBinding) {
|
||||
@@ -51,15 +52,16 @@ public abstract class MekKeyHandler {
|
||||
for (int i = 0; i < keyBindings.length; i++) {
|
||||
KeyBinding keyBinding = keyBindings[i];
|
||||
boolean state = keyBinding.isPressed();
|
||||
boolean lastState = keyDown.get(i);
|
||||
|
||||
if (state != keyDown[i] || (state && repeatings[i])) {
|
||||
if (state != lastState || (state && repeatings.get(i))) {
|
||||
if (state) {
|
||||
keyDown(keyBinding, state == keyDown[i]);
|
||||
keyDown(keyBinding, state == lastState);
|
||||
} else {
|
||||
keyUp(keyBinding);
|
||||
}
|
||||
|
||||
keyDown[i] = state;
|
||||
keyDown.set(i, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,4 +69,29 @@ public abstract class MekKeyHandler {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user