forked from Clones/Controlify
in-game input and start of keybind system
This commit is contained in:
37
src/main/java/dev/isxander/controlify/bindings/Bind.java
Normal file
37
src/main/java/dev/isxander/controlify/bindings/Bind.java
Normal file
@ -0,0 +1,37 @@
|
||||
package dev.isxander.controlify.bindings;
|
||||
|
||||
import dev.isxander.controlify.controller.ControllerState;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Bind {
|
||||
boolean state(ControllerState controllerState);
|
||||
|
||||
Bind A_BUTTON = state -> state.buttons().a();
|
||||
Bind B_BUTTON = state -> state.buttons().b();
|
||||
Bind X_BUTTON = state -> state.buttons().x();
|
||||
Bind Y_BUTTON = state -> state.buttons().y();
|
||||
Bind LEFT_BUMPER = state -> state.buttons().leftBumper();
|
||||
Bind RIGHT_BUMPER = state -> state.buttons().rightBumper();
|
||||
Bind LEFT_STICK = state -> state.buttons().leftStick();
|
||||
Bind RIGHT_STICK = state -> state.buttons().rightStick();
|
||||
Bind START = state -> state.buttons().start();
|
||||
Bind BACK = state -> state.buttons().back();
|
||||
Bind LEFT_TRIGGER = leftTrigger(0.5f);
|
||||
Bind RIGHT_TRIGGER = rightTrigger(0.5f);
|
||||
|
||||
Bind[] ALL = {
|
||||
A_BUTTON, B_BUTTON, X_BUTTON, Y_BUTTON,
|
||||
LEFT_BUMPER, RIGHT_BUMPER,
|
||||
LEFT_STICK, RIGHT_STICK,
|
||||
START, BACK,
|
||||
LEFT_TRIGGER, RIGHT_TRIGGER
|
||||
};
|
||||
|
||||
static Bind leftTrigger(float threshold) {
|
||||
return state -> state.axes().leftTrigger() > threshold;
|
||||
}
|
||||
|
||||
static Bind rightTrigger(float threshold) {
|
||||
return state -> state.axes().rightTrigger() > threshold;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package dev.isxander.controlify.bindings;
|
||||
|
||||
import dev.isxander.controlify.controller.Controller;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
public class ControllerBinding {
|
||||
private final Controller controller;
|
||||
private final Bind bind;
|
||||
private final Component name, description;
|
||||
|
||||
public ControllerBinding(Controller controller, Bind defaultBind, String id, Component description) {
|
||||
this.controller = controller;
|
||||
this.bind = defaultBind;
|
||||
this.name = Component.translatable("controlify.binding." + id);
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public ControllerBinding(Controller controller, Bind defaultBind, String id) {
|
||||
this(controller, defaultBind, id, Component.empty());
|
||||
}
|
||||
|
||||
public boolean held() {
|
||||
return bind.state(controller.state());
|
||||
}
|
||||
|
||||
public boolean justPressed() {
|
||||
return held() && !bind.state(controller.prevState());
|
||||
}
|
||||
|
||||
public boolean justReleased() {
|
||||
return !held() && bind.state(controller.prevState());
|
||||
}
|
||||
|
||||
public Component name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Component description() {
|
||||
return description;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package dev.isxander.controlify.bindings;
|
||||
|
||||
import dev.isxander.controlify.controller.Controller;
|
||||
|
||||
public class ControllerBindings {
|
||||
public final ControllerBinding JUMP, SNEAK, ATTACK, USE, SPRINT, NEXT_SLOT, PREV_SLOT;
|
||||
public final ControllerBinding[] ALL;
|
||||
|
||||
public ControllerBindings(Controller controller) {
|
||||
JUMP = new ControllerBinding(controller, Bind.A_BUTTON, "jump");
|
||||
SNEAK = new ControllerBinding(controller, Bind.RIGHT_STICK, "sneak");
|
||||
ATTACK = new ControllerBinding(controller, Bind.RIGHT_TRIGGER, "attack");
|
||||
USE = new ControllerBinding(controller, Bind.LEFT_TRIGGER, "use");
|
||||
SPRINT = new ControllerBinding(controller, Bind.LEFT_STICK, "sprint");
|
||||
NEXT_SLOT = new ControllerBinding(controller, Bind.RIGHT_BUMPER, "next_slot");
|
||||
PREV_SLOT = new ControllerBinding(controller, Bind.LEFT_BUMPER, "prev_slot");
|
||||
|
||||
ALL = new ControllerBinding[] {
|
||||
JUMP, SNEAK, ATTACK, USE, SPRINT, NEXT_SLOT, PREV_SLOT
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user