Fix emitting events for key presses.

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
sigonasr2 2022-06-06 23:55:37 -05:00
parent 627c788d05
commit 5b6796d50d
3 changed files with 23 additions and 7 deletions

View File

@ -16,6 +16,7 @@ public enum Action {
PLAY_GAME(new Key(KeyEvent.VK_F1)),;
float val;
Key controllingKey;
Action(Component...components) {
KeyBind.KEYBINDS.put(this,new ArrayList<>(Arrays.asList(components)));

View File

@ -118,6 +118,7 @@ public class Key implements Component{
public static void setKeyHeld(int keycode,boolean pressed) {
KEYS.put(KEY_CONVERSION_MAP.get(keycode),pressed);
//System.out.println(KEYS);
}
public static boolean isKeyHeld(int keycode) {
@ -129,7 +130,7 @@ public class Key implements Component{
}
@Override
public Identifier getIdentifier() {
public Identifier.Key getIdentifier() {
return KEY_CONVERSION_MAP.get(keycode);
}

View File

@ -45,19 +45,33 @@ public class KeyBind {
public static void poll() {
//Polls all KeyBinds based on device.
for (Action a : Action.values()) {
boolean held = false;
Component cc = null;
for (Component c : KEYBINDS.get(a)) {
if (c instanceof Key) {
actionEventCheck(a,((Key)c).isKeyHeld());
held = ((Key)c).isKeyHeld();
actionEventCheck(a,held);
} else
if (c instanceof Identifier.Button) {
actionEventCheck(a,c.getPollData()>0.0f);
held = c.getPollData()>0.0f;
actionEventCheck(a,held);
} else
if (c.getIdentifier()==Identifier.Axis.POV) {
actionEventCheck(a,a.val==c.getPollData());
held = a.val==c.getPollData();
actionEventCheck(a,held);
} else
if (c.getIdentifier() instanceof Identifier.Axis) {
actionEventCheck(a,c.getPollData()>=c.getDeadZone()&&Math.signum(c.getPollData())==Math.signum(a.val));
held = c.getPollData()>=c.getDeadZone()&&Math.signum(c.getPollData())==Math.signum(a.val);
actionEventCheck(a,held);
}
if (held) {
cc=c;
break;
}
}
if (held) {
KEYBINDS.get(a).remove(cc);
KEYBINDS.get(a).add(0,cc);
}
}
}
@ -73,9 +87,9 @@ public class KeyBind {
}
private static void emitReleaseEvent(Action a) {
System.out.println("Release for "+a);
//System.out.println("Release for "+a);
}
private static void emitPressEvent(Action a) {
System.out.println("Press for "+a);
//System.out.println("Press for "+a);
}
}