1
0
forked from Clones/Controlify

refactor button guide, bindings and controller identification

This commit is contained in:
isXander
2023-02-19 21:56:14 +00:00
parent 23c048f401
commit 0f719c0f45
21 changed files with 334 additions and 220 deletions

View File

@ -38,11 +38,12 @@ public class ControllerBinding<T extends ControllerState> {
this(controller, defaultBind, id, null, () -> false);
}
public ControllerBinding(Controller<T, ?> controller, GamepadBind defaultBind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride) {
this(controller, controller instanceof GamepadController ? (IBind<T>) defaultBind : new EmptyBind<>(), id, override, toggleOverride);
@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);
}
public ControllerBinding(Controller<T, ?> controller, GamepadBind defaultBind, ResourceLocation id) {
public ControllerBinding(Controller<T, ?> controller, GamepadBinds defaultBind, ResourceLocation id) {
this(controller, defaultBind, id, null, () -> false);
}
@ -55,11 +56,11 @@ public class ControllerBinding<T extends ControllerState> {
}
public boolean held() {
return bind.held(controller.state(), controller);
return bind.held(controller.state());
}
public boolean prevHeld() {
return bind.held(controller.prevState(), controller);
return bind.held(controller.prevState());
}
public boolean justPressed() {
@ -108,6 +109,10 @@ public class ControllerBinding<T extends ControllerState> {
return description;
}
public boolean unbound() {
return bind instanceof EmptyBind;
}
public KeyMappingOverride override() {
return override;
}

View File

@ -48,52 +48,52 @@ public class ControllerBindings<T extends ControllerState> {
this.controller = controller;
var options = Minecraft.getInstance().options;
register(WALK_FORWARD = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "walk_forward")));
register(WALK_BACKWARD = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "walk_backward")));
register(WALK_LEFT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_LEFT, new ResourceLocation("controlify", "strafe_left")));
register(WALK_RIGHT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "strafe_right")));
register(LOOK_UP = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_FORWARD, new ResourceLocation("controlify", "look_up")));
register(LOOK_DOWN = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_BACKWARD, new ResourceLocation("controlify", "look_down")));
register(LOOK_LEFT = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_LEFT, new ResourceLocation("controlify", "look_left")));
register(LOOK_RIGHT = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_RIGHT, new ResourceLocation("controlify", "look_right")));
register(JUMP = new ControllerBinding<>(controller, GamepadBind.A_BUTTON, new ResourceLocation("controlify", "jump"), options.keyJump, () -> false));
register(SNEAK = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_PRESS, new ResourceLocation("controlify", "sneak"), options.keyShift, () -> controller.config().toggleSneak));
register(ATTACK = new ControllerBinding<>(controller, GamepadBind.RIGHT_TRIGGER, new ResourceLocation("controlify", "attack"), options.keyAttack, () -> false));
register(USE = new ControllerBinding<>(controller, GamepadBind.LEFT_TRIGGER, new ResourceLocation("controlify", "use"), options.keyUse, () -> false));
register(SPRINT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_PRESS, new ResourceLocation("controlify", "sprint"), options.keySprint, () -> controller.config().toggleSprint));
register(DROP = new ControllerBinding<>(controller, GamepadBind.DPAD_DOWN, new ResourceLocation("controlify", "drop"), options.keyDrop, () -> false));
register(NEXT_SLOT = new ControllerBinding<>(controller, GamepadBind.RIGHT_BUMPER, new ResourceLocation("controlify", "next_slot")));
register(PREV_SLOT = new ControllerBinding<>(controller, GamepadBind.LEFT_BUMPER, new ResourceLocation("controlify", "prev_slot")));
register(PAUSE = new ControllerBinding<>(controller, GamepadBind.START, new ResourceLocation("controlify", "pause")));
register(INVENTORY = new ControllerBinding<>(controller, GamepadBind.Y_BUTTON, new ResourceLocation("controlify", "inventory"), options.keyInventory, () -> false));
register(CHANGE_PERSPECTIVE = new ControllerBinding<>(controller, GamepadBind.BACK, new ResourceLocation("controlify", "change_perspective"), options.keyTogglePerspective, () -> false));
register(SWAP_HANDS = new ControllerBinding<>(controller, GamepadBind.X_BUTTON, new ResourceLocation("controlify", "swap_hands"), options.keySwapOffhand, () -> false));
register(OPEN_CHAT = new ControllerBinding<>(controller, GamepadBind.DPAD_UP, new ResourceLocation("controlify", "open_chat"), options.keyChat, () -> false));
register(GUI_PRESS = new ControllerBinding<>(controller, GamepadBind.A_BUTTON, new ResourceLocation("controlify", "gui_press")));
register(GUI_BACK = new ControllerBinding<>(controller, GamepadBind.B_BUTTON, new ResourceLocation("controlify", "gui_back")));
register(GUI_NEXT_TAB = new ControllerBinding<>(controller, GamepadBind.RIGHT_BUMPER, new ResourceLocation("controlify", "gui_next_tab")));
register(GUI_PREV_TAB = new ControllerBinding<>(controller, GamepadBind.LEFT_BUMPER, new ResourceLocation("controlify", "gui_prev_tab")));
register(PICK_BLOCK = new ControllerBinding<>(controller, GamepadBind.DPAD_LEFT, new ResourceLocation("controlify", "pick_block"), options.keyPickItem, () -> false));
register(WALK_FORWARD = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "walk_forward")));
register(WALK_BACKWARD = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "walk_backward")));
register(WALK_LEFT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_LEFT, new ResourceLocation("controlify", "strafe_left")));
register(WALK_RIGHT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "strafe_right")));
register(LOOK_UP = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_FORWARD, new ResourceLocation("controlify", "look_up")));
register(LOOK_DOWN = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_BACKWARD, new ResourceLocation("controlify", "look_down")));
register(LOOK_LEFT = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_LEFT, new ResourceLocation("controlify", "look_left")));
register(LOOK_RIGHT = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_RIGHT, new ResourceLocation("controlify", "look_right")));
register(JUMP = new ControllerBinding<>(controller, GamepadBinds.A_BUTTON, new ResourceLocation("controlify", "jump"), options.keyJump, () -> false));
register(SNEAK = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_PRESS, new ResourceLocation("controlify", "sneak"), options.keyShift, () -> controller.config().toggleSneak));
register(ATTACK = new ControllerBinding<>(controller, GamepadBinds.RIGHT_TRIGGER, new ResourceLocation("controlify", "attack"), options.keyAttack, () -> false));
register(USE = new ControllerBinding<>(controller, GamepadBinds.LEFT_TRIGGER, new ResourceLocation("controlify", "use"), options.keyUse, () -> false));
register(SPRINT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_PRESS, new ResourceLocation("controlify", "sprint"), options.keySprint, () -> controller.config().toggleSprint));
register(DROP = new ControllerBinding<>(controller, GamepadBinds.DPAD_DOWN, new ResourceLocation("controlify", "drop"), options.keyDrop, () -> false));
register(NEXT_SLOT = new ControllerBinding<>(controller, GamepadBinds.RIGHT_BUMPER, new ResourceLocation("controlify", "next_slot")));
register(PREV_SLOT = new ControllerBinding<>(controller, GamepadBinds.LEFT_BUMPER, new ResourceLocation("controlify", "prev_slot")));
register(PAUSE = new ControllerBinding<>(controller, GamepadBinds.START, new ResourceLocation("controlify", "pause")));
register(INVENTORY = new ControllerBinding<>(controller, GamepadBinds.Y_BUTTON, new ResourceLocation("controlify", "inventory"), options.keyInventory, () -> false));
register(CHANGE_PERSPECTIVE = new ControllerBinding<>(controller, GamepadBinds.BACK, new ResourceLocation("controlify", "change_perspective"), options.keyTogglePerspective, () -> false));
register(SWAP_HANDS = new ControllerBinding<>(controller, GamepadBinds.X_BUTTON, new ResourceLocation("controlify", "swap_hands"), options.keySwapOffhand, () -> false));
register(OPEN_CHAT = new ControllerBinding<>(controller, GamepadBinds.DPAD_UP, new ResourceLocation("controlify", "open_chat"), options.keyChat, () -> false));
register(GUI_PRESS = new ControllerBinding<>(controller, GamepadBinds.A_BUTTON, new ResourceLocation("controlify", "gui_press")));
register(GUI_BACK = new ControllerBinding<>(controller, GamepadBinds.B_BUTTON, new ResourceLocation("controlify", "gui_back")));
register(GUI_NEXT_TAB = new ControllerBinding<>(controller, GamepadBinds.RIGHT_BUMPER, new ResourceLocation("controlify", "gui_next_tab")));
register(GUI_PREV_TAB = new ControllerBinding<>(controller, GamepadBinds.LEFT_BUMPER, new ResourceLocation("controlify", "gui_prev_tab")));
register(PICK_BLOCK = new ControllerBinding<>(controller, GamepadBinds.DPAD_LEFT, new ResourceLocation("controlify", "pick_block"), options.keyPickItem, () -> false));
register(TOGGLE_HUD_VISIBILITY = new ControllerBinding<>(controller, new EmptyBind<>(), new ResourceLocation("controlify", "toggle_hud_visibility")));
register(SHOW_PLAYER_LIST = new ControllerBinding<>(controller, GamepadBind.DPAD_RIGHT, new ResourceLocation("controlify", "show_player_list"), options.keyPlayerList, () -> false));
register(VMOUSE_MOVE_UP = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "vmouse_move_up")));
register(VMOUSE_MOVE_DOWN = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "vmouse_move_down")));
register(VMOUSE_MOVE_LEFT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_LEFT, new ResourceLocation("controlify", "vmouse_move_left")));
register(VMOUSE_MOVE_RIGHT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "vmouse_move_right")));
register(VMOUSE_LCLICK = new ControllerBinding<>(controller, GamepadBind.A_BUTTON, new ResourceLocation("controlify", "vmouse_lclick")));
register(VMOUSE_RCLICK = new ControllerBinding<>(controller, GamepadBind.X_BUTTON, new ResourceLocation("controlify", "vmouse_rclick")));
register(VMOUSE_SHIFT_CLICK = new ControllerBinding<>(controller, GamepadBind.Y_BUTTON, new ResourceLocation("controlify", "vmouse_shift_click")));
register(VMOUSE_SCROLL_UP = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_FORWARD, new ResourceLocation("controlify", "vmouse_scroll_up")));
register(VMOUSE_SCROLL_DOWN = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_BACKWARD, new ResourceLocation("controlify", "vmouse_scroll_down")));
register(VMOUSE_ESCAPE = new ControllerBinding<>(controller, GamepadBind.B_BUTTON, new ResourceLocation("controlify", "vmouse_escape")));
register(VMOUSE_SHIFT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_PRESS, new ResourceLocation("controlify", "vmouse_shift")));
register(VMOUSE_TOGGLE = new ControllerBinding<>(controller, GamepadBind.BACK, new ResourceLocation("controlify", "vmouse_toggle")));
register(GUI_NAVI_UP = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "gui_navi_up")));
register(GUI_NAVI_DOWN = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "gui_navi_down")));
register(GUI_NAVI_LEFT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_LEFT, new ResourceLocation("controlify", "gui_navi_left")));
register(GUI_NAVI_RIGHT = new ControllerBinding<>(controller, GamepadBind.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "gui_navi_right")));
register(YACL_CYCLE_OPT_FORWARD = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_RIGHT, new ResourceLocation("controlify", "yacl_cycle_opt_forward")));
register(YACL_CYCLE_OPT_BACKWARD = new ControllerBinding<>(controller, GamepadBind.RIGHT_STICK_LEFT, new ResourceLocation("controlify", "yacl_cycle_opt_backward")));
register(SHOW_PLAYER_LIST = new ControllerBinding<>(controller, GamepadBinds.DPAD_RIGHT, new ResourceLocation("controlify", "show_player_list"), options.keyPlayerList, () -> false));
register(VMOUSE_MOVE_UP = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "vmouse_move_up")));
register(VMOUSE_MOVE_DOWN = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "vmouse_move_down")));
register(VMOUSE_MOVE_LEFT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_LEFT, new ResourceLocation("controlify", "vmouse_move_left")));
register(VMOUSE_MOVE_RIGHT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "vmouse_move_right")));
register(VMOUSE_LCLICK = new ControllerBinding<>(controller, GamepadBinds.A_BUTTON, new ResourceLocation("controlify", "vmouse_lclick")));
register(VMOUSE_RCLICK = new ControllerBinding<>(controller, GamepadBinds.X_BUTTON, new ResourceLocation("controlify", "vmouse_rclick")));
register(VMOUSE_SHIFT_CLICK = new ControllerBinding<>(controller, GamepadBinds.Y_BUTTON, new ResourceLocation("controlify", "vmouse_shift_click")));
register(VMOUSE_SCROLL_UP = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_FORWARD, new ResourceLocation("controlify", "vmouse_scroll_up")));
register(VMOUSE_SCROLL_DOWN = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_BACKWARD, new ResourceLocation("controlify", "vmouse_scroll_down")));
register(VMOUSE_ESCAPE = new ControllerBinding<>(controller, GamepadBinds.B_BUTTON, new ResourceLocation("controlify", "vmouse_escape")));
register(VMOUSE_SHIFT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_PRESS, new ResourceLocation("controlify", "vmouse_shift")));
register(VMOUSE_TOGGLE = new ControllerBinding<>(controller, GamepadBinds.BACK, new ResourceLocation("controlify", "vmouse_toggle")));
register(GUI_NAVI_UP = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_FORWARD, new ResourceLocation("controlify", "gui_navi_up")));
register(GUI_NAVI_DOWN = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_BACKWARD, new ResourceLocation("controlify", "gui_navi_down")));
register(GUI_NAVI_LEFT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_LEFT, new ResourceLocation("controlify", "gui_navi_left")));
register(GUI_NAVI_RIGHT = new ControllerBinding<>(controller, GamepadBinds.LEFT_STICK_RIGHT, new ResourceLocation("controlify", "gui_navi_right")));
register(YACL_CYCLE_OPT_FORWARD = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_RIGHT, new ResourceLocation("controlify", "yacl_cycle_opt_forward")));
register(YACL_CYCLE_OPT_BACKWARD = new ControllerBinding<>(controller, GamepadBinds.RIGHT_STICK_LEFT, new ResourceLocation("controlify", "yacl_cycle_opt_backward")));
ControlifyEvents.CONTROLLER_BIND_REGISTRY.invoker().onRegisterControllerBinds(this, controller);

View File

@ -15,7 +15,12 @@ public class EmptyBind<T extends ControllerState> implements IBind<T> {
}
@Override
public void draw(PoseStack matrices, int x, int centerY, Controller<T, ?> controller) {
public boolean held(T state) {
return false;
}
@Override
public void draw(PoseStack matrices, int x, int centerY) {
}
@ -31,6 +36,11 @@ public class EmptyBind<T extends ControllerState> implements IBind<T> {
return object;
}
@Override
public Controller<T, ?> controller() {
return null;
}
@Override
public boolean equals(Object obj) {
return obj instanceof EmptyBind;

View File

@ -4,78 +4,37 @@ import com.google.gson.JsonObject;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.gamepad.GamepadConfig;
import dev.isxander.controlify.controller.gamepad.BuiltinGamepadTheme;
import dev.isxander.controlify.controller.gamepad.GamepadConfig;
import dev.isxander.controlify.controller.gamepad.GamepadController;
import dev.isxander.controlify.controller.gamepad.GamepadState;
import dev.isxander.controlify.gui.DrawSize;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.resources.ResourceLocation;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public enum GamepadBind implements IBind<GamepadState> {
A_BUTTON(state -> state.gamepadButtons().a(), "a_button"),
B_BUTTON(state -> state.gamepadButtons().b(), "b_button"),
X_BUTTON(state -> state.gamepadButtons().x(), "x_button"),
Y_BUTTON(state -> state.gamepadButtons().y(), "y_button"),
LEFT_BUMPER(state -> state.gamepadButtons().leftBumper(), "left_bumper"),
RIGHT_BUMPER(state -> state.gamepadButtons().rightBumper(), "right_bumper"),
LEFT_STICK_PRESS(state -> state.gamepadButtons().leftStick(), "left_stick_press"),
RIGHT_STICK_PRESS(state -> state.gamepadButtons().rightStick(), "right_stick_press"),
START(state -> state.gamepadButtons().start(), "start"),
BACK(state -> state.gamepadButtons().back(), "back"),
GUIDE(state -> state.gamepadButtons().guide(), "guide"), // the middle button
DPAD_UP(state -> state.gamepadButtons().dpadUp(), "dpad_up"),
DPAD_DOWN(state -> state.gamepadButtons().dpadDown(), "dpad_down"),
DPAD_LEFT(state -> state.gamepadButtons().dpadLeft(), "dpad_left"),
DPAD_RIGHT(state -> state.gamepadButtons().dpadRight(), "dpad_right"),
LEFT_TRIGGER(state -> state.gamepadAxes().leftTrigger(), "left_trigger", true),
RIGHT_TRIGGER(state -> state.gamepadAxes().rightTrigger(), "right_trigger", true),
LEFT_STICK_FORWARD(state -> -Math.min(0, state.gamepadAxes().leftStickY()), "left_stick_up", true),
LEFT_STICK_BACKWARD(state -> Math.max(0, state.gamepadAxes().leftStickY()), "left_stick_down", true),
LEFT_STICK_LEFT(state -> -Math.min(0, state.gamepadAxes().leftStickX()), "left_stick_left", true),
LEFT_STICK_RIGHT(state -> Math.max(0, state.gamepadAxes().leftStickX()), "left_stick_right", true),
RIGHT_STICK_FORWARD(state -> -Math.min(0, state.gamepadAxes().rightStickY()), "right_stick_up", true),
RIGHT_STICK_BACKWARD(state -> Math.max(0, state.gamepadAxes().rightStickY()), "right_stick_down", true),
RIGHT_STICK_LEFT(state -> -Math.min(0, state.gamepadAxes().rightStickX()), "right_stick_left", true),
RIGHT_STICK_RIGHT(state -> Math.max(0, state.gamepadAxes().rightStickX()), "right_stick_right", true);
public static final String BIND_ID = "gamepad";
private final Function<GamepadState, Float> state;
public class GamepadBind implements IBind<GamepadState> {
private final Function<GamepadState, Float> stateSupplier;
private final String identifier;
private final Map<BuiltinGamepadTheme, ResourceLocation> textureLocations;
private final GamepadController gamepad;
private final ResourceLocation defaultTexture;
GamepadBind(Function<GamepadState, Float> state, String identifier, boolean jvmIsBad) {
this.state = state;
public GamepadBind(Function<GamepadState, Float> stateSupplier, String identifier, GamepadController gamepad) {
this.stateSupplier = stateSupplier;
this.identifier = identifier;
this.textureLocations = new HashMap<>();
for (BuiltinGamepadTheme theme : BuiltinGamepadTheme.values()) {
if (theme == BuiltinGamepadTheme.DEFAULT) continue;
textureLocations.put(theme, new ResourceLocation("controlify", "textures/gui/gamepad_buttons/" + theme.id() + "/" + identifier + ".png"));
}
}
GamepadBind(Function<GamepadState, Boolean> state, String identifier) {
this(state1 -> state.apply(state1) ? 1f : 0f, identifier, true);
this.gamepad = gamepad;
this.defaultTexture = new ResourceLocation("controlify", "textures/gui/gamepad_buttons/" + gamepad.config().theme.id() + "/" + identifier + ".png");
}
@Override
public float state(GamepadState state) {
return this.state.apply(state);
return stateSupplier.apply(state);
}
@Override
public void draw(PoseStack matrices, int x, int centerY, Controller<GamepadState, ?> controller) {
ResourceLocation texture;
if (((GamepadConfig)controller.config()).theme == BuiltinGamepadTheme.DEFAULT) {
texture = new ResourceLocation("controlify", "textures/gui/gamepad/" + controller.type().identifier() + "/" + identifier + ".png");
} else {
texture = textureLocations.get(((GamepadConfig)controller.config()).theme);
}
public void draw(PoseStack matrices, int x, int centerY) {
ResourceLocation texture = getTexture(gamepad.config().theme);
RenderSystem.setShaderTexture(0, texture);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
@ -92,19 +51,22 @@ public enum GamepadBind implements IBind<GamepadState> {
return identifier;
}
@Override
public Controller<GamepadState, ?> controller() {
return this.gamepad;
}
@Override
public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("type", BIND_ID);
object.addProperty("type", GamepadBinds.BIND_ID);
object.addProperty("bind", identifier);
return object;
}
public static GamepadBind fromJson(JsonObject object) {
String name = object.get("bind").getAsString();
for (GamepadBind bind : values()) {
if (bind.identifier.equals(name)) return bind;
}
return null;
private ResourceLocation getTexture(BuiltinGamepadTheme theme) {
if (theme == BuiltinGamepadTheme.DEFAULT)
return defaultTexture;
return new ResourceLocation("controlify", "textures/gui/gamepad_buttons/" + theme.id() + "/" + identifier + ".png");
}
}

View File

@ -0,0 +1,62 @@
package dev.isxander.controlify.bindings;
import com.google.gson.JsonObject;
import dev.isxander.controlify.controller.gamepad.GamepadController;
import dev.isxander.controlify.controller.gamepad.GamepadState;
import java.util.Optional;
import java.util.function.Function;
public enum GamepadBinds {
A_BUTTON(state -> state.gamepadButtons().a(), "a_button"),
B_BUTTON(state -> state.gamepadButtons().b(), "b_button"),
X_BUTTON(state -> state.gamepadButtons().x(), "x_button"),
Y_BUTTON(state -> state.gamepadButtons().y(), "y_button"),
LEFT_BUMPER(state -> state.gamepadButtons().leftBumper(), "left_bumper"),
RIGHT_BUMPER(state -> state.gamepadButtons().rightBumper(), "right_bumper"),
LEFT_STICK_PRESS(state -> state.gamepadButtons().leftStick(), "left_stick_press"),
RIGHT_STICK_PRESS(state -> state.gamepadButtons().rightStick(), "right_stick_press"),
START(state -> state.gamepadButtons().start(), "start"),
BACK(state -> state.gamepadButtons().back(), "back"),
GUIDE(state -> state.gamepadButtons().guide(), "guide"), // the middle button
DPAD_UP(state -> state.gamepadButtons().dpadUp(), "dpad_up"),
DPAD_DOWN(state -> state.gamepadButtons().dpadDown(), "dpad_down"),
DPAD_LEFT(state -> state.gamepadButtons().dpadLeft(), "dpad_left"),
DPAD_RIGHT(state -> state.gamepadButtons().dpadRight(), "dpad_right"),
LEFT_TRIGGER(state -> state.gamepadAxes().leftTrigger(), "left_trigger", true),
RIGHT_TRIGGER(state -> state.gamepadAxes().rightTrigger(), "right_trigger", true),
LEFT_STICK_FORWARD(state -> -Math.min(0, state.gamepadAxes().leftStickY()), "left_stick_up", true),
LEFT_STICK_BACKWARD(state -> Math.max(0, state.gamepadAxes().leftStickY()), "left_stick_down", true),
LEFT_STICK_LEFT(state -> -Math.min(0, state.gamepadAxes().leftStickX()), "left_stick_left", true),
LEFT_STICK_RIGHT(state -> Math.max(0, state.gamepadAxes().leftStickX()), "left_stick_right", true),
RIGHT_STICK_FORWARD(state -> -Math.min(0, state.gamepadAxes().rightStickY()), "right_stick_up", true),
RIGHT_STICK_BACKWARD(state -> Math.max(0, state.gamepadAxes().rightStickY()), "right_stick_down", true),
RIGHT_STICK_LEFT(state -> -Math.min(0, state.gamepadAxes().rightStickX()), "right_stick_left", true),
RIGHT_STICK_RIGHT(state -> Math.max(0, state.gamepadAxes().rightStickX()), "right_stick_right", true);
public static final String BIND_ID = "gamepad";
private final Function<GamepadState, Float> state;
private final String identifier;
GamepadBinds(Function<GamepadState, Float> state, String identifier, boolean jvmIsBad) {
this.state = state;
this.identifier = identifier;
}
GamepadBinds(Function<GamepadState, Boolean> state, String identifier) {
this(state1 -> state.apply(state1) ? 1f : 0f, identifier, true);
}
public GamepadBind forGamepad(GamepadController gamepad) {
return new GamepadBind(state, identifier, gamepad);
}
public static Optional<GamepadBinds> fromJson(JsonObject object) {
String name = object.get("bind").getAsString();
for (GamepadBinds bind : values()) {
if (bind.identifier.equals(name)) return Optional.of(bind);
}
return Optional.empty();
}
}

View File

@ -10,23 +10,25 @@ import dev.isxander.controlify.gui.DrawSize;
public interface IBind<S extends ControllerState> {
float state(S state);
default boolean held(S state, Controller<S, ?> controller) {
return state(state) > controller.config().buttonActivationThreshold;
default boolean held(S state) {
return state(state) > controller().config().buttonActivationThreshold;
}
void draw(PoseStack matrices, int x, int centerY, Controller<S, ?> controller);
void draw(PoseStack matrices, int x, int centerY);
DrawSize drawSize();
JsonObject toJson();
Controller<S, ?> controller();
@SuppressWarnings("unchecked")
static <T extends ControllerState> IBind<T> fromJson(JsonObject json, Controller<T, ?> controller) {
var type = json.get("type").getAsString();
if (type.equals(EmptyBind.BIND_ID))
return new EmptyBind<>();
if (controller instanceof GamepadController && type.equals(GamepadBind.BIND_ID)) {
return (IBind<T>) GamepadBind.fromJson(json);
if (controller instanceof GamepadController gamepad && type.equals(GamepadBinds.BIND_ID)) {
return GamepadBinds.fromJson(json).map(bind -> (IBind<T>) bind.forGamepad(gamepad)).orElse(new EmptyBind<>());
} else if (controller instanceof JoystickController joystick) {
return (IBind<T>) switch (type) {
case JoystickButtonBind.BIND_ID -> JoystickButtonBind.fromJson(json, joystick);

View File

@ -39,8 +39,7 @@ public class JoystickAxisBind implements IBind<JoystickState> {
}
@Override
public void draw(PoseStack matrices, int x, int centerY, Controller<JoystickState, ?> controller) {
if (controller != joystick) return;
public void draw(PoseStack matrices, int x, int centerY) {
JoystickMapping mapping = joystick.mapping();
String type = joystick.type().identifier();
@ -77,6 +76,11 @@ public class JoystickAxisBind implements IBind<JoystickState> {
return object;
}
@Override
public Controller<JoystickState, ?> controller() {
return joystick;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -31,9 +31,7 @@ public class JoystickButtonBind implements IBind<JoystickState> {
}
@Override
public void draw(PoseStack matrices, int x, int centerY, Controller<JoystickState, ?> controller) {
if (controller != joystick) return;
public void draw(PoseStack matrices, int x, int centerY) {
String type = joystick.type().identifier();
String button = joystick.mapping().button(buttonIndex).identifier();
var texture = new ResourceLocation("controlify", "textures/gui/joystick/" + type + "/button_" + button + ".png");
@ -56,6 +54,11 @@ public class JoystickButtonBind implements IBind<JoystickState> {
return object;
}
@Override
public Controller<JoystickState, ?> controller() {
return joystick;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -33,9 +33,7 @@ public class JoystickHatBind implements IBind<JoystickState> {
}
@Override
public void draw(PoseStack matrices, int x, int centerY, Controller<JoystickState, ?> controller) {
if (controller != joystick) return;
public void draw(PoseStack matrices, int x, int centerY) {
String type = joystick.type().identifier();
String button = joystick.mapping().button(hatIndex).identifier();
String direction = "centered";
@ -69,6 +67,11 @@ public class JoystickHatBind implements IBind<JoystickState> {
return object;
}
@Override
public Controller<JoystickState, ?> controller() {
return joystick;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;