From c32d7a1c226bb5841b13a5e30fb5d4d57d150a08 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Thu, 19 Dec 2013 17:31:14 -0500 Subject: [PATCH] Work in progress: fixing gamepad to work on all OSs and be customizable --- src/com/limelight/gui/SettingsFrame.java | 113 ++++++++++++++++++ .../limelight/input/ControllerComponent.java | 31 +++++ src/com/limelight/input/Gamepad.java | 12 ++ src/com/limelight/input/GamepadHandler.java | 7 ++ src/com/limelight/input/GamepadSettings.java | 17 +++ 5 files changed, 180 insertions(+) create mode 100644 src/com/limelight/gui/SettingsFrame.java create mode 100644 src/com/limelight/input/ControllerComponent.java create mode 100644 src/com/limelight/input/GamepadSettings.java diff --git a/src/com/limelight/gui/SettingsFrame.java b/src/com/limelight/gui/SettingsFrame.java new file mode 100644 index 0000000..75c8a60 --- /dev/null +++ b/src/com/limelight/gui/SettingsFrame.java @@ -0,0 +1,113 @@ +package com.limelight.gui; + +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import net.java.games.input.Component; +import net.java.games.input.Event; +import net.java.games.input.EventQueue; + +import com.limelight.input.ControllerComponent; +import com.limelight.input.Gamepad; +import com.limelight.input.GamepadHandler; +import com.limelight.input.GamepadSettings; + +public class SettingsFrame extends JFrame { + private static final long serialVersionUID = 1L; + + public SettingsFrame() { + super("Limelight Settings"); + this.setSize(800, 500); + this.setResizable(false); + this.setAlwaysOnTop(true); + } + + public void build() { + Container c = this.getContentPane(); + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)); + + Box leftColumn = Box.createVerticalBox(); + Box rightColumn = Box.createVerticalBox(); + + leftColumn.add(Box.createVerticalStrut(10)); + rightColumn.add(Box.createVerticalStrut(10)); + + + ControllerComponent[] components = ControllerComponent.values(); + for (int i = 0; i < components.length; i++) { + Box componentBox = Box.createHorizontalBox(); + + componentBox.add(Box.createHorizontalStrut(10)); + componentBox.add(components[i].getLabel()); + componentBox.add(Box.createHorizontalGlue()); + componentBox.add(components[i].getTextField()); + componentBox.add(Box.createHorizontalStrut(10)); + components[i].getTextField().setColumns(10); + components[i].getTextField().setMaximumSize(new Dimension(50, 30)); + components[i].getTextField().addActionListener(createListener()); + if (i > components.length / 2) { + rightColumn.add(componentBox); + if (i < components.length - 1) { + rightColumn.add(Box.createVerticalStrut(5)); + } + } else { + leftColumn.add(componentBox); + if (i < components.length / 2 - 1) { + leftColumn.add(Box.createVerticalStrut(5)); + } + } + } + + rightColumn.add(Box.createVerticalGlue()); + leftColumn.add(Box.createVerticalGlue()); + + mainPanel.add(Box.createHorizontalStrut(20)); + mainPanel.add(leftColumn); + mainPanel.add(Box.createHorizontalGlue()); + mainPanel.add(rightColumn); + + mainPanel.add(Box.createHorizontalStrut(20)); + + c.add(mainPanel); + + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); + this.setVisible(true); + } + + private ActionListener createListener() { + return new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //#allthejank + ControllerComponent contComp = ControllerComponent.valueOf(((JTextField)e.getSource()).getName()); + + contComp.getTextField().setText("Select Input"); + + Gamepad listenPad = GamepadHandler.getGamepads().get(0); + listenPad.poll(); + EventQueue queue = listenPad.getEvents(); + Event event = new Event(); + queue.getNextEvent(event); + Component comp = event.getComponent(); + contComp.getTextField().setText(comp.getName()); + + GamepadSettings config = listenPad.getConfiguration(); + + config.insertSetting(contComp, comp); + + } + }; + } + +} diff --git a/src/com/limelight/input/ControllerComponent.java b/src/com/limelight/input/ControllerComponent.java new file mode 100644 index 0000000..65c75d7 --- /dev/null +++ b/src/com/limelight/input/ControllerComponent.java @@ -0,0 +1,31 @@ +package com.limelight.input; + +import javax.swing.JLabel; +import javax.swing.JTextField; + +public enum ControllerComponent { + BTN_A("Button 1 (A)"), BTN_X("Button 2 (X)"), BTN_Y("Button 3 (Y)"), BTN_B("Button 4 (B)"), + DPAD_UP("D-pad Up"), DPAD_DOWN("D-pad Down"), DPAD_LEFT("D-pad Left"), DPAD_RIGHT("D-pad Right"), + LS_X("Left Stick X"), LS_Y("Left Stick X"), RS_X("Right Stick X"), RS_Y("Left Stick Y"), + LS_THUMB("Left Stick Button"), RS_THUMB("Right Stick Button"), + LT("Left Trigger"), RT("Right Trigger"), LB("Left Bumper"), RB("Right Bumper"), + BTN_START("Start Button"), BTN_BACK("Back Button"), BTN_SPECIAL("Special Button"); + + private JLabel label; + private JTextField textBox; + + private ControllerComponent(String name) { + this.label = new JLabel(name); + this.textBox = new JTextField(); + this.textBox.setEditable(false); + this.textBox.setName(this.name()); + } + + public JLabel getLabel() { + return label; + } + + public JTextField getTextField() { + return textBox; + } +} diff --git a/src/com/limelight/input/Gamepad.java b/src/com/limelight/input/Gamepad.java index 62aec94..fee64fa 100644 --- a/src/com/limelight/input/Gamepad.java +++ b/src/com/limelight/input/Gamepad.java @@ -19,6 +19,8 @@ public abstract class Gamepad { protected short leftStickX = 0x0000; protected short leftStickY = 0x0000; + protected GamepadSettings configuration; + public enum ControllerType { XBOX, PS3 }; @@ -37,12 +39,18 @@ public abstract class Gamepad { this.conn = conn; this.pad = pad; + configuration = new GamepadSettings(); + for (Component comp : pad.getComponents()) { initValue(comp); } } + public GamepadSettings getConfiguration() { + return configuration; + } + private void initValue(Component comp) { handleComponent(comp, comp.getPollData()); } @@ -56,6 +64,10 @@ public abstract class Gamepad { leftStickX, leftStickY, rightStickX, rightStickY); } + public EventQueue getEvents() { + return pad.getEventQueue(); + } + public void handleEvents() { EventQueue queue = pad.getEventQueue(); Event event = new Event(); diff --git a/src/com/limelight/input/GamepadHandler.java b/src/com/limelight/input/GamepadHandler.java index 4fde2dc..c63daf5 100644 --- a/src/com/limelight/input/GamepadHandler.java +++ b/src/com/limelight/input/GamepadHandler.java @@ -1,9 +1,12 @@ package com.limelight.input; +import java.util.Collections; import java.util.LinkedList; import java.util.List; +import javax.swing.event.ListSelectionEvent; + import com.limelight.input.Gamepad.ControllerType; import com.limelight.nvstream.NvConnection; @@ -37,6 +40,10 @@ public class GamepadHandler { return null; } + public static List getGamepads() { + return Collections.unmodifiableList(gamepads); + } + private void startUp() { new Thread(new Runnable() { @Override diff --git a/src/com/limelight/input/GamepadSettings.java b/src/com/limelight/input/GamepadSettings.java new file mode 100644 index 0000000..7f38431 --- /dev/null +++ b/src/com/limelight/input/GamepadSettings.java @@ -0,0 +1,17 @@ +package com.limelight.input; + +import java.util.HashMap; + +import net.java.games.input.Component; + +public class GamepadSettings { + private HashMap mapping; + + public void insertSetting(ControllerComponent contComp, Component comp) { + mapping.put(contComp, comp); + } + + public Component getComponent(ControllerComponent contComp) { + return mapping.get(contComp); + } +}