mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-17 14:22:00 +00:00
Added javadoc to gamepad classes
This commit is contained in:
@@ -9,6 +9,10 @@ import net.java.games.input.Controller;
|
|||||||
import net.java.games.input.Event;
|
import net.java.games.input.Event;
|
||||||
import net.java.games.input.EventQueue;
|
import net.java.games.input.EventQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a gamepad connected to the system
|
||||||
|
* @author Diego Waxemberg
|
||||||
|
*/
|
||||||
public class Gamepad {
|
public class Gamepad {
|
||||||
private Controller pad;
|
private Controller pad;
|
||||||
private GamepadMapping config;
|
private GamepadMapping config;
|
||||||
@@ -21,6 +25,11 @@ public class Gamepad {
|
|||||||
private short leftStickX = 0x0000;
|
private short leftStickX = 0x0000;
|
||||||
private short leftStickY = 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) {
|
public Gamepad(Controller pad, GamepadMapping settings) {
|
||||||
this.config = settings;
|
this.config = settings;
|
||||||
this.pad = pad;
|
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) {
|
private void initValue(Component comp) {
|
||||||
handleComponent(comp, comp.getPollData());
|
handleComponent(comp, comp.getPollData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polls the gamepad for component values.
|
||||||
|
* @return true if the gamepad was polled successfully, false otherwise
|
||||||
|
*/
|
||||||
public boolean poll() {
|
public boolean poll() {
|
||||||
return pad.poll();
|
return pad.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sends a controller packet to the specified connection containing the current gamepad values
|
||||||
|
*/
|
||||||
private void sendControllerPacket(NvConnection conn) {
|
private void sendControllerPacket(NvConnection conn) {
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
|
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() {
|
public EventQueue getEvents() {
|
||||||
return pad.getEventQueue();
|
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) {
|
public void handleEvents(NvConnection conn) {
|
||||||
EventQueue queue = pad.getEventQueue();
|
EventQueue queue = pad.getEventQueue();
|
||||||
Event event = new Event();
|
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.
|
* used for debugging, normally unused.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@@ -97,6 +122,9 @@ public class Gamepad {
|
|||||||
System.out.println(builder.toString());
|
System.out.println(builder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Handles a given event
|
||||||
|
*/
|
||||||
private void handleEvent(Event event) {
|
private void handleEvent(Event event) {
|
||||||
Component comp = event.getComponent();
|
Component comp = event.getComponent();
|
||||||
float value = event.getValue();
|
float value = event.getValue();
|
||||||
@@ -104,6 +132,9 @@ public class Gamepad {
|
|||||||
handleComponent(comp, value);
|
handleComponent(comp, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Handles the component that an event occurred on
|
||||||
|
*/
|
||||||
private void handleComponent(Component comp, float value) {
|
private void handleComponent(Component comp, float value) {
|
||||||
Mapping mapping = config.get(comp);
|
Mapping mapping = config.get(comp);
|
||||||
if (mapping != null) {
|
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) {
|
private float sanitizeValue(Mapping mapping, float value) {
|
||||||
float sanitized = value;
|
float sanitized = value;
|
||||||
if (mapping.invert) {
|
if (mapping.invert) {
|
||||||
@@ -126,14 +160,20 @@ public class Gamepad {
|
|||||||
return sanitized;
|
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;
|
inputMap |= button;
|
||||||
} else {
|
} else {
|
||||||
inputMap &= ~button;
|
inputMap &= ~button;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Handles analog component input
|
||||||
|
*/
|
||||||
private void handleAnalog(GamepadComponent contComp, float value) {
|
private void handleAnalog(GamepadComponent contComp, float value) {
|
||||||
switch (contComp) {
|
switch (contComp) {
|
||||||
case LS_X:
|
case LS_X:
|
||||||
@@ -160,6 +200,9 @@ public class Gamepad {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Handles button input
|
||||||
|
*/
|
||||||
private void handleButtons(GamepadComponent contComp, float value) {
|
private void handleButtons(GamepadComponent contComp, float value) {
|
||||||
boolean press = false;
|
boolean press = false;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import java.io.Serializable;
|
|||||||
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumerator for every gamepad component GFE recognizes
|
||||||
|
* @author Diego Waxemberg
|
||||||
|
*/
|
||||||
public enum GamepadComponent implements Serializable {
|
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),
|
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),
|
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 JLabel label;
|
||||||
private boolean analog;
|
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) {
|
private GamepadComponent(String name, boolean analog) {
|
||||||
this.label = new JLabel(name);
|
this.label = new JLabel(name);
|
||||||
this.analog = analog;
|
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() {
|
public JLabel getLabel() {
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this component is analog or digital
|
||||||
|
* @returns whether this component is analog
|
||||||
|
*/
|
||||||
public boolean isAnalog() {
|
public boolean isAnalog() {
|
||||||
return analog;
|
return analog;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ import com.limelight.settings.GamepadSettingsManager;
|
|||||||
|
|
||||||
import net.java.games.input.Controller;
|
import net.java.games.input.Controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A handler for all the gamepads attached to this system
|
||||||
|
* @author Diego Waxemberg
|
||||||
|
*/
|
||||||
public class GamepadHandler {
|
public class GamepadHandler {
|
||||||
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
|
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
|
||||||
private static Lock gamepadLock = new ReentrantLock();
|
private static Lock gamepadLock = new ReentrantLock();
|
||||||
@@ -19,6 +23,10 @@ public class GamepadHandler {
|
|||||||
private static Thread handler;
|
private static Thread handler;
|
||||||
private static boolean run = true;
|
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) {
|
public static void addGamepads(List<Controller> pads) {
|
||||||
LinkedList<Gamepad> newPadList = new LinkedList<Gamepad>();
|
LinkedList<Gamepad> newPadList = new LinkedList<Gamepad>();
|
||||||
|
|
||||||
@@ -33,14 +41,25 @@ public class GamepadHandler {
|
|||||||
gamepadLock.unlock();
|
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) {
|
public static void setConnection(NvConnection connection) {
|
||||||
conn = 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() {
|
public static List<Gamepad> getGamepads() {
|
||||||
return Collections.unmodifiableList(gamepads);
|
return Collections.unmodifiableList(gamepads);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts up a thread that handles the gamepads
|
||||||
|
*/
|
||||||
public static void startUp() {
|
public static void startUp() {
|
||||||
if (handler == null || !handler.isAlive()) {
|
if (handler == null || !handler.isAlive()) {
|
||||||
run = true;
|
run = true;
|
||||||
@@ -73,6 +92,9 @@ public class GamepadHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops handling the gamepads
|
||||||
|
*/
|
||||||
public static void stopHandler() {
|
public static void stopHandler() {
|
||||||
if (handler != null && handler.isAlive()) {
|
if (handler != null && handler.isAlive()) {
|
||||||
System.out.println("Stopping Gamepad Handler thread");
|
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() {
|
public static boolean isRunning() {
|
||||||
return (handler != null && handler.isAlive());
|
return (handler != null && handler.isAlive());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ import com.limelight.nvstream.NvConnection;
|
|||||||
import net.java.games.input.Controller;
|
import net.java.games.input.Controller;
|
||||||
import net.java.games.input.ControllerEnvironment;
|
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 {
|
public class GamepadListener {
|
||||||
private static Thread listenerThread;
|
private static Thread listenerThread;
|
||||||
private static NvConnection conn;
|
private static NvConnection conn;
|
||||||
@@ -79,6 +83,9 @@ public class GamepadListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops listening for controllers, ie. stops the thread if it is running
|
||||||
|
*/
|
||||||
public static void stopListening() {
|
public static void stopListening() {
|
||||||
if (listenerThread != null && listenerThread.isAlive()) {
|
if (listenerThread != null && listenerThread.isAlive()) {
|
||||||
System.out.println("Stopping Controller Listener thread");
|
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) {
|
public static void startSendingInput(NvConnection connection) {
|
||||||
System.out.println("Starting to send controller input");
|
System.out.println("Starting to send controller input");
|
||||||
conn = connection;
|
conn = connection;
|
||||||
@@ -97,6 +108,9 @@ public class GamepadListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells the handler to stop sending events to the host
|
||||||
|
*/
|
||||||
public static void stopSendingInput() {
|
public static void stopSendingInput() {
|
||||||
System.out.println("Stopping sending controller input");
|
System.out.println("Stopping sending controller input");
|
||||||
conn = null;
|
conn = null;
|
||||||
|
|||||||
@@ -7,23 +7,44 @@ import java.util.Map.Entry;
|
|||||||
|
|
||||||
import net.java.games.input.Component;
|
import net.java.games.input.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappings for gamepad components
|
||||||
|
* @author Diego Waxemberg
|
||||||
|
*/
|
||||||
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, Mapping> mapping;
|
private HashMap<String, Mapping> mapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new mapping that has nothing mapped.
|
||||||
|
*/
|
||||||
public GamepadMapping() {
|
public GamepadMapping() {
|
||||||
mapping = new HashMap<String, Mapping>();
|
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) {
|
public void insertMapping(Mapping toMap, Component comp) {
|
||||||
mapping.put(comp.getIdentifier().getName(), toMap);
|
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) {
|
public Mapping get(Component comp) {
|
||||||
return mapping.get(comp.getIdentifier().getName());
|
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) {
|
public void remove(Component comp) {
|
||||||
mapping.remove(comp.getIdentifier().getName());
|
mapping.remove(comp.getIdentifier().getName());
|
||||||
}
|
}
|
||||||
@@ -57,13 +78,34 @@ public class GamepadMapping implements Serializable {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a mapping, that is which gamepad component, whether it is inverted, a trigger, etc.
|
||||||
|
* @author Diego Waxemberg
|
||||||
|
*/
|
||||||
public class Mapping implements Serializable {
|
public class Mapping implements Serializable {
|
||||||
private static final long serialVersionUID = -8407172977953214242L;
|
private static final long serialVersionUID = -8407172977953214242L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The component this mapping belongs to
|
||||||
|
*/
|
||||||
public GamepadComponent contComp;
|
public GamepadComponent contComp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the value of this component should be inverted
|
||||||
|
*/
|
||||||
public boolean invert;
|
public boolean invert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this component should be treated as a trigger
|
||||||
|
*/
|
||||||
public boolean 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) {
|
public Mapping(GamepadComponent contComp, boolean invert, boolean trigger) {
|
||||||
this.contComp = contComp;
|
this.contComp = contComp;
|
||||||
this.invert = invert;
|
this.invert = invert;
|
||||||
|
|||||||
Reference in New Issue
Block a user