From 55276867cc903d67cf37e156027cb9192ea0d49a Mon Sep 17 00:00:00 2001 From: Nic0Nic0Nii Date: Mon, 6 Jun 2022 20:16:29 +0000 Subject: [PATCH] Setup and prepare keybind system Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/RabiClone.java | 108 ++---------------------------------- src/sig/engine/Action.java | 28 ++++++++++ src/sig/engine/Key.java | 104 +++++++++++++++++++++++++++++++++- src/sig/engine/KeyBind.java | 81 +++++++++++++++++++++++++++ 4 files changed, 218 insertions(+), 103 deletions(-) create mode 100644 src/sig/engine/Action.java create mode 100644 src/sig/engine/KeyBind.java diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index a9921a9..88de32a 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -18,6 +18,7 @@ import sig.objects.Erinoah; import sig.objects.LevelRenderer; import sig.objects.Player; import sig.engine.Key; +import sig.engine.KeyBind; import sig.engine.Object; import java.awt.Toolkit; import java.awt.event.KeyEvent; @@ -44,7 +45,9 @@ public class RabiClone{ public static Controller[] CONTROLLERS = new Controller[]{}; public static void main(String[] args) { - InitializeKeyConversionMap(); + Key.InitializeKeyConversionMap(); + + f = new JFrame(PROGRAM_NAME); f.setResizable(false); @@ -93,6 +96,8 @@ public class RabiClone{ }*/ } + KeyBind.poll(); + if (Key.isKeyHeld(KeyEvent.VK_F1)) { if (level_renderer instanceof EditorRenderer) { OBJ.remove(level_renderer); @@ -117,107 +122,6 @@ public class RabiClone{ } } - private static void InitializeKeyConversionMap() { - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_0,Identifier.Key._0); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_1,Identifier.Key._1); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_2,Identifier.Key._2); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_3,Identifier.Key._3); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_4,Identifier.Key._4); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_5,Identifier.Key._5); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_6,Identifier.Key._6); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_7,Identifier.Key._7); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_8,Identifier.Key._8); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_9,Identifier.Key._9); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_A,Identifier.Key.A); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_B,Identifier.Key.B); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_C,Identifier.Key.C); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_D,Identifier.Key.D); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_E,Identifier.Key.E); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F,Identifier.Key.F); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_G,Identifier.Key.G); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_H,Identifier.Key.H); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_I,Identifier.Key.I); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_J,Identifier.Key.J); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_K,Identifier.Key.K); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_L,Identifier.Key.L); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_M,Identifier.Key.M); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_N,Identifier.Key.N); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_O,Identifier.Key.O); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_P,Identifier.Key.P); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Q,Identifier.Key.Q); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_R,Identifier.Key.R); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_S,Identifier.Key.S); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_T,Identifier.Key.T); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_U,Identifier.Key.U); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_V,Identifier.Key.V); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_W,Identifier.Key.W); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_X,Identifier.Key.X); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Y,Identifier.Key.Y); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Z,Identifier.Key.Z); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_MINUS,Identifier.Key.MINUS); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_EQUALS,Identifier.Key.EQUALS); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_QUOTE,Identifier.Key.GRAVE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_OPEN_BRACKET,Identifier.Key.LBRACKET); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CLOSE_BRACKET,Identifier.Key.RBRACKET); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_SLASH,Identifier.Key.BACKSLASH); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_SPACE,Identifier.Key.BACK); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_TAB,Identifier.Key.TAB); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CAPS_LOCK,Identifier.Key.CAPITAL); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SEMICOLON,Identifier.Key.SEMICOLON); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ENTER,Identifier.Key.RETURN); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SHIFT,Identifier.Key.LSHIFT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_COMMA,Identifier.Key.COMMA); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PERIOD,Identifier.Key.PERIOD); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SLASH,Identifier.Key.SLASH); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CONTROL,Identifier.Key.LCONTROL); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ALT,Identifier.Key.LALT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SPACE,Identifier.Key.SPACE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_WINDOWS,Identifier.Key.SYSRQ); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CONTEXT_MENU,Identifier.Key.APPS); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_HOME,Identifier.Key.HOME); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_INSERT,Identifier.Key.INSERT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DELETE,Identifier.Key.DELETE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_END,Identifier.Key.END); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAGE_UP,Identifier.Key.PAGEUP); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAGE_DOWN,Identifier.Key.PAGEDOWN); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ESCAPE,Identifier.Key.ESCAPE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F1,Identifier.Key.F1); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F2,Identifier.Key.F2); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F3,Identifier.Key.F3); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F4,Identifier.Key.F4); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F5,Identifier.Key.F5); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F6,Identifier.Key.F6); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F7,Identifier.Key.F7); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F8,Identifier.Key.F8); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F9,Identifier.Key.F9); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F10,Identifier.Key.F10); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F11,Identifier.Key.F11); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F12,Identifier.Key.F12); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PRINTSCREEN,Identifier.Key.KANA); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SCROLL_LOCK,Identifier.Key.SCROLL); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAUSE,Identifier.Key.PAUSE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_LEFT,Identifier.Key.LEFT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_RIGHT,Identifier.Key.RIGHT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_UP,Identifier.Key.UP); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DOWN,Identifier.Key.DOWN); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUM_LOCK,Identifier.Key.NUMLOCK); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ASTERISK,Identifier.Key.AX); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DIVIDE,Identifier.Key.DIVIDE); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SUBTRACT,Identifier.Key.SUBTRACT); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ADD,Identifier.Key.ADD); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DECIMAL,Identifier.Key.DECIMAL); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD0,Identifier.Key.NUMPAD0); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD1,Identifier.Key.NUMPAD1); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD2,Identifier.Key.NUMPAD2); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD3,Identifier.Key.NUMPAD3); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD4,Identifier.Key.NUMPAD4); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD5,Identifier.Key.NUMPAD5); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD6,Identifier.Key.NUMPAD6); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD7,Identifier.Key.NUMPAD7); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD8,Identifier.Key.NUMPAD8); - Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD9,Identifier.Key.NUMPAD9); - } - private static void ResetGame() { player=null; } diff --git a/src/sig/engine/Action.java b/src/sig/engine/Action.java new file mode 100644 index 0000000..5da89d0 --- /dev/null +++ b/src/sig/engine/Action.java @@ -0,0 +1,28 @@ +package sig.engine; + +import java.awt.event.KeyEvent; +import java.util.ArrayList; +import java.util.Arrays; + +import net.java.games.input.Component; + +public enum Action { + MOVE_RIGHT(new Key(KeyEvent.VK_RIGHT),new Key(KeyEvent.VK_D)), + MOVE_LEFT(new Key(KeyEvent.VK_LEFT),new Key(KeyEvent.VK_A)), + JUMP(new Key(KeyEvent.VK_SPACE),new Key(KeyEvent.VK_W)), + FALL(new Key(KeyEvent.VK_DOWN),new Key(KeyEvent.VK_S)), + SLIDE(new Key(KeyEvent.VK_CONTROL)), + LEVEL_EDITOR(new Key(KeyEvent.VK_F2)), + PLAY_GAME(new Key(KeyEvent.VK_F1)),; + + float val; + + Action(Component...components) { + KeyBind.KEYBINDS.put(this,new ArrayList<>(Arrays.asList(components))); + } + Action(Component axis,float val) { + ArrayList comps = new ArrayList(); + comps.add(axis); + KeyBind.KEYBINDS.put(this,comps); + } +} diff --git a/src/sig/engine/Key.java b/src/sig/engine/Key.java index a691cc7..a30e195 100644 --- a/src/sig/engine/Key.java +++ b/src/sig/engine/Key.java @@ -2,6 +2,7 @@ package sig.engine; import java.util.HashMap; +import java.awt.event.KeyEvent; import net.java.games.input.Component; public class Key implements Component{ @@ -14,6 +15,107 @@ public class Key implements Component{ this.keycode=keycode; } + public static void InitializeKeyConversionMap() { + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_0,Identifier.Key._0); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_1,Identifier.Key._1); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_2,Identifier.Key._2); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_3,Identifier.Key._3); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_4,Identifier.Key._4); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_5,Identifier.Key._5); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_6,Identifier.Key._6); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_7,Identifier.Key._7); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_8,Identifier.Key._8); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_9,Identifier.Key._9); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_A,Identifier.Key.A); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_B,Identifier.Key.B); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_C,Identifier.Key.C); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_D,Identifier.Key.D); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_E,Identifier.Key.E); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F,Identifier.Key.F); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_G,Identifier.Key.G); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_H,Identifier.Key.H); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_I,Identifier.Key.I); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_J,Identifier.Key.J); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_K,Identifier.Key.K); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_L,Identifier.Key.L); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_M,Identifier.Key.M); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_N,Identifier.Key.N); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_O,Identifier.Key.O); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_P,Identifier.Key.P); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Q,Identifier.Key.Q); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_R,Identifier.Key.R); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_S,Identifier.Key.S); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_T,Identifier.Key.T); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_U,Identifier.Key.U); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_V,Identifier.Key.V); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_W,Identifier.Key.W); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_X,Identifier.Key.X); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Y,Identifier.Key.Y); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_Z,Identifier.Key.Z); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_MINUS,Identifier.Key.MINUS); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_EQUALS,Identifier.Key.EQUALS); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_QUOTE,Identifier.Key.GRAVE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_OPEN_BRACKET,Identifier.Key.LBRACKET); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CLOSE_BRACKET,Identifier.Key.RBRACKET); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_SLASH,Identifier.Key.BACKSLASH); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_BACK_SPACE,Identifier.Key.BACK); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_TAB,Identifier.Key.TAB); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CAPS_LOCK,Identifier.Key.CAPITAL); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SEMICOLON,Identifier.Key.SEMICOLON); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ENTER,Identifier.Key.RETURN); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SHIFT,Identifier.Key.LSHIFT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_COMMA,Identifier.Key.COMMA); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PERIOD,Identifier.Key.PERIOD); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SLASH,Identifier.Key.SLASH); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CONTROL,Identifier.Key.LCONTROL); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ALT,Identifier.Key.LALT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SPACE,Identifier.Key.SPACE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_WINDOWS,Identifier.Key.SYSRQ); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_CONTEXT_MENU,Identifier.Key.APPS); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_HOME,Identifier.Key.HOME); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_INSERT,Identifier.Key.INSERT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DELETE,Identifier.Key.DELETE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_END,Identifier.Key.END); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAGE_UP,Identifier.Key.PAGEUP); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAGE_DOWN,Identifier.Key.PAGEDOWN); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ESCAPE,Identifier.Key.ESCAPE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F1,Identifier.Key.F1); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F2,Identifier.Key.F2); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F3,Identifier.Key.F3); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F4,Identifier.Key.F4); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F5,Identifier.Key.F5); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F6,Identifier.Key.F6); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F7,Identifier.Key.F7); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F8,Identifier.Key.F8); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F9,Identifier.Key.F9); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F10,Identifier.Key.F10); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F11,Identifier.Key.F11); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_F12,Identifier.Key.F12); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PRINTSCREEN,Identifier.Key.KANA); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SCROLL_LOCK,Identifier.Key.SCROLL); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_PAUSE,Identifier.Key.PAUSE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_LEFT,Identifier.Key.LEFT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_RIGHT,Identifier.Key.RIGHT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_UP,Identifier.Key.UP); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DOWN,Identifier.Key.DOWN); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUM_LOCK,Identifier.Key.NUMLOCK); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ASTERISK,Identifier.Key.AX); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DIVIDE,Identifier.Key.DIVIDE); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_SUBTRACT,Identifier.Key.SUBTRACT); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_ADD,Identifier.Key.ADD); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_DECIMAL,Identifier.Key.DECIMAL); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD0,Identifier.Key.NUMPAD0); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD1,Identifier.Key.NUMPAD1); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD2,Identifier.Key.NUMPAD2); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD3,Identifier.Key.NUMPAD3); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD4,Identifier.Key.NUMPAD4); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD5,Identifier.Key.NUMPAD5); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD6,Identifier.Key.NUMPAD6); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD7,Identifier.Key.NUMPAD7); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD8,Identifier.Key.NUMPAD8); + Key.KEY_CONVERSION_MAP.put(KeyEvent.VK_NUMPAD9,Identifier.Key.NUMPAD9); + } + public static void setKeyHeld(int keycode,boolean pressed) { KEYS.put(KEY_CONVERSION_MAP.get(keycode),pressed); } @@ -53,7 +155,7 @@ public class Key implements Component{ @Override public String getName() { - return "Java System Keyboard"; + return "Java System Keyboard - "+Key.KEY_CONVERSION_MAP.get(keycode).getName(); } } diff --git a/src/sig/engine/KeyBind.java b/src/sig/engine/KeyBind.java new file mode 100644 index 0000000..e720c22 --- /dev/null +++ b/src/sig/engine/KeyBind.java @@ -0,0 +1,81 @@ +package sig.engine; + +import java.util.HashMap; +import java.util.List; + +import net.java.games.input.Component; +import net.java.games.input.Component.Identifier; + +public class KeyBind { + public static HashMap> KEYBINDS = new HashMap<>(); + static HashMap KEYS = new HashMap<>(); + + Component c; + float val; + + public KeyBind(Component c) { + this.c=c; + } + + public KeyBind(Component c, float val) { + this.c=c; + this.val=val; + } + + public static boolean IsKeyHeld(Action action) { + return KEYS.getOrDefault(action,false); + } + + public static void setKeyPressed(Action action, boolean state) { + KEYS.put(action,state); + /*if (c instanceof Key) { + return ((Key)c).isKeyHeld(); + } else + if (c instanceof Identifier.Button) { + return c.getPollData()>0.0f; + } else + if (c.getIdentifier()==Identifier.Axis.POV) { + return val==c.getPollData(); + } else + if (c instanceof Identifier.Axis) { + return c.getPollData()>=c.getDeadZone()&&Math.signum(c.getPollData())==Math.signum(val); + }*/ + } + + public static void poll() { + //Polls all KeyBinds based on device. + for (Action a : Action.values()) { + for (Component c : KEYBINDS.get(a)) { + if (c instanceof Key) { + actionEventCheck(a,((Key)c).isKeyHeld()); + } else + if (c instanceof Identifier.Button) { + actionEventCheck(a,c.getPollData()>0.0f); + } else + if (c.getIdentifier()==Identifier.Axis.POV) { + actionEventCheck(a,a.val==c.getPollData()); + } else + if (c.getIdentifier() instanceof Identifier.Axis) { + actionEventCheck(a,c.getPollData()>=c.getDeadZone()&&Math.signum(c.getPollData())==Math.signum(a.val)); + } + } + } + } + + private static void actionEventCheck(Action a, boolean held) { + if (KeyBind.IsKeyHeld(a)&&!held) { + emitReleaseEvent(a); + } else + if (!KeyBind.IsKeyHeld(a)&&held) { + emitPressEvent(a); + } + KeyBind.setKeyPressed(a, held); + } + + private static void emitReleaseEvent(Action a) { + System.out.println("Release for "+a); + } + private static void emitPressEvent(Action a) { + System.out.println("Press for "+a); + } +} \ No newline at end of file