Use more enums instead of integers

This commit is contained in:
Martin Prokoph
2024-07-22 13:19:09 +02:00
parent f782e17349
commit 044bbfe9ed
5 changed files with 93 additions and 99 deletions

View File

@@ -20,6 +20,7 @@ import eu.midnightdust.midnightcontrols.MidnightControlsFeature;
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding; import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding;
import eu.midnightdust.midnightcontrols.client.controller.Controller; import eu.midnightdust.midnightcontrols.client.controller.Controller;
import eu.midnightdust.midnightcontrols.client.controller.InputManager; import eu.midnightdust.midnightcontrols.client.controller.InputManager;
import eu.midnightdust.midnightcontrols.client.enums.ButtonState;
import eu.midnightdust.midnightcontrols.client.enums.CameraMode; import eu.midnightdust.midnightcontrols.client.enums.CameraMode;
import eu.midnightdust.midnightcontrols.client.enums.ControllerType; import eu.midnightdust.midnightcontrols.client.enums.ControllerType;
import eu.midnightdust.midnightcontrols.client.enums.HudSide; import eu.midnightdust.midnightcontrols.client.enums.HudSide;
@@ -73,6 +74,7 @@ public class MidnightControlsConfig extends MidnightConfig {
@Entry(category = VISUAL, name = "Reacharound Outline Alpha", isSlider = true, min = 0, max = 255) public static int reacharoundOutlineColorAlpha = 102; @Entry(category = VISUAL, name = "Reacharound Outline Alpha", isSlider = true, min = 0, max = 255) public static int reacharoundOutlineColorAlpha = 102;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.right_dead_zone", isSlider = true, min = 0.05, max = 1) public static double rightDeadZone = 0.25; @Entry(category = CONTROLLER, name = "midnightcontrols.menu.right_dead_zone", isSlider = true, min = 0.05, max = 1) public static double rightDeadZone = 0.25;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.left_dead_zone", isSlider = true, min = 0.05, max = 1) public static double leftDeadZone = 0.25; @Entry(category = CONTROLLER, name = "midnightcontrols.menu.left_dead_zone", isSlider = true, min = 0.05, max = 1) public static double leftDeadZone = 0.25;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.trigger_dead_zone", isSlider = true, min = 0.05, max = 1) public static double triggerDeadZone = 0.1;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.invert_right_y_axis") public static boolean invertRightYAxis = false; @Entry(category = CONTROLLER, name = "midnightcontrols.menu.invert_right_y_axis") public static boolean invertRightYAxis = false;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.invert_right_x_axis") public static boolean invertRightXAxis = false; @Entry(category = CONTROLLER, name = "midnightcontrols.menu.invert_right_x_axis") public static boolean invertRightXAxis = false;
@Entry(category = CONTROLLER, name = "midnightcontrols.menu.rotation_speed", isSlider = true, min = 0, max = 100, precision = 10) public static double rotationSpeed = 35.0; //used for x-axis, name kept for compatibility @Entry(category = CONTROLLER, name = "midnightcontrols.menu.rotation_speed", isSlider = true, min = 0, max = 100, precision = 10) public static double rotationSpeed = 35.0; //used for x-axis, name kept for compatibility
@@ -311,28 +313,28 @@ public class MidnightControlsConfig extends MidnightConfig {
else BINDING.put("controller.controls." + binding.getName(), binding.getButtonCode()); else BINDING.put("controller.controls." + binding.getName(), binding.getButtonCode());
} }
public static boolean isBackButton(int btn, boolean isBtn, int state) { public static boolean isBackButton(int btn, boolean isBtn, ButtonState state) {
if (!isBtn && state == 0) if (!isBtn && state == ButtonState.NONE)
return false; return false;
return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_Y, false) == ButtonBinding.axisAsButton(btn, state == 1); return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_Y, false) == ButtonBinding.axisAsButton(btn, state == ButtonState.PRESS);
} }
public static boolean isForwardButton(int btn, boolean isBtn, int state) { public static boolean isForwardButton(int btn, boolean isBtn, ButtonState state) {
if (!isBtn && state == 0) if (!isBtn && state == ButtonState.NONE)
return false; return false;
return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_Y, true) == ButtonBinding.axisAsButton(btn, state == 1); return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_Y, true) == ButtonBinding.axisAsButton(btn, state == ButtonState.PRESS);
} }
public static boolean isLeftButton(int btn, boolean isBtn, int state) { public static boolean isLeftButton(int btn, boolean isBtn, ButtonState state) {
if (!isBtn && state == 0) if (!isBtn && state == ButtonState.NONE)
return false; return false;
return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_X, false) == ButtonBinding.axisAsButton(btn, state == 1); return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_X, false) == ButtonBinding.axisAsButton(btn, state == ButtonState.PRESS);
} }
public static boolean isRightButton(int btn, boolean isBtn, int state) { public static boolean isRightButton(int btn, boolean isBtn, ButtonState state) {
if (!isBtn && state == 0) if (!isBtn && state == ButtonState.NONE)
return false; return false;
return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_X, true) == ButtonBinding.axisAsButton(btn, state == 1); return ButtonBinding.axisAsButton(GLFW_GAMEPAD_AXIS_LEFT_X, true) == ButtonBinding.axisAsButton(btn, state == ButtonState.PRESS);
} }
/** /**

View File

@@ -109,14 +109,14 @@ public class MidnightInput {
// Handles the key bindings. // Handles the key bindings.
if (MidnightControlsClient.BINDING_LOOK_UP.isPressed()) { if (MidnightControlsClient.BINDING_LOOK_UP.isPressed()) {
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 2)); this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, -0.8F, 0d));
} else if (MidnightControlsClient.BINDING_LOOK_DOWN.isPressed()) { } else if (MidnightControlsClient.BINDING_LOOK_DOWN.isPressed()) {
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 1)); this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 0d));
} }
if (MidnightControlsClient.BINDING_LOOK_LEFT.isPressed()) { if (MidnightControlsClient.BINDING_LOOK_LEFT.isPressed()) {
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 2)); this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, -0.8F, 0d));
} else if (MidnightControlsClient.BINDING_LOOK_RIGHT.isPressed()) { } else if (MidnightControlsClient.BINDING_LOOK_RIGHT.isPressed()) {
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 1)); this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 0d));
} }
InputManager.INPUT_MANAGER.tick(); InputManager.INPUT_MANAGER.tick();
@@ -256,20 +256,20 @@ public class MidnightInput {
var buffer = gamepadState.buttons(); var buffer = gamepadState.buttons();
for (int i = 0; i < buffer.limit(); i++) { for (int i = 0; i < buffer.limit(); i++) {
int btn = leftJoycon ? ButtonBinding.controller2Button(i) : i; int btn = leftJoycon ? ButtonBinding.controller2Button(i) : i;
boolean btnState = buffer.get() == (byte) 1; boolean pressed = buffer.get() == (byte) 1;
var state = ButtonState.NONE; var state = ButtonState.NONE;
var previousState = InputManager.STATES.getOrDefault(btn, ButtonState.NONE); var previousState = InputManager.STATES.getOrDefault(btn, ButtonState.NONE);
if (btnState != previousState.isPressed()) { if (pressed != previousState.isPressed()) {
state = btnState ? ButtonState.PRESS : ButtonState.RELEASE; state = pressed ? ButtonState.PRESS : ButtonState.RELEASE;
this.handleButton(new ButtonStorage(btn, btnState ? 0 : 1, btnState)); this.handleButton(new ButtonStorage(btn, state));
if (btnState) if (pressed)
BUTTON_COOLDOWNS.put(btn, 5); BUTTON_COOLDOWNS.put(btn, 5);
} else if (btnState) { } else if (pressed) {
state = ButtonState.REPEAT; state = ButtonState.REPEAT;
if (BUTTON_COOLDOWNS.getOrDefault(btn, 0) == 0) { if (BUTTON_COOLDOWNS.getOrDefault(btn, 0) == 0) {
BUTTON_COOLDOWNS.put(btn, 5); BUTTON_COOLDOWNS.put(btn, 5);
this.handleButton(new ButtonStorage(btn, 2, true)); this.handleButton(new ButtonStorage(btn, state));
} }
} }
@@ -305,13 +305,11 @@ public class MidnightInput {
case GLFW_GAMEPAD_AXIS_RIGHT_X -> value = rightX; case GLFW_GAMEPAD_AXIS_RIGHT_X -> value = rightX;
case GLFW_GAMEPAD_AXIS_RIGHT_Y -> value = rightY; case GLFW_GAMEPAD_AXIS_RIGHT_Y -> value = rightY;
} }
float absValue = Math.abs(value);
if (i == GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y) if (i == GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y)
value *= -1.0F; value *= -1.0F;
int state = value > MidnightControlsConfig.rightDeadZone ? 1 : (value < -MidnightControlsConfig.rightDeadZone ? 2 : 0); this.handleJoystickAxis(new AxisStorage(axis, value));
this.handleJoystickAxis(new AxisStorage(axis, value, absValue, state));
} }
} }
else { else {
@@ -326,16 +324,14 @@ public class MidnightInput {
for (int i = GLFW_GAMEPAD_AXIS_LEFT_TRIGGER; i <= GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER; i++) { for (int i = GLFW_GAMEPAD_AXIS_LEFT_TRIGGER; i <= GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER; i++) {
int axis = leftJoycon ? ButtonBinding.controller2Button(i) : i; int axis = leftJoycon ? ButtonBinding.controller2Button(i) : i;
float value = buffer.get(i); float value = buffer.get(i);
float absValue = Math.abs(value);
int state = value > MidnightControlsConfig.rightDeadZone ? 1 : (value < -MidnightControlsConfig.rightDeadZone ? 2 : 0); this.handleTriggerAxis(new AxisStorage(axis, value, MidnightControlsConfig.triggerDeadZone));
this.handleTriggerAxis(new AxisStorage(axis, value, absValue, state));
} }
} }
public void handleButton(ButtonStorage storage) { public void handleButton(ButtonStorage storage) {
if (this.controlsInput != null && this.controlsInput.focusedBinding != null) { if (this.controlsInput != null && this.controlsInput.focusedBinding != null) {
if (storage.action == 0 && !this.controlsInput.currentButtons.contains(storage.button)) { if (storage.state == ButtonState.PRESS && !this.controlsInput.currentButtons.contains(storage.button)) {
this.controlsInput.currentButtons.add(storage.button); this.controlsInput.currentButtons.add(storage.button);
var buttons = new int[this.controlsInput.currentButtons.size()]; var buttons = new int[this.controlsInput.currentButtons.size()];
@@ -348,12 +344,12 @@ public class MidnightInput {
return; return;
} }
if (client.currentScreen != null && (storage.action != 1) && storage.button == GLFW_GAMEPAD_BUTTON_Y && if (client.currentScreen != null && storage.state.isPressed() && storage.button == GLFW_GAMEPAD_BUTTON_Y &&
MidnightControlsConfig.arrowScreens.contains(client.currentScreen.getClass().getCanonicalName())) { MidnightControlsConfig.arrowScreens.contains(client.currentScreen.getClass().getCanonicalName())) {
pressKeyboardKey(client, GLFW.GLFW_KEY_ENTER); pressKeyboardKey(client, GLFW.GLFW_KEY_ENTER);
this.screenCloseCooldown = 5; this.screenCloseCooldown = 5;
} }
else if (storage.action != 1) { else if (storage.state.isPressed()) {
if (client.currentScreen != null && storage.isDpad() && this.actionGuiCooldown == 0) { if (client.currentScreen != null && storage.isDpad() && this.actionGuiCooldown == 0) {
switch (storage.button) { switch (storage.button) {
case GLFW_GAMEPAD_BUTTON_DPAD_UP -> this.changeFocus(client.currentScreen, NavigationDirection.UP); case GLFW_GAMEPAD_BUTTON_DPAD_UP -> this.changeFocus(client.currentScreen, NavigationDirection.UP);
@@ -395,14 +391,14 @@ public class MidnightInput {
if (!this.ignoreNextARelease && client.currentScreen != null) { if (!this.ignoreNextARelease && client.currentScreen != null) {
var accessor = (MouseAccessor) client.mouse; var accessor = (MouseAccessor) client.mouse;
accessor.midnightcontrols$onCursorPos(client.getWindow().getHandle(), client.mouse.getX(), client.mouse.getY()); accessor.midnightcontrols$onCursorPos(client.getWindow().getHandle(), client.mouse.getX(), client.mouse.getY());
switch (storage.action) { switch (storage.state) {
// Button pressed // Button pressed
case 0 -> accessor.midnightcontrols$onMouseButton(client.getWindow().getHandle(), GLFW_MOUSE_BUTTON_LEFT, 1, 0); case PRESS -> accessor.midnightcontrols$onMouseButton(client.getWindow().getHandle(), GLFW_MOUSE_BUTTON_LEFT, 1, 0);
case 1 -> { // Button released case RELEASE -> { // Button released
accessor.midnightcontrols$onMouseButton(client.getWindow().getHandle(), GLFW_MOUSE_BUTTON_LEFT, 0, 0); accessor.midnightcontrols$onMouseButton(client.getWindow().getHandle(), GLFW_MOUSE_BUTTON_LEFT, 0, 0);
client.currentScreen.setDragging(false); client.currentScreen.setDragging(false);
} }
case 2 -> client.currentScreen.setDragging(true); // Button held down / dragging case REPEAT -> client.currentScreen.setDragging(true); // Button held down / dragging
} }
this.screenCloseCooldown = 5; this.screenCloseCooldown = 5;
} else { } else {
@@ -416,8 +412,8 @@ public class MidnightInput {
if (client.currentScreen instanceof HandledScreen<?> handledScreen && ((HandledScreenAccessor) handledScreen).midnightcontrols$getSlotAt( if (client.currentScreen instanceof HandledScreen<?> handledScreen && ((HandledScreenAccessor) handledScreen).midnightcontrols$getSlotAt(
mouseX, mouseY) != null) return; mouseX, mouseY) != null) return;
if (!this.ignoreNextXRelease && client.currentScreen != null) { if (!this.ignoreNextXRelease && client.currentScreen != null) {
if (storage.action == 0) client.currentScreen.mouseClicked(mouseX, mouseY, GLFW.GLFW_MOUSE_BUTTON_2); if (storage.state == ButtonState.PRESS) client.currentScreen.mouseClicked(mouseX, mouseY, GLFW.GLFW_MOUSE_BUTTON_2);
else if (storage.action == 1) client.currentScreen.mouseReleased(mouseX, mouseY, GLFW.GLFW_MOUSE_BUTTON_2); else if (storage.state == ButtonState.RELEASE) client.currentScreen.mouseReleased(mouseX, mouseY, GLFW.GLFW_MOUSE_BUTTON_2);
this.screenCloseCooldown = 5; this.screenCloseCooldown = 5;
} else { } else {
this.ignoreNextXRelease = false; this.ignoreNextXRelease = false;
@@ -443,13 +439,13 @@ public class MidnightInput {
boolean allowMouseControl = true; boolean allowMouseControl = true;
if (this.actionGuiCooldown == 0 && MidnightControlsConfig.isMovementAxis(storage.axis) && isScreenInteractive(client.currentScreen)) { if (this.actionGuiCooldown == 0 && MidnightControlsConfig.isMovementAxis(storage.axis) && isScreenInteractive(client.currentScreen)) {
if (MidnightControlsConfig.isForwardButton(storage.axis, false, storage.asButtonState)) { if (MidnightControlsConfig.isForwardButton(storage.axis, false, storage.buttonState)) {
allowMouseControl = this.changeFocus(client.currentScreen, NavigationDirection.UP); allowMouseControl = this.changeFocus(client.currentScreen, NavigationDirection.UP);
} else if (MidnightControlsConfig.isBackButton(storage.axis, false, storage.asButtonState)) { } else if (MidnightControlsConfig.isBackButton(storage.axis, false, storage.buttonState)) {
allowMouseControl = this.changeFocus(client.currentScreen, NavigationDirection.DOWN); allowMouseControl = this.changeFocus(client.currentScreen, NavigationDirection.DOWN);
} else if (MidnightControlsConfig.isLeftButton(storage.axis, false, storage.asButtonState)) { } else if (MidnightControlsConfig.isLeftButton(storage.axis, false, storage.buttonState)) {
allowMouseControl = this.handleLeftRight(client.currentScreen, false); allowMouseControl = this.handleLeftRight(client.currentScreen, false);
} else if (MidnightControlsConfig.isRightButton(storage.axis, false, storage.asButtonState)) { } else if (MidnightControlsConfig.isRightButton(storage.axis, false, storage.buttonState)) {
allowMouseControl = this.handleLeftRight(client.currentScreen, true); allowMouseControl = this.handleLeftRight(client.currentScreen, true);
} }
} }
@@ -457,13 +453,13 @@ public class MidnightInput {
float movementX = 0.f; float movementX = 0.f;
float movementY = 0.f; float movementY = 0.f;
if (MidnightControlsConfig.isBackButton(storage.axis, false, (storage.value > 0 ? 1 : 2))) { if (MidnightControlsConfig.isBackButton(storage.axis, false, (storage.value > 0 ? ButtonState.PRESS : ButtonState.RELEASE))) {
movementY = storage.absValue; movementY = storage.absValue;
} else if (MidnightControlsConfig.isForwardButton(storage.axis, false, (storage.value > 0 ? 1 : 2))) { } else if (MidnightControlsConfig.isForwardButton(storage.axis, false, (storage.value > 0 ? ButtonState.PRESS : ButtonState.RELEASE))) {
movementY = -storage.absValue; movementY = -storage.absValue;
} else if (MidnightControlsConfig.isLeftButton(storage.axis, false, (storage.value > 0 ? 1 : 2))) { } else if (MidnightControlsConfig.isLeftButton(storage.axis, false, (storage.value > 0 ? ButtonState.PRESS : ButtonState.RELEASE))) {
movementX = -storage.absValue; movementX = -storage.absValue;
} else if (MidnightControlsConfig.isRightButton(storage.axis, false, (storage.value > 0 ? 1 : 2))) { } else if (MidnightControlsConfig.isRightButton(storage.axis, false, (storage.value > 0 ? ButtonState.PRESS : ButtonState.RELEASE))) {
movementX = storage.absValue; movementX = storage.absValue;
} }
@@ -514,15 +510,13 @@ public class MidnightInput {
} }
private boolean handleScreenScrolling(Screen screen, AxisStorage storage) { private boolean handleScreenScrolling(Screen screen, AxisStorage storage) {
if (storage.axis > GLFW_GAMEPAD_AXIS_RIGHT_Y) return false;
// @TODO allow rebinding to left stick // @TODO allow rebinding to left stick
int preferredAxis = true ? GLFW_GAMEPAD_AXIS_RIGHT_Y : GLFW_GAMEPAD_AXIS_LEFT_Y; int preferredAxis = true ? GLFW_GAMEPAD_AXIS_RIGHT_Y : GLFW_GAMEPAD_AXIS_LEFT_Y;
if (this.controlsInput != null && this.controlsInput.focusedBinding != null) { if (this.controlsInput != null && this.controlsInput.focusedBinding != null) {
if (storage.asButtonState != 0 && !this.controlsInput.currentButtons.contains(ButtonBinding.axisAsButton(storage.axis, storage.asButtonState == 1))) { if (storage.buttonState != ButtonState.NONE && !this.controlsInput.currentButtons.contains(ButtonBinding.axisAsButton(storage.axis, storage.buttonState == ButtonState.PRESS))) {
this.controlsInput.currentButtons.add(ButtonBinding.axisAsButton(storage.axis, storage.asButtonState == 1)); this.controlsInput.currentButtons.add(ButtonBinding.axisAsButton(storage.axis, storage.buttonState == ButtonState.PRESS));
int[] buttons = new int[this.controlsInput.currentButtons.size()]; int[] buttons = new int[this.controlsInput.currentButtons.size()];
for (int i = 0; i < this.controlsInput.currentButtons.size(); i++) for (int i = 0; i < this.controlsInput.currentButtons.size(); i++)
@@ -702,7 +696,7 @@ public class MidnightInput {
private double prevX = 0; private double prevX = 0;
private double prevY = 0; private double prevY = 0;
private double xValue; private double xValue;
private int xState; private AxisStorage.Polarity xPolarity;
/** /**
* Handles the look direction input. * Handles the look direction input.
@@ -717,8 +711,8 @@ public class MidnightInput {
} }
private void handleFlatLook(AxisStorage storage) { private void handleFlatLook(AxisStorage storage) {
if (storage.state != 0) { if (storage.polarity != AxisStorage.Polarity.ZERO) {
double rotation = Math.pow(storage.value, 2.0) * 0.11D * (storage.state == 2 ? -1 : 1); double rotation = Math.pow(storage.value, 2.0) * 0.11D * storage.polarity.multiplier;
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation * MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed / 2; if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation * MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed / 2;
else this.targetYaw = rotation * MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed / 2; else this.targetYaw = rotation * MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed / 2;
@@ -727,13 +721,13 @@ public class MidnightInput {
private void handleAdaptiveLook(AxisStorage storage) { private void handleAdaptiveLook(AxisStorage storage) {
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_X) { if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_X) {
xValue = storage.value; xValue = storage.value;
xState = storage.state; xPolarity = storage.polarity;
} }
else { else {
double yStep = (MidnightControlsConfig.yAxisRotationSpeed / 50) * 0.6000000238418579 + 0.20000000298023224; double yStep = (MidnightControlsConfig.yAxisRotationSpeed / 50) * 0.6000000238418579 + 0.20000000298023224;
double xStep = (MidnightControlsConfig.rotationSpeed / 50) * 0.6000000238418579 + 0.20000000298023224; double xStep = (MidnightControlsConfig.rotationSpeed / 50) * 0.6000000238418579 + 0.20000000298023224;
float yValue = storage.value; float yValue = storage.value;
float yState = storage.state; AxisStorage.Polarity yPolarity = storage.polarity;
double cursorDeltaX = 2 * xValue - this.prevX; double cursorDeltaX = 2 * xValue - this.prevX;
double cursorDeltaY = 2 * yValue - this.prevY; double cursorDeltaY = 2 * yValue - this.prevY;
@@ -744,13 +738,13 @@ public class MidnightInput {
double powXValue = Math.pow(x, 2.0); double powXValue = Math.pow(x, 2.0);
double powYValue = Math.pow(y, 2.0); double powYValue = Math.pow(y, 2.0);
if (xState != 0) { if (xPolarity != AxisStorage.Polarity.ZERO) {
double sign = MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed; double sign = MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed;
this.targetYaw = sign * powXValue * 0.11D * (xState == 2 ? -1 : 1); this.targetYaw = sign * powXValue * 0.11D * xPolarity.multiplier;
} }
if (yState != 0) { if (yPolarity != AxisStorage.Polarity.ZERO) {
double sign = MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed; double sign = MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed;
this.targetPitch = sign * powYValue * 0.11D * (yState == 2 ? -1 : 1); this.targetPitch = sign * powYValue * 0.11D * yPolarity.multiplier;
} }
this.prevY = yValue; this.prevY = yValue;
@@ -758,7 +752,7 @@ public class MidnightInput {
} }
} }
public void handleTouchscreenLook(AxisStorage storage) { public void handleTouchscreenLook(AxisStorage storage) {
if (storage.state != 0) { if (storage.polarity != AxisStorage.Polarity.ZERO) {
double rotation = storage.value * 0.11D * MidnightControlsConfig.touchSpeed/5; double rotation = storage.value * 0.11D * MidnightControlsConfig.touchSpeed/5;
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation; if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation;

View File

@@ -352,8 +352,8 @@ public class TouchscreenOverlay extends Screen {
deltaX = -deltaX; deltaX = -deltaX;
deltaY = -deltaY; deltaY = -deltaY;
} }
input.handleTouchscreenLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, (float) deltaY, deltaY > 0.01 ? 2 : 1)); input.handleTouchscreenLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, (float) deltaY, 0.25d));
input.handleTouchscreenLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, (float) deltaX, deltaX > 0.01 ? 2 : 1)); input.handleTouchscreenLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, (float) deltaX, 0.25d));
} }
else TouchInput.isDragging = true; else TouchInput.isDragging = true;
} }

View File

@@ -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; import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER;
public class AxisStorage { public class AxisStorage {
public int axis, state, asButtonState; public final int axis;
public float value, absValue; public float value, absValue;
public double deadZone; public final double deadZone;
public Polarity polarity; public final Polarity polarity;
public boolean isTrigger; public final boolean isTrigger;
public final ButtonState buttonState;
// Only used for camera handling // Used for joysticks
public AxisStorage(int axis, float value, int state) { public AxisStorage(int axis, float value) {
this.axis = axis; this(axis, value, isLeftAxis(axis) ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone);
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;
} }
public AxisStorage(int axis, float value, float absValue, int state) { public AxisStorage(int axis, float value, double deadZone) {
this.axis = axis; this.axis = axis;
this.value = value; this.deadZone = deadZone;
this.absValue = absValue;
this.state = state;
this.deadZone = isLeftAxis() ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone;
this.asButtonState = value > .5f ? 1 : (value < -.5f ? 2 : 0);
if (axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER || axis == GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER 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)) { || axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER) || axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
this.isTrigger = true; this.isTrigger = true;
if (asButtonState == 2) { if (value < -.5f) {
this.asButtonState = 0; value = 0f;
} }
else { else {
// Fixes Triggers not working correctly on some controllers // Fixes Triggers not working correctly on some controllers
if (MidnightControlsConfig.triggerFix) { if (MidnightControlsConfig.triggerFix) {
this.value = 1.0f; value = 1.0f;
this.absValue = 1.0f;
this.state = 1;
this.asButtonState = 1;
}
} }
} }
} 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 currentPlusState = value > deadZone;
boolean currentMinusState = value < -deadZone; boolean currentMinusState = value < -deadZone;
if (isTrigger) currentMinusState = false; if (isTrigger) currentMinusState = false;
else if (!MidnightControlsConfig.analogMovement && isLeftAxis()) { else if (!MidnightControlsConfig.analogMovement && isLeftAxis(axis)) {
currentPlusState = asButtonState == 1; currentPlusState = buttonState == ButtonState.PRESS;
currentMinusState = asButtonState == 2; currentMinusState = buttonState == ButtonState.RELEASE;
} }
this.polarity = currentPlusState ? AxisStorage.Polarity.PLUS : currentMinusState ? AxisStorage.Polarity.MINUS : AxisStorage.Polarity.ZERO; 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; return axis == GLFW_GAMEPAD_AXIS_LEFT_X || axis == GLFW_GAMEPAD_AXIS_LEFT_Y || axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER;
} }
public boolean isRightAxis() { public static boolean isRightAxis(int axis) {
return !isLeftAxis(); return !isLeftAxis(axis);
} }
public enum Polarity { 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() { public boolean isPositive() {
return this == PLUS; return this == PLUS;
} }

View File

@@ -1,15 +1,16 @@
package eu.midnightdust.midnightcontrols.client.util.storage; 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_LEFT;
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_BUTTON_DPAD_UP; import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_BUTTON_DPAD_UP;
public class ButtonStorage { public class ButtonStorage {
public final int button, action; public final int button;
public final boolean state; public final ButtonState state;
public ButtonStorage(int button, int action, boolean state) { public ButtonStorage(int button, ButtonState state) {
this.button = button; this.button = button;
this.action = action;
this.state = state; this.state = state;
} }
public boolean isDpad() { public boolean isDpad() {