mirror of
https://github.com/TeamMidnightDust/MidnightControls.git
synced 2025-12-16 16:45:09 +01:00
Use more enums instead of integers
This commit is contained in:
@@ -13,53 +13,45 @@ import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y;
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER;
|
||||
|
||||
public class AxisStorage {
|
||||
public int axis, state, asButtonState;
|
||||
public final int axis;
|
||||
public float value, absValue;
|
||||
public double deadZone;
|
||||
public Polarity polarity;
|
||||
public boolean isTrigger;
|
||||
public final double deadZone;
|
||||
public final Polarity polarity;
|
||||
public final boolean isTrigger;
|
||||
public final ButtonState buttonState;
|
||||
|
||||
// Only used for camera handling
|
||||
public AxisStorage(int axis, float value, int state) {
|
||||
this.axis = axis;
|
||||
this.value = value;
|
||||
this.state = state;
|
||||
this.deadZone = isLeftAxis() ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone;
|
||||
boolean currentPlusState = value > deadZone;
|
||||
boolean currentMinusState = value < -deadZone;
|
||||
this.polarity = currentPlusState ? AxisStorage.Polarity.PLUS : currentMinusState ? AxisStorage.Polarity.MINUS : AxisStorage.Polarity.ZERO;
|
||||
// Used for joysticks
|
||||
public AxisStorage(int axis, float value) {
|
||||
this(axis, value, isLeftAxis(axis) ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone);
|
||||
}
|
||||
|
||||
public AxisStorage(int axis, float value, float absValue, int state) {
|
||||
public AxisStorage(int axis, float value, double deadZone) {
|
||||
this.axis = axis;
|
||||
this.value = value;
|
||||
this.absValue = absValue;
|
||||
this.state = state;
|
||||
this.deadZone = isLeftAxis() ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone;
|
||||
this.asButtonState = value > .5f ? 1 : (value < -.5f ? 2 : 0);
|
||||
this.deadZone = deadZone;
|
||||
|
||||
if (axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER || axis == GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER
|
||||
|| axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER) || axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
|
||||
this.isTrigger = true;
|
||||
if (asButtonState == 2) {
|
||||
this.asButtonState = 0;
|
||||
if (value < -.5f) {
|
||||
value = 0f;
|
||||
}
|
||||
else {
|
||||
// Fixes Triggers not working correctly on some controllers
|
||||
if (MidnightControlsConfig.triggerFix) {
|
||||
this.value = 1.0f;
|
||||
this.absValue = 1.0f;
|
||||
this.state = 1;
|
||||
this.asButtonState = 1;
|
||||
value = 1.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else isTrigger = false;
|
||||
|
||||
this.value = value;
|
||||
this.buttonState = value > .5f ? ButtonState.PRESS : (value < -.5f ? ButtonState.RELEASE : ButtonState.NONE);
|
||||
this.absValue = Math.abs(value);
|
||||
boolean currentPlusState = value > deadZone;
|
||||
boolean currentMinusState = value < -deadZone;
|
||||
if (isTrigger) currentMinusState = false;
|
||||
else if (!MidnightControlsConfig.analogMovement && isLeftAxis()) {
|
||||
currentPlusState = asButtonState == 1;
|
||||
currentMinusState = asButtonState == 2;
|
||||
else if (!MidnightControlsConfig.analogMovement && isLeftAxis(axis)) {
|
||||
currentPlusState = buttonState == ButtonState.PRESS;
|
||||
currentMinusState = buttonState == ButtonState.RELEASE;
|
||||
}
|
||||
this.polarity = currentPlusState ? AxisStorage.Polarity.PLUS : currentMinusState ? AxisStorage.Polarity.MINUS : AxisStorage.Polarity.ZERO;
|
||||
}
|
||||
@@ -92,15 +84,20 @@ public class AxisStorage {
|
||||
}
|
||||
}
|
||||
}
|
||||
public boolean isLeftAxis() {
|
||||
public static boolean isLeftAxis(int axis) {
|
||||
return axis == GLFW_GAMEPAD_AXIS_LEFT_X || axis == GLFW_GAMEPAD_AXIS_LEFT_Y || axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER;
|
||||
}
|
||||
public boolean isRightAxis() {
|
||||
return !isLeftAxis();
|
||||
public static boolean isRightAxis(int axis) {
|
||||
return !isLeftAxis(axis);
|
||||
}
|
||||
|
||||
public enum Polarity {
|
||||
MINUS, ZERO, PLUS;
|
||||
MINUS(-1), ZERO(0), PLUS(1);
|
||||
|
||||
public final int multiplier;
|
||||
Polarity(int multiplier) {
|
||||
this.multiplier = multiplier;
|
||||
}
|
||||
public boolean isPositive() {
|
||||
return this == PLUS;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package eu.midnightdust.midnightcontrols.client.util.storage;
|
||||
|
||||
import eu.midnightdust.midnightcontrols.client.enums.ButtonState;
|
||||
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT;
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_BUTTON_DPAD_UP;
|
||||
|
||||
public class ButtonStorage {
|
||||
public final int button, action;
|
||||
public final boolean state;
|
||||
public final int button;
|
||||
public final ButtonState state;
|
||||
|
||||
public ButtonStorage(int button, int action, boolean state) {
|
||||
public ButtonStorage(int button, ButtonState state) {
|
||||
this.button = button;
|
||||
this.action = action;
|
||||
this.state = state;
|
||||
}
|
||||
public boolean isDpad() {
|
||||
|
||||
Reference in New Issue
Block a user