From 24bdf92bbc09a8fbd90eb334c67b84e37df97dfa Mon Sep 17 00:00:00 2001 From: Ash Date: Tue, 11 Apr 2017 20:31:04 +1000 Subject: [PATCH] Basic device filtering backend stuff --- .../gui/GuiOptionsWindow.java | 22 ++++---- .../HIDToVPADNetworkClient/util/Settings.java | 55 ++++++++++++++++++- .../util/Utilities.java | 15 +++++ 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiOptionsWindow.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiOptionsWindow.java index 686b62b..bb32059 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiOptionsWindow.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiOptionsWindow.java @@ -46,6 +46,7 @@ import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; import lombok.extern.java.Log; +import net.ash.HIDToVPADNetworkClient.util.Settings; @Log public class GuiOptionsWindow extends JPanel { @@ -99,11 +100,10 @@ public class GuiOptionsWindow extends JPanel { cFilterList = new ControllerFilteringList(); cFilterList.setBackground(Color.BLUE); - ControllerFilteringListItem cHidGamepadsListItem = new ControllerFilteringListItem("Gamepads (HID)"); - cFilterList.add(cHidGamepadsListItem); - - ControllerFilteringListItem cKeyboardsListItem = new ControllerFilteringListItem("Keyboards (HID)"); - cFilterList.add(cKeyboardsListItem); + for (Settings.ControllerFiltering.Type type : Settings.ControllerFiltering.Type.values()) { + ControllerFilteringListItem item = new ControllerFilteringListItem(type); + cFilterList.add(item); + } add(cFilterList); @@ -137,24 +137,26 @@ public class GuiOptionsWindow extends JPanel { private static final long serialVersionUID = 1L; 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)); + this.type = typeIn; - cBox = new JCheckBox(type); + cBox = new JCheckBox(type.getName()); 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() { @Override public void actionPerformed(ActionEvent e) { - //TODO actually change filtering + Settings.ControllerFiltering.setFilterState(type, cBox.isSelected()); } }); add(cBox); } 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 diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index 74295fa..b01a3a0 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -26,6 +26,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Properties; 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!"); } @@ -139,7 +147,8 @@ public final class Settings { prop.setProperty("autoActivatingController", Boolean.toString(Settings.AUTO_ACTIVATE_CONTROLLER)); prop.setProperty("sendDataOnlyOnChanges", Boolean.toString(Settings.SEND_DATA_ONLY_ON_CHANGE)); prop.setProperty("scanAutomaticallyForControllers", Boolean.toString(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS)); - + prop.setProperty("filterStates", ControllerFiltering.getFilterStates()); + try { FileOutputStream outStream = new FileOutputStream(configFile); prop.store(outStream, "HIDToVPADNetworkClient"); @@ -189,4 +198,48 @@ public final class Settings { public enum Platform { 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; + } + } } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java b/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java index af5f9de..52693ed 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java @@ -76,4 +76,19 @@ public final class Utilities { public static short signedShortToByte(short 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; + } }