fixed up gamepad mapping a bit. added options to invert and to act as a trigger

This commit is contained in:
Diego Waxemberg
2013-12-20 23:13:18 -05:00
parent 18c7d3c098
commit bef355487d
4 changed files with 178 additions and 73 deletions

View File

@@ -3,6 +3,7 @@ package com.limelight.input;
import java.io.Serializable;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
public enum ControllerComponent implements Serializable {
@@ -15,13 +16,24 @@ public enum ControllerComponent implements Serializable {
private JLabel label;
private JButton mapButton;
private JCheckBox invertBox;
private JCheckBox triggerBox;
private boolean analog;
private boolean invert;
private boolean trigger;
private ControllerComponent(String name, boolean analog) {
this.label = new JLabel(name);
this.mapButton = new JButton();
this.mapButton.setName(this.name());
this.invertBox = new JCheckBox("Invert");
this.invertBox.setName(this.name());
this.triggerBox = new JCheckBox("Trigger");
this.triggerBox.setName(this.name());
this.triggerBox.setToolTipText("If this component should act as a trigger.");
this.analog = analog;
this.invert = false;
this.trigger = false;
}
public JLabel getLabel() {
@@ -32,7 +44,31 @@ public enum ControllerComponent implements Serializable {
return mapButton;
}
public JCheckBox getInvertBox() {
return invertBox;
}
public JCheckBox getTriggerBox() {
return triggerBox;
}
public boolean isAnalog() {
return analog;
}
public boolean isTrigger() {
return trigger;
}
public void trigger(boolean isTrigger) {
trigger = isTrigger;
}
public void invert(boolean invert) {
this.invert = invert;
}
public boolean invert() {
return invert;
}
}

View File

@@ -107,13 +107,24 @@ public class Gamepad {
ControllerComponent contComp = config.getControllerComponent(comp);
if (contComp != null) {
if (contComp.isAnalog()) {
handleAnalog(contComp, value);
handleAnalog(contComp, sanitizeValue(contComp, value));
} else {
handleButtons(contComp, value);
handleButtons(contComp, sanitizeValue(contComp, value));
}
}
}
private float sanitizeValue(ControllerComponent contComp, float value) {
float sanitized = value;
if (contComp.invert()) {
sanitized = -sanitized;
}
if (contComp.isTrigger()) {
sanitized = (sanitized + 1)/2;
}
return sanitized;
}
private void toggle(short button, boolean press) {
if (press) {
inputMap |= button;
@@ -123,8 +134,6 @@ public class Gamepad {
}
private void handleAnalog(ControllerComponent contComp, float value) {
switch (contComp) {
case LS_X:
leftStickX = (short)Math.round(value * 0x7FFF);
@@ -139,10 +148,10 @@ public class Gamepad {
rightStickY = (short)Math.round(value * 0x7FFF);
break;
case LT:
leftTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
leftTrigger = (byte)Math.round(value * 0xFF);
break;
case RT:
rightTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
rightTrigger = (byte)Math.round(value * 0xFF);
break;
default:
System.out.println("A mapping error has occured. Ignoring: " + contComp.name());

View File

@@ -23,6 +23,10 @@ public class GamepadMapping implements Serializable {
return mapping.get(comp.getIdentifier().getName());
}
public void removeMapping(Component comp) {
mapping.remove(comp.getIdentifier().getName());
}
/**
* Gets the mapping for the specified component.</br>
* NOTE: Use sparingly takes O(N) time.