From 5b6796d50d84bc6f5831db1adcb75eabdeaaf8e7 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 6 Jun 2022 23:55:37 -0500 Subject: [PATCH] Fix emitting events for key presses. Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/engine/Action.java | 1 + src/sig/engine/Key.java | 3 ++- src/sig/engine/KeyBind.java | 26 ++++++++++++++++++++------ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/sig/engine/Action.java b/src/sig/engine/Action.java index 5da89d0..a019a94 100644 --- a/src/sig/engine/Action.java +++ b/src/sig/engine/Action.java @@ -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))); diff --git a/src/sig/engine/Key.java b/src/sig/engine/Key.java index a30e195..9bb5507 100644 --- a/src/sig/engine/Key.java +++ b/src/sig/engine/Key.java @@ -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); } diff --git a/src/sig/engine/KeyBind.java b/src/sig/engine/KeyBind.java index e720c22..996b4ce 100644 --- a/src/sig/engine/KeyBind.java +++ b/src/sig/engine/KeyBind.java @@ -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); } } \ No newline at end of file