Implement keyboard keys as a component

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 6e900581ea
commit a6e6b92f7b
  1. 108
      src/sig/RabiClone.java
  2. 51
      src/sig/engine/Key.java
  3. 2
      src/sig/engine/Object.java
  4. 8
      src/sig/engine/Panel.java

@ -9,6 +9,7 @@ import net.java.games.input.ControllerEvent;
import net.java.games.input.ControllerListener; import net.java.games.input.ControllerListener;
import net.java.games.input.Event; import net.java.games.input.Event;
import net.java.games.input.EventQueue; import net.java.games.input.EventQueue;
import net.java.games.input.Component.Identifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -21,6 +22,7 @@ import sig.objects.EditorRenderer;
import sig.objects.Erinoah; import sig.objects.Erinoah;
import sig.objects.LevelRenderer; import sig.objects.LevelRenderer;
import sig.objects.Player; import sig.objects.Player;
import sig.engine.Key;
import sig.engine.Object; import sig.engine.Object;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -46,6 +48,9 @@ public class RabiClone{
public static Maps CURRENT_MAP = Maps.WORLD1; public static Maps CURRENT_MAP = Maps.WORLD1;
public static Controller[] CONTROLLERS = new Controller[]{}; public static Controller[] CONTROLLERS = new Controller[]{};
public static void main(String[] args) { public static void main(String[] args) {
InitializeKeyConversionMap();
f = new JFrame(PROGRAM_NAME); f = new JFrame(PROGRAM_NAME);
f.setResizable(false); f.setResizable(false);
f.setUndecorated(true); f.setUndecorated(true);
@ -76,7 +81,7 @@ public class RabiClone{
lastGameTime=System.nanoTime(); lastGameTime=System.nanoTime();
double updateMult = Math.min(1/60d,timePassed/1000000000d); double updateMult = Math.min(1/60d,timePassed/1000000000d);
CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers() CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers();
for (int i=0;i<CONTROLLERS.length;i++) { for (int i=0;i<CONTROLLERS.length;i++) {
if (CONTROLLERS[i].poll()) { if (CONTROLLERS[i].poll()) {
Component[] components = CONTROLLERS[i].getComponents(); Component[] components = CONTROLLERS[i].getComponents();
@ -118,6 +123,107 @@ 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() { private static void ResetGame() {
player=null; player=null;
} }

@ -0,0 +1,51 @@
package sig.engine;
import java.util.HashMap;
import net.java.games.input.Component;
import sig.RabiClone;
public class Key implements Component{
public static HashMap<Integer,Identifier.Key> KEY_CONVERSION_MAP = new HashMap<>();
int keycode;
Key(int keycode) {
this.keycode=keycode;
}
public boolean isKeyHeld() {
return RabiClone.p.KEYS.get(getIdentifier());
}
@Override
public Identifier getIdentifier() {
return KEY_CONVERSION_MAP.get(keycode);
}
@Override
public boolean isRelative() {
return false;
}
@Override
public boolean isAnalog() {
return false;
}
@Override
public float getDeadZone() {
return 0;
}
@Override
public float getPollData() {
return isKeyHeld()?1.0f:0.0f;
}
@Override
public String getName() {
return "Java System Keyboard";
}
}

@ -90,7 +90,7 @@ public abstract class Object implements GameEntity{
} }
protected boolean KeyHeld(int key) { protected boolean KeyHeld(int key) {
return panel.KEYS.getOrDefault(key,false); return panel.KEYS.getOrDefault(Key.KEY_CONVERSION_MAP.get(key),false);
} }
protected void KeyPressed(int key) { protected void KeyPressed(int key) {

@ -18,6 +18,8 @@ import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.event.MouseInputListener; import javax.swing.event.MouseInputListener;
import net.java.games.input.Component;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import sig.DrawLoop; import sig.DrawLoop;
@ -45,7 +47,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
public double nanaY = 0; public double nanaY = 0;
public Point mousePos=new Point(0,0); public Point mousePos=new Point(0,0);
public int button = 0; public int button = 0;
public HashMap<Integer,Boolean> KEYS = new HashMap<>(); public HashMap<Component.Identifier.Key,Boolean> KEYS = new HashMap<>();
public HashMap<Integer,Boolean> MOUSE = new HashMap<>(); public HashMap<Integer,Boolean> MOUSE = new HashMap<>();
public static byte[] generalPalette = new byte[]{ public static byte[] generalPalette = new byte[]{
(byte)0x5b,(byte)0xa6,(byte)0x75, (byte)0x5b,(byte)0xa6,(byte)0x75,
@ -413,7 +415,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (!KEYS.getOrDefault(e.getKeyCode(),false)) { if (!KEYS.getOrDefault(e.getKeyCode(),false)) {
KEYS.put(e.getKeyCode(),true); KEYS.put(Key.KEY_CONVERSION_MAP.get(e.getKeyCode()),true);
for (int i=0;i<RabiClone.OBJ.size();i++) { for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyPressed(e.getKeyCode()); RabiClone.OBJ.get(i).KeyPressed(e.getKeyCode());
} }
@ -423,7 +425,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
KEYS.put(e.getKeyCode(),false); KEYS.put(Key.KEY_CONVERSION_MAP.get(e.getKeyCode()),false);
for (int i=0;i<RabiClone.OBJ.size();i++) { for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyReleased(e.getKeyCode()); RabiClone.OBJ.get(i).KeyReleased(e.getKeyCode());
} }

Loading…
Cancel
Save