Giter Site home page Giter Site logo

valkryst / vcontroller Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 287 KB

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.

License: Apache License 2.0

Java 100.00%
jinput controller usb controller-api controller-manager game-development gamedev game-dev roguelike roguelike-library

vcontroller's Introduction

Java CI with Maven CodeQL

VController is a helper library for JInput with the following features:

  • Automatic polling of controller input events VIA ControllerPoller.
  • Automatic detection of controller connection and disconnection events VIA HotSwapPoller.
    • 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.
      • 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 and HotSwapListener.

This library assumes that you have already properly configured JInput and that it is working.

Table of Contents

Installation

VController is hosted on the JitPack package repository which supports Gradle, Maven, and sbt.

Gradle 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 Maven

Add JitPack as a repository.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add VController as a dependency.

<dependency>
    <groupId>com.github.Valkryst</groupId>
    <artifactId>VController</artifactId>
    <version>2023.10.26</version>
</dependency>

Scala SBT 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.

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.

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

vcontroller's People

Contributors

dependabot[bot] avatar valkryst avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

vcontroller's Issues

Lower minimum Java version

Could you perhaps lower the Java version to something lower like the oldest supported JDK for example? I want to use the library for a large repo but can't because we're still on 17.

Memory leak

There's a memory leak somewhere, the RAM usage climbs to about 1.6 GB after around 40 minutes and all the connected devices get lost.

image

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.