1
0
forked from Clones/Controlify

compound joysticks, button guide in screens, improve API

This commit is contained in:
isXander
2023-03-26 18:13:02 +01:00
parent de210df84f
commit 0d9321e3ba
55 changed files with 1188 additions and 287 deletions

View File

@ -0,0 +1,46 @@
package dev.isxander.controlify.controller.joystick;
import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerType;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public record CompoundJoystickInfo(Collection<String> joystickUids, String friendlyName) {
public ControllerType type() {
return new ControllerType(friendlyName, createUID(joystickUids));
}
public boolean canBeUsed() {
List<Controller<?, ?>> joysticks = Controller.CONTROLLERS.values().stream().filter(c -> joystickUids.contains(c.uid())).toList();
if (joysticks.size() != joystickUids().size()) {
return false; // not all controllers are connected
}
if (joysticks.stream().anyMatch(c -> !c.canBeUsed())) {
return false; // not all controllers can be used
}
return true;
}
public boolean isLoaded() {
return Controller.CONTROLLERS.containsKey(createUID(joystickUids));
}
public Optional<CompoundJoystickController> attemptCreate() {
if (!canBeUsed()) return Optional.empty();
List<Integer> joystickIDs = Controller.CONTROLLERS.values().stream()
.filter(c -> joystickUids.contains(c.uid()))
.map(Controller::joystickId)
.toList();
ControllerType type = type();
return Optional.of(new CompoundJoystickController(joystickIDs, type.identifier(), type));
}
public static String createUID(Collection<String> joystickUIDs) {
return "compound-" + String.join("_", joystickUIDs);
}
}