fixed invert and trigger settings not being saved

This commit is contained in:
Diego Waxemberg
2013-12-21 00:07:32 -05:00
parent f3f52f9616
commit 81c8a09385
4 changed files with 83 additions and 51 deletions

View File

@@ -28,6 +28,7 @@ import com.limelight.input.ControllerListener;
import com.limelight.input.Gamepad; import com.limelight.input.Gamepad;
import com.limelight.input.GamepadHandler; import com.limelight.input.GamepadHandler;
import com.limelight.input.GamepadMapping; import com.limelight.input.GamepadMapping;
import com.limelight.input.GamepadMapping.Mapping;
import com.limelight.settings.GamepadSettingsManager; import com.limelight.settings.GamepadSettingsManager;
public class SettingsFrame extends JFrame { public class SettingsFrame extends JFrame {
@@ -62,42 +63,54 @@ public class SettingsFrame extends JFrame {
ControllerComponent[] components = ControllerComponent.values(); ControllerComponent[] components = ControllerComponent.values();
for (int i = 0; i < components.length; i++) { for (int i = 0; i < components.length; i++) {
Mapping mapping = config.getMappedComponent(components[i]);
ControllerComponent comp = null;
if (mapping == null) {
comp = components[i];
} else {
comp = mapping.contComp;
}
Box componentBox = Box.createHorizontalBox(); Box componentBox = Box.createHorizontalBox();
componentBox.add(Box.createHorizontalStrut(10)); componentBox.add(Box.createHorizontalStrut(10));
componentBox.add(components[i].getLabel()); componentBox.add(comp.getLabel());
componentBox.add(Box.createHorizontalGlue()); componentBox.add(Box.createHorizontalGlue());
componentBox.add(components[i].getMapButton()); componentBox.add(comp.getMapButton());
componentBox.add(Box.createHorizontalStrut(5)); componentBox.add(Box.createHorizontalStrut(5));
componentBox.add(components[i].getInvertBox()); componentBox.add(comp.getInvertBox());
componentBox.add(Box.createHorizontalStrut(5)); componentBox.add(Box.createHorizontalStrut(5));
componentBox.add(components[i].getTriggerBox()); componentBox.add(comp.getTriggerBox());
componentBox.add(Box.createHorizontalStrut(10)); componentBox.add(Box.createHorizontalStrut(10));
Dimension buttonSize = new Dimension(110,32); Dimension buttonSize = new Dimension(110,32);
components[i].getMapButton().setMaximumSize(buttonSize); comp.getMapButton().setMaximumSize(buttonSize);
components[i].getMapButton().setMinimumSize(buttonSize); comp.getMapButton().setMinimumSize(buttonSize);
components[i].getMapButton().setPreferredSize(buttonSize); comp.getMapButton().setPreferredSize(buttonSize);
components[i].getMapButton().addActionListener(createListener()); comp.getMapButton().addActionListener(createListener());
components[i].getMapButton().setText(config.getMapping(components[i])); comp.getMapButton().setText(config.getMapping(comp));
components[i].getInvertBox().addItemListener(new ItemListener() { if (mapping != null) {
comp.getInvertBox().setSelected(mapping.invert);
comp.getTriggerBox().setSelected(mapping.trigger);
}
comp.getInvertBox().addItemListener(new ItemListener() {
@Override @Override
public void itemStateChanged(ItemEvent e) { public void itemStateChanged(ItemEvent e) {
JCheckBox clicked = (JCheckBox)e.getItem(); JCheckBox clicked = (JCheckBox)e.getItem();
ControllerComponent contComp = ControllerComponent.valueOf(clicked.getName()); ControllerComponent contComp = ControllerComponent.valueOf(clicked.getName());
contComp.invert(e.getStateChange() == ItemEvent.SELECTED); config.getMappedComponent(contComp).invert = (e.getStateChange() == ItemEvent.SELECTED);
configChanged = true; configChanged = true;
} }
}); });
components[i].getTriggerBox().addItemListener(new ItemListener() { comp.getTriggerBox().addItemListener(new ItemListener() {
@Override @Override
public void itemStateChanged(ItemEvent e) { public void itemStateChanged(ItemEvent e) {
JCheckBox clicked = (JCheckBox)e.getItem(); JCheckBox clicked = (JCheckBox)e.getItem();
ControllerComponent contComp = ControllerComponent.valueOf(clicked.getName()); ControllerComponent contComp = ControllerComponent.valueOf(clicked.getName());
contComp.trigger(e.getStateChange() == ItemEvent.SELECTED); config.getMappedComponent(contComp).trigger = (e.getStateChange() == ItemEvent.SELECTED);
configChanged = true; configChanged = true;
} }
}); });
@@ -140,6 +153,7 @@ public class SettingsFrame extends JFrame {
if (shouldStartHandler) { if (shouldStartHandler) {
GamepadHandler.startUp(); GamepadHandler.startUp();
} }
dispose();
} }
}); });
@@ -215,13 +229,17 @@ public class SettingsFrame extends JFrame {
consumeEvents.setName("Consume Events Thread"); consumeEvents.setName("Consume Events Thread");
consumeEvents.start(); consumeEvents.start();
ControllerComponent oldConfig = config.getControllerComponent(newMapping); Mapping oldConfig = config.getMapping(newMapping);
if (oldConfig != null) { if (oldConfig != null) {
config.removeMapping(newMapping); config.removeMapping(newMapping);
oldConfig.getMapButton().setText(""); oldConfig.contComp.getMapButton().setText("");
} }
config.insertMapping(contComp, newMapping); Mapping newConfig = config.getMappedComponent(contComp);
if (newConfig == null) {
newConfig = config.new Mapping(contComp, false, false);
}
config.insertMapping(newConfig, newMapping);
contComp.getMapButton().setText(newMapping.getName()); contComp.getMapButton().setText(newMapping.getName());
configChanged = true; configChanged = true;
contComp.getMapButton().setSelected(false); contComp.getMapButton().setSelected(false);

View File

@@ -19,8 +19,6 @@ public enum ControllerComponent implements Serializable {
private JCheckBox invertBox; private JCheckBox invertBox;
private JCheckBox triggerBox; private JCheckBox triggerBox;
private boolean analog; private boolean analog;
private boolean invert;
private boolean trigger;
private ControllerComponent(String name, boolean analog) { private ControllerComponent(String name, boolean analog) {
this.label = new JLabel(name); this.label = new JLabel(name);
@@ -32,8 +30,6 @@ public enum ControllerComponent implements Serializable {
this.triggerBox.setName(this.name()); this.triggerBox.setName(this.name());
this.triggerBox.setToolTipText("If this component should act as a trigger."); this.triggerBox.setToolTipText("If this component should act as a trigger.");
this.analog = analog; this.analog = analog;
this.invert = false;
this.trigger = false;
} }
public JLabel getLabel() { public JLabel getLabel() {
@@ -56,19 +52,7 @@ public enum ControllerComponent implements Serializable {
return analog; return analog;
} }
public boolean isTrigger() { public boolean sameAs(ControllerComponent other) {
return trigger; return this.name().equals(other.name());
}
public void trigger(boolean isTrigger) {
trigger = isTrigger;
}
public void invert(boolean invert) {
this.invert = invert;
}
public boolean invert() {
return invert;
} }
} }

View File

@@ -1,5 +1,6 @@
package com.limelight.input; package com.limelight.input;
import com.limelight.input.GamepadMapping.Mapping;
import com.limelight.nvstream.NvConnection; import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.ControllerPacket; import com.limelight.nvstream.input.ControllerPacket;
@@ -104,22 +105,22 @@ public class Gamepad {
} }
private void handleComponent(Component comp, float value) { private void handleComponent(Component comp, float value) {
ControllerComponent contComp = config.getControllerComponent(comp); Mapping mapping = config.getMapping(comp);
if (contComp != null) { if (mapping != null) {
if (contComp.isAnalog()) { if (mapping.contComp.isAnalog()) {
handleAnalog(contComp, sanitizeValue(contComp, value)); handleAnalog(mapping.contComp, sanitizeValue(mapping, value));
} else { } else {
handleButtons(contComp, sanitizeValue(contComp, value)); handleButtons(mapping.contComp, sanitizeValue(mapping, value));
} }
} }
} }
private float sanitizeValue(ControllerComponent contComp, float value) { private float sanitizeValue(Mapping mapping, float value) {
float sanitized = value; float sanitized = value;
if (contComp.invert()) { if (mapping.invert) {
sanitized = -sanitized; sanitized = -sanitized;
} }
if (contComp.isTrigger()) { if (mapping.trigger) {
sanitized = (sanitized + 1)/2; sanitized = (sanitized + 1)/2;
} }
return sanitized; return sanitized;

View File

@@ -9,17 +9,17 @@ import net.java.games.input.Component;
public class GamepadMapping implements Serializable { public class GamepadMapping implements Serializable {
private static final long serialVersionUID = -185035113915743149L; private static final long serialVersionUID = -185035113915743149L;
private HashMap<String, ControllerComponent> mapping; private HashMap<String, Mapping> mapping;
public GamepadMapping() { public GamepadMapping() {
mapping = new HashMap<String, ControllerComponent>(); mapping = new HashMap<String, Mapping>();
} }
public void insertMapping(ControllerComponent contComp, Component comp) { public void insertMapping(Mapping toMap, Component comp) {
mapping.put(comp.getIdentifier().getName(), contComp); mapping.put(comp.getIdentifier().getName(), toMap);
} }
public ControllerComponent getControllerComponent(Component comp) { public Mapping getMapping(Component comp) {
return mapping.get(comp.getIdentifier().getName()); return mapping.get(comp.getIdentifier().getName());
} }
@@ -27,6 +27,21 @@ public class GamepadMapping implements Serializable {
mapping.remove(comp.getIdentifier().getName()); mapping.remove(comp.getIdentifier().getName());
} }
/**
* Gets the mapped ControllerComponent for the specified ControllerComponent.</br>
* NOTE: Use sparingly takes O(N) time.
* @param contComp the component to get a mapping for
* @return a mapping or an null if there is none
*/
public Mapping getMappedComponent(ControllerComponent contComp) {
for (Entry<String, Mapping> entry : mapping.entrySet()) {
if (entry.getValue().contComp.sameAs(contComp)) {
return entry.getValue();
}
}
return null;
}
/** /**
* Gets the mapping for the specified component.</br> * Gets the mapping for the specified component.</br>
* NOTE: Use sparingly takes O(N) time. * NOTE: Use sparingly takes O(N) time.
@@ -34,11 +49,25 @@ public class GamepadMapping implements Serializable {
* @return a mapping or an empty string if there is none * @return a mapping or an empty string if there is none
*/ */
public String getMapping(ControllerComponent contComp) { public String getMapping(ControllerComponent contComp) {
for (Entry<String, ControllerComponent> entry : mapping.entrySet()) { for (Entry<String, Mapping> entry : mapping.entrySet()) {
if (entry.getValue().equals(contComp)) { if (entry.getValue().contComp.sameAs(contComp)) {
return entry.getKey(); return entry.getKey();
} }
} }
return ""; return "";
} }
public class Mapping implements Serializable {
private static final long serialVersionUID = -8407172977953214242L;
public ControllerComponent contComp;
public boolean invert;
public boolean trigger;
public Mapping(ControllerComponent contComp, boolean invert, boolean trigger) {
this.contComp = contComp;
this.invert = invert;
this.trigger = trigger;
}
}
} }