Basic device filtering backend stuff

This commit is contained in:
Ash 2017-04-11 20:31:04 +10:00
parent a43a45857a
commit 24bdf92bbc
3 changed files with 81 additions and 11 deletions

View File

@ -46,6 +46,7 @@ import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import lombok.extern.java.Log; import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.util.Settings;
@Log @Log
public class GuiOptionsWindow extends JPanel { public class GuiOptionsWindow extends JPanel {
@ -99,11 +100,10 @@ public class GuiOptionsWindow extends JPanel {
cFilterList = new ControllerFilteringList(); cFilterList = new ControllerFilteringList();
cFilterList.setBackground(Color.BLUE); cFilterList.setBackground(Color.BLUE);
ControllerFilteringListItem cHidGamepadsListItem = new ControllerFilteringListItem("Gamepads (HID)"); for (Settings.ControllerFiltering.Type type : Settings.ControllerFiltering.Type.values()) {
cFilterList.add(cHidGamepadsListItem); ControllerFilteringListItem item = new ControllerFilteringListItem(type);
cFilterList.add(item);
ControllerFilteringListItem cKeyboardsListItem = new ControllerFilteringListItem("Keyboards (HID)"); }
cFilterList.add(cKeyboardsListItem);
add(cFilterList); add(cFilterList);
@ -137,24 +137,26 @@ public class GuiOptionsWindow extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final JCheckBox cBox; private final JCheckBox cBox;
private final Settings.ControllerFiltering.Type type;
private ControllerFilteringListItem(String type) { private ControllerFilteringListItem(Settings.ControllerFiltering.Type typeIn) {
super(new GridLayout(1, 1)); super(new GridLayout(1, 1));
this.type = typeIn;
cBox = new JCheckBox(type); cBox = new JCheckBox(type.getName());
cBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); cBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
cBox.setSelected(true); //TODO get checkbox state cBox.setSelected(Settings.ControllerFiltering.getFilterState(type));
cBox.addActionListener(new ActionListener() { cBox.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
//TODO actually change filtering Settings.ControllerFiltering.setFilterState(type, cBox.isSelected());
} }
}); });
add(cBox); add(cBox);
} }
public void updateItem() { public void updateItem() {
cBox.setSelected(true); //TODO get checkbox State cBox.setSelected(Settings.ControllerFiltering.getFilterState(type));
} }
//I can't believe I didn't figure this out for GuiControllerList //I can't believe I didn't figure this out for GuiControllerList

View File

@ -26,6 +26,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
import lombok.Getter; import lombok.Getter;
@ -117,6 +118,13 @@ public final class Settings {
} }
} }
String filterStates = prop.getProperty("filterStates");
if (filterStates != null) {
ControllerFiltering.loadFilterStates(filterStates);
} else {
ControllerFiltering.setDefaultFilterStates();
}
log.info("Loaded config successfully!"); log.info("Loaded config successfully!");
} }
@ -139,7 +147,8 @@ public final class Settings {
prop.setProperty("autoActivatingController", Boolean.toString(Settings.AUTO_ACTIVATE_CONTROLLER)); prop.setProperty("autoActivatingController", Boolean.toString(Settings.AUTO_ACTIVATE_CONTROLLER));
prop.setProperty("sendDataOnlyOnChanges", Boolean.toString(Settings.SEND_DATA_ONLY_ON_CHANGE)); prop.setProperty("sendDataOnlyOnChanges", Boolean.toString(Settings.SEND_DATA_ONLY_ON_CHANGE));
prop.setProperty("scanAutomaticallyForControllers", Boolean.toString(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS)); prop.setProperty("scanAutomaticallyForControllers", Boolean.toString(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS));
prop.setProperty("filterStates", ControllerFiltering.getFilterStates());
try { try {
FileOutputStream outStream = new FileOutputStream(configFile); FileOutputStream outStream = new FileOutputStream(configFile);
prop.store(outStream, "HIDToVPADNetworkClient"); prop.store(outStream, "HIDToVPADNetworkClient");
@ -189,4 +198,48 @@ public final class Settings {
public enum Platform { public enum Platform {
LINUX, WINDOWS, MAC_OS_X, UNKNOWN LINUX, WINDOWS, MAC_OS_X, UNKNOWN
} }
//TODO rename this to something less nonsensical
public static class ControllerFiltering {
public static enum Type {
HIDGAMEPAD (0, "HID Gamepads"),
HIDKEYBOARD (1, "HID Keyboards"),
HIDOTHER (2, "Other HIDs");
private int index;
@Getter private String name;
private Type(int index, String name) {
this.index = index;
this.name = name;
}
}
private static boolean[] filterStates = new boolean[Type.values().length];
public static String getFilterStates() {
return Arrays.toString(filterStates);
}
public static void loadFilterStates(String newFilterStates) {
boolean[] newFilterStatesParsed = Utilities.stringToBoolArray(newFilterStates);
if (newFilterStatesParsed.length != filterStates.length) {
//TODO handle changes in filtering more gracefully
log.warning("Number of controller filters in config does not match reality, using defaults...");
setDefaultFilterStates();
} else {
filterStates = newFilterStatesParsed;
}
}
public static void setFilterState(Type filter, boolean state) {
filterStates[filter.index] = state;
log.info("Just set " + filter + " to " + state);
}
public static boolean getFilterState(Type filter) {
return filterStates[filter.index];
}
public static void setDefaultFilterStates() {
filterStates[Type.HIDGAMEPAD.index] = true;
filterStates[Type.HIDKEYBOARD.index] = false;
filterStates[Type.HIDOTHER.index] = false;
}
}
} }

View File

@ -76,4 +76,19 @@ public final class Utilities {
public static short signedShortToByte(short value) { public static short signedShortToByte(short value) {
return signedShortToByte((int) value); return signedShortToByte((int) value);
} }
/**
* Arrays.toString(boolean[]) in reverse.
* https://stackoverflow.com/questions/456367/
* @param string
* @return array
*/
public static boolean[] stringToBoolArray(String string) {
String[] strings = string.replace("[", "").replace("]", "").split(", ");
boolean result[] = new boolean[strings.length];
for (int i = 0; i < result.length; i++) {
result[i] = Boolean.parseBoolean(strings[i]);
}
return result;
}
} }