1
0
forked from Clones/Controlify

Controller conflict detection

This commit is contained in:
isXander
2023-05-07 12:47:32 +01:00
parent 63ebf2dc61
commit fa1e1293c7
10 changed files with 258 additions and 44 deletions

View File

@ -0,0 +1,64 @@
package dev.isxander.controlify.bindings;
import com.google.common.collect.ImmutableSet;
import net.minecraft.resources.ResourceLocation;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public final class BindContext {
private final ResourceLocation context;
private final Set<BindContext> parents;
private final Set<ResourceLocation> flattened;
public BindContext(ResourceLocation context, Set<BindContext> parents) {
this.context = context;
this.parents = parents;
Set<ResourceLocation> flattened = new HashSet<>();
flattened.add(context());
parents().forEach(p -> flattened.addAll(p.flattened()));
this.flattened = ImmutableSet.copyOf(flattened);
}
public ResourceLocation context() {
return context;
}
public Set<BindContext> parents() {
return parents;
}
public Set<ResourceLocation> flattened() {
return flattened;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (BindContext) obj;
return Objects.equals(this.context, that.context) &&
Objects.equals(this.parents, that.parents);
}
@Override
public int hashCode() {
return Objects.hash(context, parents);
}
@Override
public String toString() {
return "BindContext[" +
"context=" + context + ", " +
"parents=" + parents + ']';
}
public static Set<ResourceLocation> flatten(Set<BindContext> contexts) {
return contexts.stream()
.flatMap(ctx -> ctx.flattened.stream())
.collect(Collectors.toSet());
}
}

View File

@ -0,0 +1,18 @@
package dev.isxander.controlify.bindings;
import dev.isxander.controlify.Controlify;
import java.util.Set;
public final class BindContexts {
public static final BindContext
INGAME = ctx("ingame"),
GUI = ctx("gui"),
GUI_VMOUSE = ctx("gui_vmouse"),
CONTROLIFY_CONFIG = ctx("controlify_config", GUI),
INVENTORY = ctx("inventory", GUI_VMOUSE);
private static BindContext ctx(String path, BindContext... parents) {
return new BindContext(Controlify.id(path), Set.of(parents));
}
}

View File

@ -1,5 +1,6 @@
package dev.isxander.controlify.bindings;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonObject;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.isxander.controlify.Controlify;
@ -36,11 +37,12 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
private BindRenderer renderer;
private final ResourceLocation id;
private final Component name, description, category;
private final Set<BindContext> contexts;
private final KeyMappingOverride override;
private static final Map<Controller<?, ?>, Set<IBind<?>>> pressedBinds = new HashMap<>();
private ControllerBindingImpl(Controller<T, ?> controller, IBind<T> defaultBind, ResourceLocation id, KeyMappingOverride vanillaOverride, Component name, Component description, Component category) {
private ControllerBindingImpl(Controller<T, ?> controller, IBind<T> defaultBind, ResourceLocation id, KeyMappingOverride vanillaOverride, Component name, Component description, Component category, Set<BindContext> contexts) {
this.controller = controller;
this.bind = this.defaultBind = defaultBind;
this.renderer = new BindRendererImpl(bind);
@ -49,6 +51,7 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
this.name = name;
this.description = description;
this.category = category;
this.contexts = ImmutableSet.copyOf(contexts);
}
@Override
@ -133,6 +136,16 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
return category;
}
@Override
public Set<BindContext> contexts() {
return contexts;
}
@Override
public IBind<T> getBind() {
return bind;
}
@Override
public boolean isUnbound() {
return bind instanceof EmptyBind;
@ -154,7 +167,7 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
}
@Override
public Option<?> generateYACLOption() {
public Option.Builder<?> startYACLOption() {
Option.Builder<IBind<T>> option = Option.createBuilder((Class<IBind<T>>) (Class<?>) IBind.class)
.name(name())
.binding(defaultBind(), this::currentBind, this::setCurrentBind)
@ -166,7 +179,7 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
((Option.Builder<IBind<JoystickState>>) (Object) option).controller(opt -> new JoystickBindController(opt, joystick));
}
return option.build();
return option;
}
// FIXME: very hack solution please remove me
@ -197,6 +210,7 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
private ResourceLocation id;
private Component name = null, description = null, category = null;
private KeyMappingOverride override = null;
private final Set<BindContext> contexts = new HashSet<>();
public ControllerBindingBuilderImpl(Controller<T, ?> controller) {
this.controller = controller;
@ -248,6 +262,12 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
return this;
}
@Override
public ControllerBindingBuilder<T> context(BindContext... contexts) {
this.contexts.addAll(Set.of(contexts));
return this;
}
@Override
public ControllerBindingBuilder<T> vanillaOverride(KeyMapping keyMapping, BooleanSupplier toggleable) {
this.override = new KeyMappingOverride(keyMapping, toggleable);
@ -276,7 +296,7 @@ public class ControllerBindingImpl<T extends ControllerState> implements Control
}
}
return new ControllerBindingImpl<>(controller, bind, id, override, name, description, category);
return new ControllerBindingImpl<>(controller, bind, id, override, name, description, category, contexts);
}
}

View File

@ -78,47 +78,56 @@ public class ControllerBindings<T extends ControllerState> {
.identifier("controlify", "walk_forward")
.defaultBind(GamepadBinds.LEFT_STICK_FORWARD)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(WALK_BACKWARD = ControllerBindingBuilder.create(controller)
.identifier("controlify", "walk_backward")
.defaultBind(GamepadBinds.LEFT_STICK_BACKWARD)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(WALK_LEFT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "strafe_left")
.defaultBind(GamepadBinds.LEFT_STICK_LEFT)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(WALK_RIGHT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "strafe_right")
.defaultBind(GamepadBinds.LEFT_STICK_RIGHT)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(LOOK_UP = ControllerBindingBuilder.create(controller)
.identifier("controlify", "look_up")
.defaultBind(GamepadBinds.RIGHT_STICK_FORWARD)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(LOOK_DOWN = ControllerBindingBuilder.create(controller)
.identifier("controlify", "look_down")
.defaultBind(GamepadBinds.RIGHT_STICK_BACKWARD)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(LOOK_LEFT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "look_left")
.defaultBind(GamepadBinds.RIGHT_STICK_LEFT)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(LOOK_RIGHT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "look_right")
.defaultBind(GamepadBinds.RIGHT_STICK_RIGHT)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
if (controller instanceof GamepadController gamepad && gamepad.hasGyro()) {
register(GAMEPAD_GYRO_BUTTON = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gamepad_gyro_button")
.defaultBind(new EmptyBind<>())
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
} else {
GAMEPAD_GYRO_BUTTON = null;
@ -127,209 +136,249 @@ public class ControllerBindings<T extends ControllerState> {
.identifier("controlify", "jump")
.defaultBind(GamepadBinds.A_BUTTON)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(SPRINT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "sprint")
.defaultBind(GamepadBinds.LEFT_STICK_PRESS)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keySprint, () -> controller.config().toggleSprint)
.build());
register(SNEAK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "sneak")
.defaultBind(GamepadBinds.RIGHT_STICK_PRESS)
.category(MOVEMENT_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(ATTACK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "attack")
.defaultBind(GamepadBinds.RIGHT_TRIGGER)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyAttack, () -> false)
.build());
register(USE = ControllerBindingBuilder.create(controller)
.identifier("controlify", "use")
.defaultBind(GamepadBinds.LEFT_TRIGGER)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyUse, () -> false)
.build());
register(DROP = ControllerBindingBuilder.create(controller)
.identifier("controlify", "drop")
.defaultBind(GamepadBinds.DPAD_DOWN)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME, BindContexts.INVENTORY)
.build());
register(NEXT_SLOT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "next_slot")
.defaultBind(GamepadBinds.RIGHT_BUMPER)
.category(INVENTORY_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(PREV_SLOT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "prev_slot")
.defaultBind(GamepadBinds.LEFT_BUMPER)
.category(INVENTORY_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(PAUSE = ControllerBindingBuilder.create(controller)
.identifier("controlify", "pause")
.defaultBind(GamepadBinds.START)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(INVENTORY = ControllerBindingBuilder.create(controller)
.identifier("controlify", "inventory")
.defaultBind(GamepadBinds.Y_BUTTON)
.category(INVENTORY_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyInventory, () -> false)
.build());
register(CHANGE_PERSPECTIVE = ControllerBindingBuilder.create(controller)
.identifier("controlify", "change_perspective")
.defaultBind(GamepadBinds.BACK)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyTogglePerspective, () -> false)
.build());
register(SWAP_HANDS = ControllerBindingBuilder.create(controller)
.identifier("controlify", "swap_hands")
.defaultBind(GamepadBinds.X_BUTTON)
.category(INVENTORY_CATEGORY)
.context(BindContexts.INGAME, BindContexts.INVENTORY)
.vanillaOverride(options.keySwapOffhand, () -> false)
.build());
register(OPEN_CHAT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "open_chat")
.defaultBind(GamepadBinds.DPAD_UP)
.category(MISC_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyChat, () -> false)
.build());
register(GUI_PRESS = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_press")
.defaultBind(GamepadBinds.A_BUTTON)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(GUI_BACK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_back")
.defaultBind(GamepadBinds.B_BUTTON)
.category(GUI_CATEGORY)
.context(BindContexts.GUI, BindContexts.GUI_VMOUSE)
.build());
register(GUI_NEXT_TAB = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_next_tab")
.defaultBind(GamepadBinds.RIGHT_BUMPER)
.category(GUI_CATEGORY)
.context(BindContexts.GUI, BindContexts.GUI_VMOUSE)
.build());
register(GUI_PREV_TAB = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_prev_tab")
.defaultBind(GamepadBinds.LEFT_BUMPER)
.category(GUI_CATEGORY)
.context(BindContexts.GUI, BindContexts.GUI_VMOUSE)
.build());
register(GUI_ABSTRACT_ACTION_1 = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_abstract_action_1")
.defaultBind(GamepadBinds.X_BUTTON)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(GUI_ABSTRACT_ACTION_2 = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_abstract_action_2")
.defaultBind(GamepadBinds.Y_BUTTON)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(PICK_BLOCK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "pick_block")
.defaultBind(GamepadBinds.DPAD_LEFT)
.category(GAMEPLAY_CATEGORY)
.context(BindContexts.INGAME)
.vanillaOverride(options.keyPickItem, () -> false)
.build());
register(TOGGLE_HUD_VISIBILITY = ControllerBindingBuilder.create(controller)
.identifier("controlify", "toggle_hud_visibility")
.defaultBind(new EmptyBind<>())
.category(MISC_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(SHOW_PLAYER_LIST = ControllerBindingBuilder.create(controller)
.identifier("controlify", "show_player_list")
.defaultBind(GamepadBinds.DPAD_RIGHT)
.category(MISC_CATEGORY)
.context(BindContexts.INGAME)
.build());
register(VMOUSE_MOVE_UP = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_move_up")
.defaultBind(GamepadBinds.LEFT_STICK_FORWARD)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_MOVE_DOWN = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_move_down")
.defaultBind(GamepadBinds.LEFT_STICK_BACKWARD)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_MOVE_LEFT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_move_left")
.defaultBind(GamepadBinds.LEFT_STICK_LEFT)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_MOVE_RIGHT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_move_right")
.defaultBind(GamepadBinds.LEFT_STICK_RIGHT)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_LCLICK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_lclick")
.defaultBind(GamepadBinds.A_BUTTON)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_RCLICK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_rclick")
.defaultBind(GamepadBinds.X_BUTTON)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_SHIFT_CLICK = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_shift_click")
.defaultBind(GamepadBinds.Y_BUTTON)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_SCROLL_UP = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_scroll_up")
.defaultBind(GamepadBinds.RIGHT_STICK_FORWARD)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_SCROLL_DOWN = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_scroll_down")
.defaultBind(GamepadBinds.RIGHT_STICK_BACKWARD)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_SHIFT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_shift")
.defaultBind(GamepadBinds.LEFT_STICK_PRESS)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(VMOUSE_TOGGLE = ControllerBindingBuilder.create(controller)
.identifier("controlify", "vmouse_toggle")
.defaultBind(GamepadBinds.BACK)
.category(VMOUSE_CATEGORY)
.context(BindContexts.GUI_VMOUSE)
.build());
register(GUI_NAVI_UP = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_navi_up")
.defaultBind(GamepadBinds.LEFT_STICK_FORWARD)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(GUI_NAVI_DOWN = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_navi_down")
.defaultBind(GamepadBinds.LEFT_STICK_BACKWARD)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(GUI_NAVI_LEFT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_navi_left")
.defaultBind(GamepadBinds.LEFT_STICK_LEFT)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(GUI_NAVI_RIGHT = ControllerBindingBuilder.create(controller)
.identifier("controlify", "gui_navi_right")
.defaultBind(GamepadBinds.LEFT_STICK_RIGHT)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(CYCLE_OPT_FORWARD = ControllerBindingBuilder.create(controller)
.identifier("controlify", "cycle_opt_forward")
.defaultBind(GamepadBinds.RIGHT_STICK_RIGHT)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(CYCLE_OPT_BACKWARD = ControllerBindingBuilder.create(controller)
.identifier("controlify", "cycle_opt_backward")
.defaultBind(GamepadBinds.RIGHT_STICK_LEFT)
.category(GUI_CATEGORY)
.context(BindContexts.GUI)
.build());
register(CLEAR_BINDING = ControllerBindingBuilder.create(controller)
.identifier("controlify", "clear_binding")
.defaultBind(GamepadBinds.RIGHT_STICK_PRESS)
.category(GUI_CATEGORY)
.context(BindContexts.CONTROLIFY_CONFIG)
.build());
for (var constructor : CUSTOM_BINDS.values()) {