1
0
forked from Clones/Controlify

refactor binding api (sorry enjarai)

This commit is contained in:
isXander
2023-04-14 20:11:57 +01:00
parent 07b57d5637
commit abe6b27cef
16 changed files with 183 additions and 109 deletions

View File

@ -1,8 +0,0 @@
package dev.isxander.controlify.bindings;
import dev.isxander.controlify.controller.Controller;
@FunctionalInterface
public interface BindingSupplier {
ControllerBinding<?> get(Controller<?, ?> controller);
}

View File

@ -1,9 +1,15 @@
package dev.isxander.controlify.bindings;
import com.google.gson.JsonObject;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.bind.BindRenderer;
import dev.isxander.controlify.api.bind.ControllerBinding;
import dev.isxander.controlify.api.bind.ControllerBindingBuilder;
import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerState;
import dev.isxander.controlify.controller.gamepad.GamepadController;
import dev.isxander.controlify.gui.DrawSize;
import net.minecraft.client.KeyMapping;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
@ -17,19 +23,21 @@ import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
public class ControllerBinding<T extends ControllerState> {
public class ControllerBindingImpl<T extends ControllerState> implements ControllerBinding {
private final Controller<T, ?> controller;
private IBind<T> bind;
private final IBind<T> defaultBind;
private BindRenderer renderer;
private final ResourceLocation id;
private final Component name, description, category;
private final KeyMappingOverride override;
private static final Map<Controller<?, ?>, Set<IBind<?>>> pressedBinds = new HashMap<>();
private ControllerBinding(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) {
this.controller = controller;
this.bind = this.defaultBind = defaultBind;
this.renderer = new BindRendererImpl(bind);
this.id = id;
this.override = vanillaOverride;
this.name = name;
@ -37,50 +45,27 @@ public class ControllerBinding<T extends ControllerState> {
this.category = category;
}
@Deprecated
public ControllerBinding(Controller<T, ?> controller, IBind<T> defaultBind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
this.controller = controller;
this.bind = this.defaultBind = defaultBind;
this.id = id;
this.name = Component.translatable("controlify.binding." + id.getNamespace() + "." + id.getPath());
var descKey = "controlify.binding." + id.getNamespace() + "." + id.getPath() + ".desc";
this.description = Language.getInstance().has(descKey) ? Component.translatable(descKey) : Component.empty();
this.override = override != null ? new KeyMappingOverride(override, toggleOverride) : null;
this.category = null;
}
@Deprecated
public ControllerBinding(Controller<T, ?> controller, IBind<T> defaultBind, ResourceLocation id) {
this(controller, defaultBind, id, null, () -> false);
}
@Deprecated
@SuppressWarnings("unchecked")
public ControllerBinding(Controller<T, ?> controller, GamepadBinds defaultBind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
this(controller, controller instanceof GamepadController gamepad ? (IBind<T>) defaultBind.forGamepad(gamepad) : new EmptyBind<>(), id, override, toggleOverride);
}
@Deprecated
public ControllerBinding(Controller<T, ?> controller, GamepadBinds defaultBind, ResourceLocation id) {
this(controller, defaultBind, id, null, () -> false);
}
@Override
public float state() {
return bind.state(controller.state());
}
@Override
public float prevState() {
return bind.state(controller.prevState());
}
@Override
public boolean held() {
return bind.held(controller.state());
}
@Override
public boolean prevHeld() {
return bind.held(controller.prevState());
}
@Override
public boolean justPressed() {
if (hasBindPressed(this)) return false;
@ -92,6 +77,7 @@ public class ControllerBinding<T extends ControllerState> {
}
}
@Override
public boolean justReleased() {
if (hasBindPressed(this)) return false;
@ -109,36 +95,58 @@ public class ControllerBinding<T extends ControllerState> {
public void setCurrentBind(IBind<T> bind) {
this.bind = bind;
this.renderer = new BindRendererImpl(bind);
Controlify.instance().config().setDirty();
}
public IBind<T> defaultBind() {
return defaultBind;
}
@Override
public void resetBind() {
setCurrentBind(defaultBind());
}
public ResourceLocation id() {
return id;
}
@Override
public Component name() {
return name;
}
@Override
public Component description() {
return description;
}
@Override
public Component category() {
return category;
}
public boolean unbound() {
@Override
public boolean isUnbound() {
return bind instanceof EmptyBind;
}
@Override
public KeyMappingOverride override() {
return override;
}
@Override
public JsonObject toJson() {
return currentBind().toJson();
}
@Override
public BindRenderer renderer() {
return renderer;
}
// FIXME: very hack solution please remove me
public static void clearPressedBinds(Controller<?, ?> controller) {
@ -147,12 +155,12 @@ public class ControllerBinding<T extends ControllerState> {
}
}
private static boolean hasBindPressed(ControllerBinding<?> binding) {
private static boolean hasBindPressed(ControllerBindingImpl<?> binding) {
var pressed = pressedBinds.getOrDefault(binding.controller, Set.of());
return pressed.containsAll(getBinds(binding.bind));
}
private static void addPressedBind(ControllerBinding<?> binding) {
private static void addPressedBind(ControllerBindingImpl<?> binding) {
pressedBinds.computeIfAbsent(binding.controller, c -> new HashSet<>()).addAll(getBinds(binding.bind));
}
@ -160,9 +168,6 @@ public class ControllerBinding<T extends ControllerState> {
return Set.of(bind);
}
public record KeyMappingOverride(KeyMapping keyMapping, BooleanSupplier toggleable) {
}
@ApiStatus.Internal
public static final class ControllerBindingBuilderImpl<T extends ControllerState> implements ControllerBindingBuilder<T> {
private final Controller<T, ?> controller;
@ -233,7 +238,7 @@ public class ControllerBinding<T extends ControllerState> {
}
@Override
public ControllerBinding<T> build() {
public ControllerBinding build() {
Validate.notNull(id, "Identifier must be set");
Validate.notNull(bind, "Default bind must be set");
Validate.notNull(category, "Category must be set");
@ -249,7 +254,19 @@ public class ControllerBinding<T extends ControllerState> {
}
}
return new ControllerBinding<>(controller, bind, id, override, name, description, category);
return new ControllerBindingImpl<>(controller, bind, id, override, name, description, category);
}
}
private record BindRendererImpl(IBind<?> bind) implements BindRenderer {
@Override
public void render(PoseStack poseStack, int x, int centerY) {
bind.draw(poseStack, x, centerY);
}
@Override
public DrawSize size() {
return bind.drawSize();
}
}
}

View File

@ -4,6 +4,7 @@ import com.google.gson.JsonObject;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.InputMode;
import dev.isxander.controlify.api.bind.ControlifyBindingsApi;
import dev.isxander.controlify.api.bind.ControllerBinding;
import dev.isxander.controlify.api.bind.ControllerBindingBuilder;
import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerState;
@ -25,7 +26,7 @@ import java.util.function.Function;
import java.util.function.UnaryOperator;
public class ControllerBindings<T extends ControllerState> {
private static final Map<ResourceLocation, Function<ControllerBindings<?>, ControllerBinding<?>>> CUSTOM_BINDS = new LinkedHashMap<>();
private static final Map<ResourceLocation, Function<ControllerBindings<?>, ControllerBinding>> CUSTOM_BINDS = new LinkedHashMap<>();
private static final Set<KeyMapping> EXCLUDED_VANILLA_BINDS = new HashSet<>();
public static final Component MOVEMENT_CATEGORY = Component.translatable("key.categories.movement");
@ -36,7 +37,7 @@ public class ControllerBindings<T extends ControllerState> {
public static final Component GUI_CATEGORY = Component.translatable("controlify.binding_category.gui");
public static final Component MISC_CATEGORY = Component.translatable("key.categories.misc");
public final ControllerBinding<T>
public final ControllerBinding
WALK_FORWARD, WALK_BACKWARD, WALK_LEFT, WALK_RIGHT,
LOOK_UP, LOOK_DOWN, LOOK_LEFT, LOOK_RIGHT,
GAMEPAD_GYRO_BUTTON,
@ -64,7 +65,7 @@ public class ControllerBindings<T extends ControllerState> {
GUI_NAVI_UP, GUI_NAVI_DOWN, GUI_NAVI_LEFT, GUI_NAVI_RIGHT,
CYCLE_OPT_FORWARD, CYCLE_OPT_BACKWARD;
private final Map<ResourceLocation, ControllerBinding<T>> registry = new LinkedHashMap<>();
private final Map<ResourceLocation, ControllerBinding> registry = new LinkedHashMap<>();
private final Controller<T, ?> controller;
@ -333,7 +334,7 @@ public class ControllerBindings<T extends ControllerState> {
.build());
for (var constructor : CUSTOM_BINDS.values()) {
register((ControllerBinding<T>) constructor.apply(this));
register(constructor.apply(this));
}
registerModdedKeybinds();
@ -342,37 +343,44 @@ public class ControllerBindings<T extends ControllerState> {
ControlifyEvents.INPUT_MODE_CHANGED.register(mode -> KeyMapping.releaseAll());
}
public ControllerBinding<T> register(ControllerBinding<T> binding) {
public ControllerBinding register(ControllerBinding binding) {
registry.put(binding.id(), binding);
return binding;
}
private ControllerBinding<?> create(UnaryOperator<ControllerBindingBuilder<?>> builder) {
private ControllerBinding create(UnaryOperator<ControllerBindingBuilder<?>> builder) {
return builder.apply(ControllerBindingBuilder.create(controller)).build();
}
@Deprecated
private ControllerBinding<?> create(GamepadBinds bind, ResourceLocation id) {
return new ControllerBinding<>(controller, bind, id);
private ControllerBinding create(GamepadBinds bind, ResourceLocation id) {
return ControllerBindingBuilder.create(controller)
.identifier(id)
.defaultBind(bind)
.build();
}
@Deprecated
private ControllerBinding<?> create(GamepadBinds bind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
return new ControllerBinding<>(controller, bind, id, override, toggleOverride);
private ControllerBinding create(GamepadBinds bind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
return ControllerBindingBuilder.create(controller)
.identifier(id)
.defaultBind(bind)
.vanillaOverride(override, toggleOverride)
.build();
}
public ControllerBinding<T> get(ResourceLocation id) {
public ControllerBinding get(ResourceLocation id) {
return registry.get(id);
}
public Map<ResourceLocation, ControllerBinding<T>> registry() {
public Map<ResourceLocation, ControllerBinding> registry() {
return Collections.unmodifiableMap(registry);
}
public JsonObject toJson() {
JsonObject json = new JsonObject();
for (var binding : registry().values()) {
json.add(binding.id().toString(), binding.currentBind().toJson());
json.add(binding.id().toString(), binding.toJson());
}
return json;
}
@ -392,7 +400,7 @@ public class ControllerBindings<T extends ControllerState> {
clean = false;
continue;
}
binding.setCurrentBind(IBind.fromJson(bind, controller));
((ControllerBindingImpl<T>) binding).setCurrentBind(IBind.fromJson(bind, controller));
}
return clean;
@ -416,7 +424,7 @@ public class ControllerBindings<T extends ControllerState> {
toggleOverride = ((ToggleKeyMappingAccessor) toggleKeyMapping).getNeedsToggle();
}
ControllerBinding<T> binding = ControllerBindingBuilder.create(controller)
ControllerBinding binding = ControllerBindingBuilder.create(controller)
.identifier(identifier)
.defaultBind(new EmptyBind<>())
.name(Component.translatable(keyMapping.getName()))
@ -433,7 +441,7 @@ public class ControllerBindings<T extends ControllerState> {
}
private void imitateVanillaClick() {
ControllerBinding.clearPressedBinds(controller);
ControllerBindingImpl.clearPressedBinds(controller);
if (Controlify.instance().currentInputMode() != InputMode.CONTROLLER)
return;
@ -463,21 +471,21 @@ public class ControllerBindings<T extends ControllerState> {
public static final Api INSTANCE = new Api();
@Override
public BindingSupplier registerBind(ResourceLocation id, UnaryOperator<ControllerBindingBuilder<?>> builder) {
public dev.isxander.controlify.api.bind.BindingSupplier registerBind(ResourceLocation id, UnaryOperator<ControllerBindingBuilder<?>> builder) {
CUSTOM_BINDS.put(id, bindings -> bindings.create(b -> builder.apply(b).identifier(id)));
return controller -> controller.bindings().get(id);
}
@Deprecated
@Override
public BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id) {
public dev.isxander.controlify.api.bind.BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id) {
CUSTOM_BINDS.put(id, bindings -> bindings.create(bind, id));
return controller -> controller.bindings().get(id);
}
@Deprecated
@Override
public BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
public dev.isxander.controlify.api.bind.BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
CUSTOM_BINDS.put(id, bindings -> bindings.create(bind, id, override, toggleOverride));
return controller -> controller.bindings().get(id);
}