Give priority of keybinds with special keys

Stop searching if found match.
This commit is contained in:
somehussar committed 2025-12-19 23:08:37 +01:00
1 parent 00eefb0887
commit 3568fb8a86
2 files changed
+33 -2

No files matched your search

@@ -8,7 +8,7 @@ import org.lwjgl.input.Mouse;
import java.util.function.Consumer;
public class KeyPreset {
public class KeyPreset implements Comparable<KeyPreset> {
/**
* @field SINGLE_PRESS => on releasing before 5 ticks press time
* @field PRESS_RELEASE => on pressing and releasing
@@ -148,6 +148,26 @@ public class KeyPreset {
return false;
}
@Override
public int compareTo(KeyPreset o) {
int mySpecialCount = getSpecialKeyCount(this);
int otherSpecialCount = getSpecialKeyCount(o);
return mySpecialCount - otherSpecialCount;
}
private static int getSpecialKeyCount(KeyPreset preset) {
int res = 0;
if (preset.hasAlt())
res += 1;
if (preset.hasCtrl())
res += 1;
if (preset.hasShift())
res += 1;
return res;
}
public static class KeyState {
public int keyCode = -1;
@@ -13,6 +13,8 @@ public class KeyPresetManager {
public List<KeyPreset> keys = new ArrayList<>();
public String fileName;
private boolean wasSorted = false;
public KeyPresetManager(String fileName) {
this.fileName = fileName;
}
@@ -20,12 +22,21 @@ public class KeyPresetManager {
public KeyPreset add(String name) {
KeyPreset preset = new KeyPreset(name);
keys.add(preset);
wasSorted = false;
return preset;
}
public void tick() {
for (KeyPreset key : keys)
if (!wasSorted) {
keys.sort(KeyPreset::compareTo);
wasSorted = true;
}
for (KeyPreset key : keys) {
key.tick();
if (key.isDown)
return;
}
}