Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/valkryst/vcontroller

A helper library for JInput which makes it easy to automatically poll for controller input, connection, and disconnection events and notify listeners when they occur.
https://github.com/valkryst/vcontroller

controller controller-api controller-manager game-dev game-development gamedev hacktoberfest jinput roguelike roguelike-library roguelikedev usb

Last synced: 14 days ago
JSON representation

A helper library for JInput which makes it easy to automatically poll for controller input, connection, and disconnection events and notify listeners when they occur.

Awesome Lists containing this project

README

        

[![Java CI with Maven](https://github.com/Valkryst/VController/actions/workflows/maven.yml/badge.svg)](https://github.com/Valkryst/VController/actions/workflows/maven.yml)
[![CodeQL](https://github.com/Valkryst/VController/actions/workflows/codeql.yml/badge.svg)](https://github.com/Valkryst/VController/actions/workflows/codeql.yml)

VController is a helper library for [JInput](https://github.com/jinput/jinput) with the following features:

* Automatic polling of controller input events VIA [ControllerPoller](https://github.com/Valkryst/VController/blob/master/src/main/java/com/valkryst/VController/ControllerPoller.java).
* Automatic detection of controller connection and disconnection events VIA [HotSwapPoller](https://github.com/Valkryst/VController/blob/master/src/main/java/com/valkryst/VController/HotSwapPoller.java).
* As JInput does not natively support hot-swapping, this library uses a polling approach to detect it. The downside to this approach is that JInput may print messages to `System.err` on every poll.
* **This _will_ cause a resource leak on Windows. A solution is being discussed [here](https://github.com/Valkryst/VController/issues/6).**
* To mitigate this issue, I suggest only enabling the `HotSwapPoller` when showing the user a list of controllers to select from. Reducing the polling rate will also help as each poll creates a "dead" thread which takes up system resources.
* Interfaces to listen to controller and hot-swap events VIA [ControllerListener](https://github.com/Valkryst/VController/blob/master/src/main/java/com/valkryst/VController/ControllerListener.java) and [HotSwapListener](https://github.com/Valkryst/VController/blob/master/src/main/java/com/valkryst/VController/HotSwapListener.java).

This library assumes that you have already [properly configured](https://jinput.github.io/jinput/) JInput and that it is working.

## Table of Contents

* [Installation](https://github.com/Valkryst/VController#installation)
* [Gradle](https://github.com/Valkryst/VController#-gradle)
* [Maven](https://github.com/Valkryst/VController#-maven)
* [sbt](https://github.com/Valkryst/VController#-scala-sbt)
* [Example](https://github.com/Valkryst/VController#example)
* [Controller Polling](https://github.com/Valkryst/VController#controller-polling)
* [Hot Swap Polling](https://github.com/Valkryst/VController#hot-swap-polling)

## Installation

VController is hosted on the [JitPack package repository](https://jitpack.io/#Valkryst/VController)
which supports Gradle, Maven, and sbt.

### ![Gradle](https://i.imgur.com/qtc6bXq.png?1) Gradle

Add JitPack to your `build.gradle` at the end of repositories.

```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

Add VController as a dependency.

```
dependencies {
implementation 'com.github.Valkryst:VController:2023.10.26'
}
```

### ![Maven](https://i.imgur.com/2TZzobp.png?1) Maven

Add JitPack as a repository.

``` xml


jitpack.io
https://jitpack.io

```
Add VController as a dependency.

```xml

com.github.Valkryst
VController
2023.10.26

```

### ![Scala SBT](https://i.imgur.com/Nqv3mVd.png?1) Scala SBT

Add JitPack as a resolver.

```
resolvers += "jitpack" at "https://jitpack.io"
```

Add VController as a dependency.

```
libraryDependencies += "com.github.Valkryst" % "VController" % "2023.10.26"
```

## Example

### Controller Polling

This example prompts you to select a controller, then it continuously polls the controller for events and prints them to the console.

```java
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

import java.util.Scanner;

public class ControllerExample {
public static void main(String[] args) {
final var controllerPoller = new ControllerPoller(getController());
controllerPoller.addListener(event -> {
final var component = event.getComponent();
System.out.println(component.getName());
System.out.println(component.getIdentifier());
System.out.println(component.getDeadZone());
System.out.println(component.getPollData());
System.out.println();
});
controllerPoller.start(16);

System.out.println("\nPolling for events. Try moving the controller's joysticks and pressing its buttons.");
}

private static ControllerEnvironment getEnvironment() {
final var environment = ControllerEnvironment.getDefaultEnvironment();

if (!environment.isSupported()) {
throw new IllegalStateException("Controller environment is not supported.");
}

return environment;
}

private static Controller getController() {
final var environment = getEnvironment();
final var controllers = environment.getControllers();

System.out.println("Detected Controllers");
for (int i = 0 ; i < controllers.length ; i++) {
System.out.println("\t" + i + ": " + controllers[i].getName());
}

System.out.print("\nEnter a number to select a controller: ");
final var scanner = new Scanner(System.in);
final var index = scanner.nextInt();
System.out.println("\nYou selected: " + controllers[index].getName());

return controllers[index];
}
}
```

### Hot Swap Polling

This example prints a message to the console when a controller is connected or disconnected.

```java
import net.java.games.input.Controller;

public class HotSwapExample {
public static void main(final String[] args) {
final var hotswapPoller = HotSwapPoller.getInstance();
hotswapPoller.addListener(new HotSwapListener() {
@Override
public void controllerAdded(Controller controller) {
System.out.println("Added: " + controller.getName());
}

@Override
public void controllerRemoved(Controller controller) {
System.out.println("Removed: " + controller.getName());
}
});

hotswapPoller.start();

System.out.println("Polling for hot-swaps. Try adding or removing a controller.");
}
}
```

## Credits & Inspiration

* [litiengine](https://github.com/gurkenlabs/litienginehttps://github.com/gurkenlabs/litiengine)