-
-
Notifications
You must be signed in to change notification settings - Fork 63
Add global option to disable automatic controller switching #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: multiversion/dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -471,14 +471,16 @@ public void tick(Minecraft client) { | |
| currentController | ||
| ); | ||
| }); | ||
| for (ControllerEntity controller : controllerManager.getConnectedControllers()) { | ||
| if (controller.equals(getCurrentController().orElse(null))) continue; | ||
|
|
||
| wrapControllerError( | ||
| () -> tickInactiveController(controller), | ||
| "Ticking inactive controller", | ||
| controller | ||
| ); | ||
| if (config().getSettings().globalSettings().autoSwitchControllers) { | ||
| for (ControllerEntity controller : controllerManager.getConnectedControllers()) { | ||
| if (controller.equals(getCurrentController().orElse(null))) continue; | ||
|
|
||
| wrapControllerError( | ||
| () -> tickInactiveController(controller), | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't agree with how this is done here. Inactive controllers should still be ticked. Move this if statement within |
||
| "Ticking inactive controller", | ||
| controller | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Periodically save config if dirty (e.g., from gyro calibration updates) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,8 @@ public record GlobalConfig( | |
| boolean alwaysAllowKeyboardMovement, | ||
| List<String> keyboardMovementWhitelist, | ||
| List<String> seenServers, | ||
| boolean showSplitscreenAd | ||
| boolean showSplitscreenAd, | ||
| boolean autoSwitchControllers | ||
| ) { | ||
| public static final Codec<GlobalConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
| Codec.list(Codec.STRING).fieldOf("virtual_mouse_screens").forGetter(GlobalConfig::virtualMouseScreens), | ||
|
|
@@ -34,6 +35,7 @@ public record GlobalConfig( | |
| Codec.BOOL.fieldOf("keyboard_movement").forGetter(GlobalConfig::alwaysAllowKeyboardMovement), | ||
| Codec.list(Codec.STRING).fieldOf("keyboard_movement_whitelist").forGetter(GlobalConfig::keyboardMovementWhitelist), | ||
| Codec.list(Codec.STRING).fieldOf("seen_servers").forGetter(GlobalConfig::seenServers), | ||
| Codec.BOOL.fieldOf("show_splitscreen_ad").forGetter(GlobalConfig::showSplitscreenAd) | ||
| Codec.BOOL.fieldOf("show_splitscreen_ad").forGetter(GlobalConfig::showSplitscreenAd), | ||
| Codec.BOOL.optionalFieldOf("auto_switch_controllers", true).forGetter(GlobalConfig::autoSwitchControllers) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DTOs should not have optional fields.
|
||
| ).apply(instance, GlobalConfig::new)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import dev.isxander.controlify.Controlify; | ||
| import dev.isxander.controlify.api.ControlifyApi; | ||
| import dev.isxander.controlify.api.event.ControlifyEvents; | ||
| import dev.isxander.controlify.config.settings.GlobalSettings; | ||
| import dev.isxander.controlify.controller.ControllerEntity; | ||
| import dev.isxander.controlify.driver.steamdeck.SteamDeckUtil; | ||
|
|
@@ -21,16 +22,27 @@ | |
| import net.minecraft.network.chat.Component; | ||
| import net.minecraft.resources.Identifier; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public class GlobalSettingsScreenFactory { | ||
| public static Screen createGlobalSettingsScreen(Screen parent) { | ||
| var globalSettings = Controlify.instance().config().getSettings().globalSettings(); | ||
| AtomicReference<ListOption<String>> whitelist = new AtomicReference<>(); | ||
| AtomicReference<Option<String>> controllerSelector = new AtomicReference<>(); | ||
|
|
||
| List<String> controllerUids = new ArrayList<>(); | ||
| controllerUids.add(""); | ||
| Controlify.instance().getControllerManager().ifPresent(cm -> { | ||
| for (ControllerEntity c : cm.getConnectedControllers()) { | ||
| controllerUids.add(c.uid()); | ||
| } | ||
| }); | ||
|
|
||
| boolean is12106OrLater = /*? if >=1.21.6 {*/ true /*?} else {*/ /*false *//*?}*/;; | ||
|
|
||
| return YetAnotherConfigLib.createBuilder() | ||
| Screen result = YetAnotherConfigLib.createBuilder() | ||
| .title(Component.translatable("controlify.gui.global_settings.title")) | ||
| .save(() -> Controlify.instance().config().saveSafely()) | ||
| .category(ConfigCategory.createBuilder() | ||
|
|
@@ -153,6 +165,56 @@ public static Screen createGlobalSettingsScreen(Screen parent) { | |
| .binding(GlobalSettings.defaults().mixedInput, () -> globalSettings.mixedInput, v -> globalSettings.mixedInput = v) | ||
| .controller(TickBoxControllerBuilder::create) | ||
| .build()) | ||
| .option(Option.<Boolean>createBuilder() | ||
| .name(Component.translatable("controlify.gui.auto_switch_controllers")) | ||
| .description(OptionDescription.createBuilder() | ||
| .text(Component.translatable("controlify.gui.auto_switch_controllers.tooltip")) | ||
| .build()) | ||
| .binding(GlobalSettings.defaults().autoSwitchControllers, () -> globalSettings.autoSwitchControllers, v -> globalSettings.autoSwitchControllers = v) | ||
| .controller(TickBoxControllerBuilder::create) | ||
| .addListener((opt, event) -> { | ||
| var selector = controllerSelector.get(); | ||
| if (selector != null) selector.setAvailable(!opt.pendingValue()); | ||
| }) | ||
| .build()) | ||
| .option(Util.make(() -> { | ||
| var opt = Option.<String>createBuilder() | ||
| .name(Component.translatable("controlify.gui.current_controller")) | ||
| .description(OptionDescription.createBuilder() | ||
| .text(Component.translatable("controlify.gui.current_controller.tooltip")) | ||
| .build()) | ||
| .binding( | ||
| "", | ||
| () -> Controlify.instance().getCurrentController().map(ControllerEntity::uid).orElse(""), | ||
| uid -> { | ||
| if (uid.isEmpty()) { | ||
| Controlify.instance().setCurrentController(null, true); | ||
| } else { | ||
| Controlify.instance().getControllerManager().ifPresent(cm -> | ||
| cm.getConnectedControllers().stream() | ||
| .filter(c -> c.uid().equals(uid)) | ||
| .findFirst() | ||
| .ifPresent(c -> Controlify.instance().setCurrentController(c, true))); | ||
| } | ||
| } | ||
| ) | ||
| .controller(o -> CyclingListControllerBuilder.create(o) | ||
| .values(controllerUids) | ||
| .formatValue(uid -> { | ||
| if (uid.isEmpty()) return Component.translatable("controlify.gui.carousel.entry.keyboard_mouse"); | ||
| return Controlify.instance().getControllerManager() | ||
| .map(cm -> cm.getConnectedControllers().stream() | ||
| .filter(c -> c.uid().equals(uid)) | ||
| .findFirst() | ||
| .map(c -> (Component) Component.literal(c.name())) | ||
| .orElse(Component.literal(uid))) | ||
| .orElse(Component.literal(uid)); | ||
| })) | ||
| .available(!globalSettings.autoSwitchControllers) | ||
| .build(); | ||
| controllerSelector.set(opt); | ||
| return opt; | ||
| })) | ||
| .optionIf(SteamDeckUtil.IS_STEAM_DECK, Option.<Boolean>createBuilder() | ||
| .name(Component.translatable("controlify.gui.use_enhanced_steam_deck_driver")) | ||
| .description(OptionDescription.createBuilder() | ||
|
|
@@ -182,6 +244,19 @@ public static Screen createGlobalSettingsScreen(Screen parent) { | |
| .build()) | ||
| .build()) | ||
| .build().generateScreen(parent); | ||
|
|
||
| ControlifyEvents.CONTROLLER_CONNECTED.register(event -> { | ||
| if (Minecraft.getInstance().screen == result) { | ||
| Minecraft.getInstance().setScreen(createGlobalSettingsScreen(parent)); | ||
| } | ||
| }); | ||
| ControlifyEvents.CONTROLLER_DISCONNECTED.register(event -> { | ||
| if (Minecraft.getInstance().screen == result) { | ||
| Minecraft.getInstance().setScreen(createGlobalSettingsScreen(parent)); | ||
| } | ||
| }); | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause a memory leak. It's not good to create throw-away event registers. |
||
| return result; | ||
| } | ||
|
|
||
| private static Identifier screenshot(String filename) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't bump versions in PRs.