Implement controller config loading and saving
Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
511c9e4daf
commit
daf9122bcd
BIN
bin/controls.config
Normal file
BIN
bin/controls.config
Normal file
Binary file not shown.
@ -7,6 +7,7 @@ import net.java.games.input.ControllerEnvironment;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import sig.engine.Panel;
|
import sig.engine.Panel;
|
||||||
@ -21,6 +22,7 @@ import sig.objects.ConfigureControls;
|
|||||||
import sig.objects.LevelRenderer;
|
import sig.objects.LevelRenderer;
|
||||||
import sig.objects.Player;
|
import sig.objects.Player;
|
||||||
import sig.objects.actor.RenderedObject;
|
import sig.objects.actor.RenderedObject;
|
||||||
|
import sig.engine.Action;
|
||||||
import sig.engine.Key;
|
import sig.engine.Key;
|
||||||
import sig.engine.KeyBind;
|
import sig.engine.KeyBind;
|
||||||
import sig.engine.PaletteColor;
|
import sig.engine.PaletteColor;
|
||||||
@ -73,6 +75,8 @@ public class RabiClone {
|
|||||||
public static long TIME = 0;
|
public static long TIME = 0;
|
||||||
public static long scaleTime;
|
public static long scaleTime;
|
||||||
|
|
||||||
|
public static HashMap<Action,List<KeyBind>> DEFAULT_KEYBINDS = new HashMap<>();
|
||||||
|
|
||||||
public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
|
public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -95,6 +99,7 @@ public class RabiClone {
|
|||||||
ChooseBestRatio();
|
ChooseBestRatio();
|
||||||
|
|
||||||
Map.LoadMap(Maps.WORLD1);
|
Map.LoadMap(Maps.WORLD1);
|
||||||
|
setupDefaultControls();
|
||||||
|
|
||||||
p = new Panel(f);
|
p = new Panel(f);
|
||||||
|
|
||||||
@ -169,6 +174,14 @@ public class RabiClone {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setupDefaultControls() {
|
||||||
|
for (Action a : Action.values()) {
|
||||||
|
List<KeyBind> bindList = new ArrayList<KeyBind>();
|
||||||
|
bindList.addAll(KeyBind.KEYBINDS.get(a));
|
||||||
|
DEFAULT_KEYBINDS.put(a,bindList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean detectCollision(AnimatedObject e, AnimatedObject f) {
|
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 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;
|
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;
|
||||||
|
@ -20,6 +20,10 @@ public class Key extends Identifier{
|
|||||||
static HashMap<Identifier.Key,Boolean> KEYS = new HashMap<>();
|
static HashMap<Identifier.Key,Boolean> KEYS = new HashMap<>();
|
||||||
int keycode;
|
int keycode;
|
||||||
|
|
||||||
|
public int getKeyCode() {
|
||||||
|
return keycode;
|
||||||
|
}
|
||||||
|
|
||||||
public static void InitializeKeyConversionMap() {
|
public static void InitializeKeyConversionMap() {
|
||||||
KEY_CONVERSION_MAP.put(KeyEvent.VK_0,Identifier.Key._0);
|
KEY_CONVERSION_MAP.put(KeyEvent.VK_0,Identifier.Key._0);
|
||||||
KEY_CONVERSION_MAP.put(KeyEvent.VK_1,Identifier.Key._1);
|
KEY_CONVERSION_MAP.put(KeyEvent.VK_1,Identifier.Key._1);
|
||||||
|
@ -15,13 +15,9 @@ public class KeyBind {
|
|||||||
public Identifier id;
|
public Identifier id;
|
||||||
float val;
|
float val;
|
||||||
|
|
||||||
public KeyBind(byte port, Identifier id) {
|
|
||||||
this.port=port;
|
|
||||||
this.id=id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public KeyBind(int keycode) {
|
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) {
|
public KeyBind(byte port, Identifier id, float val) {
|
||||||
@ -30,12 +26,16 @@ public class KeyBind {
|
|||||||
this.val=val;
|
this.val=val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getVal() {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isKeyHeld() {
|
public boolean isKeyHeld() {
|
||||||
if (id instanceof Key) {
|
if (id instanceof Key) {
|
||||||
return ((Key)id).isKeyHeld();
|
return ((Key)id).isKeyHeld();
|
||||||
} else if (RabiClone.CONTROLLERS.length>port && id instanceof Identifier.Button) {
|
} else if (RabiClone.CONTROLLERS.length>port && id instanceof Identifier.Button) {
|
||||||
return RabiClone.CONTROLLERS[port].getComponent(id).getPollData()>0.0f;
|
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 (RabiClone.CONTROLLERS.length>port && RabiClone.CONTROLLERS[port].getComponent(id).getIdentifier()==Identifier.Axis.POV) {
|
||||||
if (val==POV.DOWN) {
|
if (val==POV.DOWN) {
|
||||||
return RabiClone.CONTROLLERS[port].getComponent(id).getPollData()==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);
|
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 {
|
else {
|
||||||
//System.out.println("Could not find proper recognition for component "+id.getName());
|
throw new UnsupportedOperationException("Could not find proper recognition for component "+id.getName());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,19 @@
|
|||||||
package sig.objects;
|
package sig.objects;
|
||||||
|
|
||||||
import java.awt.event.MouseEvent;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.java.games.input.Component;
|
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.Event;
|
||||||
import net.java.games.input.Component.Identifier;
|
import net.java.games.input.Component.Identifier;
|
||||||
import net.java.games.input.Component.POV;
|
import net.java.games.input.Component.POV;
|
||||||
@ -12,6 +21,7 @@ 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.PaletteColor;
|
import sig.engine.PaletteColor;
|
||||||
import sig.engine.Panel;
|
import sig.engine.Panel;
|
||||||
@ -22,6 +32,8 @@ import sig.map.Map;
|
|||||||
|
|
||||||
public class ConfigureControls extends Object{
|
public class ConfigureControls extends Object{
|
||||||
|
|
||||||
|
final static File GAME_CONTROLS_FILE = new File("controls.config");
|
||||||
|
|
||||||
Action selectedAction = Action.MOVE_RIGHT;
|
Action selectedAction = Action.MOVE_RIGHT;
|
||||||
KeyBind selectedKeybind = null;
|
KeyBind selectedKeybind = null;
|
||||||
boolean assigningKey = false;
|
boolean assigningKey = false;
|
||||||
@ -33,9 +45,115 @@ public class ConfigureControls extends Object{
|
|||||||
public ConfigureControls(Panel panel) {
|
public ConfigureControls(Panel panel) {
|
||||||
super(panel);
|
super(panel);
|
||||||
RabiClone.BACKGROUND_COLOR = PaletteColor.WHITE;
|
RabiClone.BACKGROUND_COLOR = PaletteColor.WHITE;
|
||||||
|
if (GAME_CONTROLS_FILE.exists()) {
|
||||||
|
LoadControls();
|
||||||
|
}
|
||||||
updateHighlightSections();
|
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<KeyBind> binds = KeyBind.KEYBINDS.getOrDefault(a,new ArrayList<KeyBind>());
|
||||||
|
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() {
|
private void updateHighlightSections() {
|
||||||
for (int i=0;i<Action.values().length;i++) {
|
for (int i=0;i<Action.values().length;i++) {
|
||||||
Action a = Action.values()[i];
|
Action a = Action.values()[i];
|
||||||
@ -80,7 +198,7 @@ public class ConfigureControls extends Object{
|
|||||||
updateHighlightSections();
|
updateHighlightSections();
|
||||||
assigningKey=false;
|
assigningKey=false;
|
||||||
}
|
}
|
||||||
//System.out.println(e.getComponent().getName()+" value: "+e.getValue());
|
System.out.println(e.getComponent().getName()+" value: "+e.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +270,7 @@ public class ConfigureControls extends Object{
|
|||||||
public void KeyPressed(Action a) {
|
public void KeyPressed(Action a) {
|
||||||
switch(a) {
|
switch(a) {
|
||||||
case PLAY_GAME:{
|
case PLAY_GAME:{
|
||||||
|
SaveControls();
|
||||||
RabiClone.OBJ.clear();
|
RabiClone.OBJ.clear();
|
||||||
RabiClone.ResetGame();
|
RabiClone.ResetGame();
|
||||||
Map.LoadMap(RabiClone.CURRENT_MAP);
|
Map.LoadMap(RabiClone.CURRENT_MAP);
|
||||||
@ -159,6 +278,7 @@ public class ConfigureControls extends Object{
|
|||||||
RabiClone.StartGame();
|
RabiClone.StartGame();
|
||||||
}break;
|
}break;
|
||||||
case LEVEL_EDITOR:{
|
case LEVEL_EDITOR:{
|
||||||
|
SaveControls();
|
||||||
RabiClone.OBJ.clear();
|
RabiClone.OBJ.clear();
|
||||||
RabiClone.ResetGame();
|
RabiClone.ResetGame();
|
||||||
Map.LoadMap(RabiClone.CURRENT_MAP);
|
Map.LoadMap(RabiClone.CURRENT_MAP);
|
||||||
@ -185,3 +305,9 @@ public class ConfigureControls extends Object{
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ControlType{
|
||||||
|
BUTTON,
|
||||||
|
POV,
|
||||||
|
AXIS
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user