Allow KeyBind class to wrap around components, dictating what the value needs to be for detecting

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 06c8c8d909
commit 65e6ebc245
  1. 28
      src/sig/engine/Action.java
  2. 16
      src/sig/engine/KeyBind.java
  3. 9
      src/sig/objects/ConfigureControls.java

@ -7,26 +7,26 @@ 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)),
EDITOR_SET_VIEW(new Key(KeyEvent.VK_F3)),
EDITOR_SET_TYPE(new Key(KeyEvent.VK_F4)),
EDITOR_SET_BACKGROUND(new Key(KeyEvent.VK_F5)),;
MOVE_RIGHT(new KeyBind(KeyEvent.VK_RIGHT),new KeyBind(KeyEvent.VK_D)),
MOVE_LEFT(new KeyBind(KeyEvent.VK_LEFT),new KeyBind(KeyEvent.VK_A)),
JUMP(new KeyBind(KeyEvent.VK_SPACE),new KeyBind(KeyEvent.VK_W)),
FALL(new KeyBind(KeyEvent.VK_DOWN),new KeyBind(KeyEvent.VK_S)),
SLIDE(new KeyBind(KeyEvent.VK_CONTROL)),
LEVEL_EDITOR(new KeyBind(KeyEvent.VK_F2)),
PLAY_GAME(new KeyBind(KeyEvent.VK_F1)),
EDITOR_SET_VIEW(new KeyBind(KeyEvent.VK_F3)),
EDITOR_SET_TYPE(new KeyBind(KeyEvent.VK_F4)),
EDITOR_SET_BACKGROUND(new KeyBind(KeyEvent.VK_F5)),;
float val;
Key controllingKey;
Action(Component...components) {
KeyBind.KEYBINDS.put(this,new ArrayList<>(Arrays.asList(components)));
Action(KeyBind...keybinds) {
KeyBind.KEYBINDS.put(this,new ArrayList<>(Arrays.asList(keybinds)));
}
Action(Component axis,float val) {
ArrayList<Component> comps = new ArrayList<Component>();
comps.add(axis);
ArrayList<KeyBind> comps = new ArrayList<KeyBind>();
comps.add(new KeyBind(axis,val));
KeyBind.KEYBINDS.put(this,comps);
}
}

@ -9,16 +9,20 @@ import net.java.games.input.Component.POV;
import sig.RabiClone;
public class KeyBind {
public static HashMap<Action,List<Component>> KEYBINDS = new HashMap<>();
public static HashMap<Action,List<KeyBind>> KEYBINDS = new HashMap<>();
static HashMap<Action,Boolean> KEYS = new HashMap<>();
Component c;
public Component c;
float val;
public KeyBind(Component c) {
this.c=c;
}
public KeyBind(int keycode) {
this(new Key(keycode));
}
public KeyBind(Component c, float val) {
this.c=c;
this.val=val;
@ -28,7 +32,7 @@ public class KeyBind {
return KEYS.getOrDefault(action,false);
}
public static boolean isKeyHeld(Component c) {
public boolean isKeyHeld() {
if (c instanceof Key) {
return ((Key)c).isKeyHeld();
} else if (c instanceof Identifier.Button) {
@ -54,9 +58,9 @@ public class KeyBind {
//Polls all KeyBinds based on device.
for (Action a : Action.values()) {
boolean held = false;
Component cc = null;
for (Component c : KEYBINDS.get(a)) {
held = isKeyHeld(c);
KeyBind cc = null;
for (KeyBind c : KEYBINDS.get(a)) {
held = c.isKeyHeld();
actionEventCheck(a,held);
if (held) {
cc=c;

@ -1,11 +1,12 @@
package sig.objects;
import java.util.HashMap;
import net.java.games.input.Component;
import sig.RabiClone;
import sig.engine.Action;
import sig.engine.Alpha;
import sig.engine.Font;
import sig.engine.Key;
import sig.engine.KeyBind;
import sig.engine.Object;
import sig.engine.PaletteColor;
@ -13,6 +14,8 @@ import sig.engine.Panel;
public class ConfigureControls extends Object{
HashMap<Component,Float> defaultValues = new HashMap<>();
protected ConfigureControls(Panel panel) {
super(panel);
RabiClone.BACKGROUND_COLOR = PaletteColor.WHITE;
@ -34,8 +37,8 @@ public class ConfigureControls extends Object{
private StringBuilder DisplayActionKeys(Action a) {
StringBuilder sb = new StringBuilder(a.toString()).append(": ");
boolean first=true;
for (Component c : KeyBind.KEYBINDS.get(a)) {
sb.append(((Key)c).isKeyHeld()?PaletteColor.YELLOW_GREEN:PaletteColor.MIDNIGHT_BLUE).append(c.getName()).append(!first?",":"");
for (KeyBind c : KeyBind.KEYBINDS.get(a)) {
sb.append(c.isKeyHeld()?PaletteColor.YELLOW_GREEN:PaletteColor.MIDNIGHT_BLUE).append(c.c.getName()).append(!first?",":"");
sb.append("\n");
}
return sb;

Loading…
Cancel
Save