Added javadoc to gamepad classes

This commit is contained in:
Diego Waxemberg
2013-12-29 13:01:25 -05:00
parent adb36093e4
commit f892a32a78
5 changed files with 147 additions and 7 deletions

View File

@@ -9,6 +9,10 @@ import net.java.games.input.Controller;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
/**
* Represents a gamepad connected to the system
* @author Diego Waxemberg
*/
public class Gamepad {
private Controller pad;
private GamepadMapping config;
@@ -21,6 +25,11 @@ public class Gamepad {
private short leftStickX = 0x0000;
private short leftStickY = 0x0000;
/**
* Constructs a new gamepad from the specified controller that has the given mappings
* @param pad the controller to be used as a gamepad
* @param settings the mappings for the gamepad
*/
public Gamepad(Controller pad, GamepadMapping settings) {
this.config = settings;
this.pad = pad;
@@ -30,18 +39,24 @@ public class Gamepad {
}
}
public GamepadMapping getConfiguration() {
return config;
}
/*
* Initializes the value of the given component to its current state
*/
private void initValue(Component comp) {
handleComponent(comp, comp.getPollData());
}
/**
* Polls the gamepad for component values.
* @return true if the gamepad was polled successfully, false otherwise
*/
public boolean poll() {
return pad.poll();
}
/*
* Sends a controller packet to the specified connection containing the current gamepad values
*/
private void sendControllerPacket(NvConnection conn) {
if (conn != null) {
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
@@ -49,10 +64,19 @@ public class Gamepad {
}
}
/**
* Gets the event queue for this gamepad
* @return this gamepad's event queue
*/
public EventQueue getEvents() {
return pad.getEventQueue();
}
/**
* Handles the events in this gamepad's event queue and sends them
* to the specified connection
* @param conn the connection to the host that will receive the events
*/
public void handleEvents(NvConnection conn) {
EventQueue queue = pad.getEventQueue();
Event event = new Event();
@@ -70,6 +94,7 @@ public class Gamepad {
}
/*
* Prints out the specified event information for the given gamepad
* used for debugging, normally unused.
*/
@SuppressWarnings("unused")
@@ -97,6 +122,9 @@ public class Gamepad {
System.out.println(builder.toString());
}
/*
* Handles a given event
*/
private void handleEvent(Event event) {
Component comp = event.getComponent();
float value = event.getValue();
@@ -104,6 +132,9 @@ public class Gamepad {
handleComponent(comp, value);
}
/*
* Handles the component that an event occurred on
*/
private void handleComponent(Component comp, float value) {
Mapping mapping = config.get(comp);
if (mapping != null) {
@@ -115,6 +146,9 @@ public class Gamepad {
}
}
/*
* Fixes the value as specified in the mapping, that is inverts if needed, etc.
*/
private float sanitizeValue(Mapping mapping, float value) {
float sanitized = value;
if (mapping.invert) {
@@ -126,14 +160,20 @@ public class Gamepad {
return sanitized;
}
private void toggle(short button, boolean press) {
if (press) {
/*
* Toggles a flag that indicates the specified button was pressed or released
*/
private void toggle(short button, boolean pressed) {
if (pressed) {
inputMap |= button;
} else {
inputMap &= ~button;
}
}
/*
* Handles analog component input
*/
private void handleAnalog(GamepadComponent contComp, float value) {
switch (contComp) {
case LS_X:
@@ -159,7 +199,10 @@ public class Gamepad {
break;
}
}
/*
* Handles button input
*/
private void handleButtons(GamepadComponent contComp, float value) {
boolean press = false;

View File

@@ -4,6 +4,10 @@ import java.io.Serializable;
import javax.swing.JLabel;
/**
* Enumerator for every gamepad component GFE recognizes
* @author Diego Waxemberg
*/
public enum GamepadComponent implements Serializable {
BTN_A("Button 1 (A)", false), BTN_X("Button 2 (X)", false), BTN_Y("Button 3 (Y)", false), BTN_B("Button 4 (B)", false),
DPAD_UP("D-pad Up", false), DPAD_DOWN("D-pad Down", false), DPAD_LEFT("D-pad Left", false), DPAD_RIGHT("D-pad Right", false),
@@ -15,15 +19,26 @@ public enum GamepadComponent implements Serializable {
private JLabel label;
private boolean analog;
/*
* Constructs the enumerator with the given name for a label and whether it is analog or not
*/
private GamepadComponent(String name, boolean analog) {
this.label = new JLabel(name);
this.analog = analog;
}
/**
* Gets the label for this gamepad component
* @returns a label with the name of this component as the text
*/
public JLabel getLabel() {
return label;
}
/**
* Checks if this component is analog or digital
* @returns whether this component is analog
*/
public boolean isAnalog() {
return analog;
}

View File

@@ -12,6 +12,10 @@ import com.limelight.settings.GamepadSettingsManager;
import net.java.games.input.Controller;
/**
* A handler for all the gamepads attached to this system
* @author Diego Waxemberg
*/
public class GamepadHandler {
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
private static Lock gamepadLock = new ReentrantLock();
@@ -19,6 +23,10 @@ public class GamepadHandler {
private static Thread handler;
private static boolean run = true;
/**
* Adds the list of controllers as gamepads to be handled
* @param pads the controllers to be handled as gamepads
*/
public static void addGamepads(List<Controller> pads) {
LinkedList<Gamepad> newPadList = new LinkedList<Gamepad>();
@@ -33,14 +41,25 @@ public class GamepadHandler {
gamepadLock.unlock();
}
/**
* Sets the connection to which the handled gamepads should send events
* @param connection the connection to the host
*/
public static void setConnection(NvConnection connection) {
conn = connection;
}
/**
* Gets a list of the currently handled gamepads
* @return an unmodifiable list of gamepads this handler handles
*/
public static List<Gamepad> getGamepads() {
return Collections.unmodifiableList(gamepads);
}
/**
* Starts up a thread that handles the gamepads
*/
public static void startUp() {
if (handler == null || !handler.isAlive()) {
run = true;
@@ -73,6 +92,9 @@ public class GamepadHandler {
}
}
/**
* Stops handling the gamepads
*/
public static void stopHandler() {
if (handler != null && handler.isAlive()) {
System.out.println("Stopping Gamepad Handler thread");
@@ -81,6 +103,10 @@ public class GamepadHandler {
}
}
/**
* Checks if the handler is handling the gamepads.
* @return whether this handler is running
*/
public static boolean isRunning() {
return (handler != null && handler.isAlive());
}

View File

@@ -8,6 +8,10 @@ import com.limelight.nvstream.NvConnection;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
/**
* Listens to <code>Controller</code>s connected to this computer and gives any gamepad to the gamepad handler
* @author Diego Waxemberg
*/
public class GamepadListener {
private static Thread listenerThread;
private static NvConnection conn;
@@ -79,6 +83,9 @@ public class GamepadListener {
return false;
}
/**
* Stops listening for controllers, ie. stops the thread if it is running
*/
public static void stopListening() {
if (listenerThread != null && listenerThread.isAlive()) {
System.out.println("Stopping Controller Listener thread");
@@ -89,6 +96,10 @@ public class GamepadListener {
}
}
/**
* Tells the handler to start sending gamepad events to the specified connection
* @param connection the connection to the host that will receive gamepad events
*/
public static void startSendingInput(NvConnection connection) {
System.out.println("Starting to send controller input");
conn = connection;
@@ -97,6 +108,9 @@ public class GamepadListener {
}
}
/**
* Tells the handler to stop sending events to the host
*/
public static void stopSendingInput() {
System.out.println("Stopping sending controller input");
conn = null;

View File

@@ -7,23 +7,44 @@ import java.util.Map.Entry;
import net.java.games.input.Component;
/**
* Mappings for gamepad components
* @author Diego Waxemberg
*/
public class GamepadMapping implements Serializable {
private static final long serialVersionUID = -185035113915743149L;
private HashMap<String, Mapping> mapping;
/**
* Constructs a new mapping that has nothing mapped.
*/
public GamepadMapping() {
mapping = new HashMap<String, Mapping>();
}
/**
* Inserts the specified mapping into this map
* @param toMap a <code>Mapping</code> that will be mapped to the specified gamepad component
* @param comp the gamepad component to map to.
*/
public void insertMapping(Mapping toMap, Component comp) {
mapping.put(comp.getIdentifier().getName(), toMap);
}
/**
* Gets the mapping for the specified gamepad component
* @param comp the gamepad component to get a mapping for
* @return a mapping for the requested component
*/
public Mapping get(Component comp) {
return mapping.get(comp.getIdentifier().getName());
}
/**
* Removes the mapping to the specified component
* @param comp the component to no longer be mapped.
*/
public void remove(Component comp) {
mapping.remove(comp.getIdentifier().getName());
}
@@ -57,13 +78,34 @@ public class GamepadMapping implements Serializable {
return "";
}
/**
* Represents a mapping, that is which gamepad component, whether it is inverted, a trigger, etc.
* @author Diego Waxemberg
*/
public class Mapping implements Serializable {
private static final long serialVersionUID = -8407172977953214242L;
/**
* The component this mapping belongs to
*/
public GamepadComponent contComp;
/**
* Whether the value of this component should be inverted
*/
public boolean invert;
/**
* Whether this component should be treated as a trigger
*/
public boolean trigger;
/**
* Constructs a new mapping with the specified configuration
* @param contComp the component this mapping belongs to
* @param invert whether the value should be inverted
* @param trigger whether this component should be treated as a trigger
*/
public Mapping(GamepadComponent contComp, boolean invert, boolean trigger) {
this.contComp = contComp;
this.invert = invert;