From daf9122bcd4724fc5c932ac9b5692b1eac01b40f Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 21 Jun 2022 05:51:15 -0500 Subject: [PATCH] Implement controller config loading and saving Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- bin/controls.config | Bin 0 -> 338 bytes src/sig/RabiClone.java | 13 +++ src/sig/engine/Key.java | 4 + src/sig/engine/KeyBind.java | 17 ++-- src/sig/objects/ConfigureControls.java | 128 ++++++++++++++++++++++++- 5 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 bin/controls.config diff --git a/bin/controls.config b/bin/controls.config new file mode 100644 index 0000000000000000000000000000000000000000..a441761c104901ce63a28daf834fc391c72b2777 GIT binary patch literal 338 zcmZ{fTMmLS6hzM!+<`wn7T^X@DprdSMT!2f1z-Euxy8o#L7H6P&Y9^zNyQmQ#{o~l zh6dii%tmfw0Y55;T_xgJes4?$yl-Lg{!TD?$4DT z(GmJ~u~|?zC&kNCsb$pf=(?bci_}8TGC6Pj(Vdc=oSm|~-rC;(7@`^_)+y^bL@mGV P3-zh=^LzD+y> DEFAULT_KEYBINDS = new HashMap<>(); + public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); public static void main(String[] args) { @@ -95,6 +99,7 @@ public class RabiClone { ChooseBestRatio(); Map.LoadMap(Maps.WORLD1); + setupDefaultControls(); p = new Panel(f); @@ -169,6 +174,14 @@ public class RabiClone { } } + public static void setupDefaultControls() { + for (Action a : Action.values()) { + List bindList = new ArrayList(); + bindList.addAll(KeyBind.KEYBINDS.get(a)); + DEFAULT_KEYBINDS.put(a,bindList); + } + } + private static boolean detectCollision(AnimatedObject e, AnimatedObject f) { double x1=e.getX()-e.getAnimatedSpr().getWidth()/2,y1=e.getY()-e.getAnimatedSpr().getHeight()/2,x2=e.getX()+e.getAnimatedSpr().getWidth()/2,y2=e.getY()+e.getAnimatedSpr().getHeight()/2; double x3=f.getX()-f.getAnimatedSpr().getWidth()/2,y3=f.getY()-f.getAnimatedSpr().getHeight()/2,x4=f.getX()+f.getAnimatedSpr().getWidth()/2,y4=f.getY()+f.getAnimatedSpr().getHeight()/2; diff --git a/src/sig/engine/Key.java b/src/sig/engine/Key.java index fc31577..bd60b6d 100644 --- a/src/sig/engine/Key.java +++ b/src/sig/engine/Key.java @@ -20,6 +20,10 @@ public class Key extends Identifier{ static HashMap KEYS = new HashMap<>(); int keycode; + public int getKeyCode() { + return keycode; + } + public static void InitializeKeyConversionMap() { KEY_CONVERSION_MAP.put(KeyEvent.VK_0,Identifier.Key._0); KEY_CONVERSION_MAP.put(KeyEvent.VK_1,Identifier.Key._1); diff --git a/src/sig/engine/KeyBind.java b/src/sig/engine/KeyBind.java index 8d46877..8c936ac 100644 --- a/src/sig/engine/KeyBind.java +++ b/src/sig/engine/KeyBind.java @@ -15,13 +15,9 @@ public class KeyBind { public Identifier id; float val; - public KeyBind(byte port, Identifier id) { - this.port=port; - this.id=id; - } - public KeyBind(int keycode) { - this((byte)-1,new Key(keycode)); + this.port=(byte)-1; + this.id=new Key(keycode); } public KeyBind(byte port, Identifier id, float val) { @@ -30,12 +26,16 @@ public class KeyBind { this.val=val; } + public float getVal() { + return val; + } + public boolean isKeyHeld() { if (id instanceof Key) { return ((Key)id).isKeyHeld(); } else if (RabiClone.CONTROLLERS.length>port && id instanceof Identifier.Button) { return RabiClone.CONTROLLERS[port].getComponent(id).getPollData()>0.0f; - } else + } else /* POVs are a special case, so even though they are axes, we want to check them separately. */ if (RabiClone.CONTROLLERS.length>port && RabiClone.CONTROLLERS[port].getComponent(id).getIdentifier()==Identifier.Axis.POV) { if (val==POV.DOWN) { return RabiClone.CONTROLLERS[port].getComponent(id).getPollData()==POV.DOWN|| @@ -65,8 +65,7 @@ public class KeyBind { return Math.abs(RabiClone.CONTROLLERS[port].getComponent(id).getPollData())>=RabiClone.CONTROLLERS[port].getComponent(id).getDeadZone()&&Math.signum(RabiClone.CONTROLLERS[port].getComponent(id).getPollData())==Math.signum(val); } else { - //System.out.println("Could not find proper recognition for component "+id.getName()); - return false; + throw new UnsupportedOperationException("Could not find proper recognition for component "+id.getName()); } } diff --git a/src/sig/objects/ConfigureControls.java b/src/sig/objects/ConfigureControls.java index c45d999..7e8e521 100644 --- a/src/sig/objects/ConfigureControls.java +++ b/src/sig/objects/ConfigureControls.java @@ -1,10 +1,19 @@ package sig.objects; import java.awt.event.MouseEvent; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import net.java.games.input.Component; +import net.java.games.input.Controller; +import net.java.games.input.ControllerEnvironment; import net.java.games.input.Event; import net.java.games.input.Component.Identifier; import net.java.games.input.Component.POV; @@ -12,6 +21,7 @@ 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.PaletteColor; import sig.engine.Panel; @@ -22,6 +32,8 @@ import sig.map.Map; public class ConfigureControls extends Object{ + final static File GAME_CONTROLS_FILE = new File("controls.config"); + Action selectedAction = Action.MOVE_RIGHT; KeyBind selectedKeybind = null; boolean assigningKey = false; @@ -33,9 +45,115 @@ public class ConfigureControls extends Object{ public ConfigureControls(Panel panel) { super(panel); RabiClone.BACKGROUND_COLOR = PaletteColor.WHITE; + if (GAME_CONTROLS_FILE.exists()) { + LoadControls(); + } updateHighlightSections(); } + public static void LoadControls() { + try { + DataInputStream stream = new DataInputStream(new FileInputStream(GAME_CONTROLS_FILE)); + Controller[] CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().rescanControllers(); + KeyBind.KEYBINDS.clear(); + while (stream.available()>0) { + Action a = Action.valueOf(readString(stream)); + byte port = stream.readByte(); + do { + if (port==(byte)-1) { + int keycode = stream.readInt(); + KeyBind kb = new KeyBind(keycode); + appendToKeybind(a,kb); + } else { + java.lang.String controllerName = readString(stream); + Controller controller=null; + for (int i = 0; i < CONTROLLERS.length; i++) { + if (CONTROLLERS[i].getType() == Controller.Type.KEYBOARD + || CONTROLLERS[i].getType() == Controller.Type.MOUSE) { + continue; + } else + if (CONTROLLERS[i].getName().equals(controllerName)) { + controller=CONTROLLERS[i]; + } + } + if (controller==null) { + //Discard these bits of data as we didn't find a controller. + readString(stream); + stream.readFloat(); + continue; + } else { + java.lang.String componentName = readString(stream); + Component c=null; + for (Component cc : controller.getComponents()) { + if (cc.getName().equals(componentName)) { + c=cc; + break; + } + } + float val = stream.readFloat(); + if (c!=null) { + KeyBind kb = new KeyBind(port,c.getIdentifier(),val); + appendToKeybind(a,kb); + } else { + System.out.println("Could not load component "+componentName+" for keybind "+a); + } + } + } + port = stream.readByte(); + } while (port!='\0'); + } + } catch (IOException e) { + e.printStackTrace(); + RabiClone.setupDefaultControls(); + } + } + + private static void appendToKeybind(Action a,KeyBind kb) { + List binds = KeyBind.KEYBINDS.getOrDefault(a,new ArrayList()); + binds.add(kb); + KeyBind.KEYBINDS.put(a,binds); + } + + private static java.lang.String readString(DataInputStream stream) throws IOException { + StringBuilder sb = new StringBuilder(); + while (true) { + Character c = stream.readChar(); + if (c=='\0') { + return sb.toString(); + } else { + sb.append(c); + } + } + } + + public static void SaveControls() { + try { + DataOutputStream stream = new DataOutputStream(new FileOutputStream(GAME_CONTROLS_FILE)); + for (Action a : Action.values()) { + writeString(a.name(),stream); + for (KeyBind k : KeyBind.KEYBINDS.get(a)) { + stream.writeByte(k.port); + if (k.port==(byte)-1) { + stream.writeInt(((Key)k.id).getKeyCode()); + } else { + writeString(RabiClone.CONTROLLERS[k.port].getName(),stream); + writeString(RabiClone.CONTROLLERS[k.port].getComponent(k.id).getName(),stream); + stream.writeFloat(k.getVal()); + } + } + stream.writeByte('\0'); + } + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void writeString(java.lang.String s, DataOutputStream stream) throws IOException { + stream.writeChars(s); + stream.writeChar('\0'); + } + private void updateHighlightSections() { for (int i=0;i