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

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

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

Loading…
Cancel
Save