1
0
forked from Clones/Controlify

store current controller in config

This commit is contained in:
isXander
2023-02-17 02:02:42 +00:00
parent 7e53fac611
commit 23c048f401
5 changed files with 60 additions and 14 deletions

View File

@ -13,7 +13,6 @@ import dev.isxander.controlify.ingame.guide.InGameButtonGuide;
import dev.isxander.controlify.ingame.InGameInputHandler;
import dev.isxander.controlify.mixins.feature.virtualmouse.MouseHandlerAccessor;
import dev.isxander.controlify.virtualmouse.VirtualMouseHandler;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.toasts.SystemToast;
@ -29,14 +28,14 @@ public class Controlify {
public static final Logger LOGGER = LogUtils.getLogger();
private static Controlify instance = null;
private Controller<?, ?> currentController;
private Controller<?, ?> currentController = Controller.DUMMY;
private InGameInputHandler inGameInputHandler;
public InGameButtonGuide inGameButtonGuide;
private VirtualMouseHandler virtualMouseHandler;
private InputMode currentInputMode;
private ControllerHIDService controllerHIDService;
private final ControlifyConfig config = new ControlifyConfig();
private final ControlifyConfig config = new ControlifyConfig(this);
private final Queue<Controller<?, ?>> calibrationQueue = new ArrayDeque<>();
@ -57,25 +56,39 @@ public class Controlify {
if (GLFW.glfwJoystickPresent(i)) {
int jid = i;
controllerHIDService.awaitNextController(device -> {
setCurrentController(Controller.createOrGet(jid, device));
LOGGER.info("Controller found: " + currentController.name());
var controller = Controller.createOrGet(jid, device);
LOGGER.info("Controller found: " + controller.name());
if (!config().loadOrCreateControllerData(currentController)) {
calibrationQueue.add(currentController);
if (config().currentControllerUid().equals(controller.uid()))
setCurrentController(controller);
if (!config().loadOrCreateControllerData(controller)) {
calibrationQueue.add(controller);
}
});
}
}
controllerHIDService.setOnQueueEmptyEvent(() -> {
if (currentController() == Controller.DUMMY && config().isFirstLaunch()) {
this.setCurrentController(Controller.CONTROLLERS.values().stream().findFirst().orElse(null));
}
});
controllerHIDService.start();
// listen for new controllers
GLFW.glfwSetJoystickCallback((jid, event) -> {
if (event == GLFW.GLFW_CONNECTED) {
controllerHIDService.awaitNextController(device -> {
setCurrentController(Controller.createOrGet(jid, device));
LOGGER.info("Controller connected: " + currentController.name());
this.setCurrentInputMode(InputMode.CONTROLLER);
var firstController = Controller.CONTROLLERS.values().isEmpty();
var controller = Controller.createOrGet(jid, device);
LOGGER.info("Controller connected: " + controller.name());
if (firstController) {
this.setCurrentController(controller);
this.setCurrentInputMode(InputMode.CONTROLLER);
}
if (!config().loadOrCreateControllerData(currentController)) {
calibrationQueue.add(currentController);
@ -188,6 +201,12 @@ public class Controlify {
if (this.currentController == controller) return;
this.currentController = controller;
LOGGER.info("Updated current controller to " + controller.name() + "(" + controller.uid() + ")");
if (!config().currentControllerUid().equals(controller.uid())) {
config().save();
}
this.inGameInputHandler = new InGameInputHandler(controller);
if (Minecraft.getInstance().player != null) {
this.inGameButtonGuide = new InGameButtonGuide(controller, Minecraft.getInstance().player);

View File

@ -20,10 +20,17 @@ public class ControlifyConfig {
.registerTypeHierarchyAdapter(Class.class, new ClassTypeAdapter())
.create();
private final Controlify controlify;
private String currentControllerUid;
private JsonObject controllerData = new JsonObject();
private GlobalSettings globalSettings = new GlobalSettings();
private boolean firstLaunch;
public ControlifyConfig(Controlify controlify) {
this.controlify = controlify;
}
public void save() {
Controlify.LOGGER.info("Saving Controlify config...");
@ -46,8 +53,8 @@ public class ControlifyConfig {
try {
applyConfig(GSON.fromJson(Files.readString(CONFIG_PATH), JsonObject.class));
} catch (IOException e) {
throw new IllegalStateException("Failed to load config!", e);
} catch (Exception e) {
Controlify.LOGGER.error("Failed to load Controlify config!", e);
}
}
@ -63,8 +70,8 @@ public class ControlifyConfig {
controllerData = newControllerData;
config.add("controllers", controllerData);
config.add("global", GSON.toJsonTree(globalSettings));
config.addProperty("current_controller", currentControllerUid = controlify.currentController().uid());
return config;
}
@ -89,6 +96,12 @@ public class ControlifyConfig {
loadOrCreateControllerData(controller);
}
}
if (object.has("current_controller")) {
currentControllerUid = object.get("current_controller").getAsString();
} else {
currentControllerUid = controlify.currentController().uid();
}
}
public boolean loadOrCreateControllerData(Controller<?, ?> controller) {
@ -122,4 +135,8 @@ public class ControlifyConfig {
public boolean isFirstLaunch() {
return firstLaunch;
}
public String currentControllerUid() {
return currentControllerUid;
}
}

View File

@ -1,6 +1,7 @@
package dev.isxander.controlify.config;
import com.google.common.collect.Lists;
import dev.isxander.controlify.Controlify;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import java.util.List;

View File

@ -68,7 +68,7 @@ public interface Controller<S extends ControllerState, C extends ControllerConfi
@Override
public String uid() {
return "DUMMY";
return "NONE";
}
@Override

View File

@ -20,6 +20,7 @@ public class ControllerHIDService implements HidServicesListener {
private final HidServicesSpecification specification;
private final Queue<Consumer<HidDevice>> deviceQueue;
private Runnable onQueueEmpty = () -> {};
private boolean disabled = false;
@ -59,6 +60,10 @@ public class ControllerHIDService implements HidServicesListener {
if (deviceQueue.peek() != null) {
try {
deviceQueue.poll().accept(device);
if (deviceQueue.isEmpty()) {
onQueueEmpty.run();
}
} catch (Throwable e) {
Controlify.LOGGER.error("Failed to handle controller device attach event.", e);
}
@ -75,6 +80,10 @@ public class ControllerHIDService implements HidServicesListener {
return isGenericDesktopControlOrGameControl && isController;
}
public void setOnQueueEmptyEvent(Runnable runnable) {
this.onQueueEmpty = runnable;
}
public boolean isDisabled() {
return disabled;
}