mirror of
https://github.com/TeamMidnightDust/MidnightControls.git
synced 2025-12-16 08:35:10 +01:00
Improve code further
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
package eu.midnightdust.midnightcontrols.client.util;
|
||||
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
||||
import net.minecraft.screen.slot.Slot;
|
||||
import org.aperlambda.lambdacommon.utils.Pair;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.client;
|
||||
import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.input;
|
||||
|
||||
public class InventoryUtil {
|
||||
// Finds the closest slot in the GUI within 14 pixels.
|
||||
@@ -60,4 +63,62 @@ public class InventoryUtil {
|
||||
.min(Comparator.comparingDouble(p -> p.value))
|
||||
.map(p -> p.key);
|
||||
}
|
||||
|
||||
private static int targetMouseX = 0;
|
||||
private static int targetMouseY = 0;
|
||||
|
||||
// Inspired from https://github.com/MrCrayfish/Controllable/blob/1.14.X/src/main/java/com/mrcrayfish/controllable/client/ControllerInput.java#L686.
|
||||
public static void moveMouseToClosestSlot(@Nullable Screen screen) {
|
||||
// Makes the mouse attracted to slots. This helps with selecting items when using a controller.
|
||||
if (screen instanceof HandledScreen<?> inventoryScreen) {
|
||||
var accessor = (HandledScreenAccessor) inventoryScreen;
|
||||
int guiLeft = accessor.getX();
|
||||
int guiTop = accessor.getY();
|
||||
int mouseX = (int) (targetMouseX * (double) client.getWindow().getScaledWidth() / (double) client.getWindow().getWidth());
|
||||
int mouseY = (int) (targetMouseY * (double) client.getWindow().getScaledHeight() / (double) client.getWindow().getHeight());
|
||||
|
||||
// Finds the closest slot in the GUI within 14 pixels.
|
||||
Optional<net.minecraft.util.Pair<Slot, Double>> closestSlot = inventoryScreen.getScreenHandler().slots.parallelStream()
|
||||
.map(slot -> {
|
||||
int x = guiLeft + slot.x + 8;
|
||||
int y = guiTop + slot.y + 8;
|
||||
|
||||
// Distance between the slot and the cursor.
|
||||
double distance = Math.sqrt(Math.pow(x - mouseX, 2) + Math.pow(y - mouseY, 2));
|
||||
return new net.minecraft.util.Pair<>(slot, distance);
|
||||
}).filter(entry -> entry.getRight() <= 14.0)
|
||||
.min(Comparator.comparingDouble(net.minecraft.util.Pair::getRight));
|
||||
|
||||
if (closestSlot.isPresent() && client.player != null) {
|
||||
var slot = closestSlot.get().getLeft();
|
||||
if (slot.hasStack() || !client.player.getInventory().getMainHandStack().isEmpty()) {
|
||||
int slotCenterXScaled = guiLeft + slot.x + 8;
|
||||
int slotCenterYScaled = guiTop + slot.y + 8;
|
||||
int slotCenterX = (int) (slotCenterXScaled / ((double) client.getWindow().getScaledWidth() / (double) client.getWindow().getWidth()));
|
||||
int slotCenterY = (int) (slotCenterYScaled / ((double) client.getWindow().getScaledHeight() / (double) client.getWindow().getHeight()));
|
||||
double deltaX = slotCenterX - targetMouseX;
|
||||
double deltaY = slotCenterY - targetMouseY;
|
||||
|
||||
if (mouseX != slotCenterXScaled || mouseY != slotCenterYScaled) {
|
||||
targetMouseX += (int) (deltaX * 0.75);
|
||||
targetMouseY += (int) (deltaY * 0.75);
|
||||
} else {
|
||||
input.mouseSpeedX *= 0.3F;
|
||||
input.mouseSpeedY *= 0.3F;
|
||||
}
|
||||
input.mouseSpeedX *= .75F;
|
||||
input.mouseSpeedY *= .75F;
|
||||
} else {
|
||||
input.mouseSpeedX *= .1F;
|
||||
input.mouseSpeedY *= .1F;
|
||||
}
|
||||
} else {
|
||||
input.mouseSpeedX *= .3F;
|
||||
input.mouseSpeedY *= .3F;
|
||||
}
|
||||
} else {
|
||||
input.mouseSpeedX = 0.F;
|
||||
input.mouseSpeedY = 0.F;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package eu.midnightdust.midnightcontrols.client.util.storage;
|
||||
|
||||
import eu.midnightdust.midnightcontrols.client.MidnightControlsConfig;
|
||||
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding;
|
||||
import eu.midnightdust.midnightcontrols.client.enums.ButtonState;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import static eu.midnightdust.midnightcontrols.client.MidnightInput.BUTTON_COOLDOWNS;
|
||||
import static eu.midnightdust.midnightcontrols.client.controller.InputManager.STATES;
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER;
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_X;
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y;
|
||||
@@ -14,12 +17,17 @@ public class AxisStorage {
|
||||
public float value, absValue;
|
||||
public double deadZone;
|
||||
public Polarity polarity;
|
||||
public boolean isTrigger;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
public AxisStorage(int axis, float value, float absValue, int state) {
|
||||
@@ -27,14 +35,14 @@ public class AxisStorage {
|
||||
this.value = value;
|
||||
this.absValue = absValue;
|
||||
this.state = state;
|
||||
deadZone = getDeadZoneValue(axis);
|
||||
asButtonState = value > .5f ? 1 : (value < -.5f ? 2 : 0);
|
||||
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
|
||||
|| 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;
|
||||
if (asButtonState == 2) {
|
||||
asButtonState = 0;
|
||||
this.asButtonState = 0;
|
||||
}
|
||||
else {
|
||||
// Fixes Triggers not working correctly on some controllers
|
||||
@@ -44,14 +52,60 @@ public class AxisStorage {
|
||||
this.state = 1;
|
||||
this.asButtonState = 1;
|
||||
}
|
||||
//if (MidnightControlsConfig.debug) System.out.println(axis + " "+ value + " " + absValue + " " + state);
|
||||
}
|
||||
}
|
||||
boolean currentPlusState = value > deadZone;
|
||||
boolean currentMinusState = value < -deadZone;
|
||||
if (isTrigger) currentMinusState = false;
|
||||
else if (!MidnightControlsConfig.analogMovement && isLeftAxis()) {
|
||||
currentPlusState = asButtonState == 1;
|
||||
currentMinusState = asButtonState == 2;
|
||||
}
|
||||
this.polarity = currentPlusState ? AxisStorage.Polarity.PLUS : currentMinusState ? AxisStorage.Polarity.MINUS : AxisStorage.Polarity.ZERO;
|
||||
}
|
||||
|
||||
public void setupButtonStates() {
|
||||
var posButton = ButtonBinding.axisAsButton(axis, true);
|
||||
var negButton = ButtonBinding.axisAsButton(axis, false);
|
||||
var previousPlusState = STATES.getOrDefault(posButton, ButtonState.NONE);
|
||||
var previousMinusState = STATES.getOrDefault(negButton, ButtonState.NONE);
|
||||
|
||||
if (polarity.isPositive() != previousPlusState.isPressed()) {
|
||||
STATES.put(posButton, polarity.isPositive() ? ButtonState.PRESS : ButtonState.RELEASE);
|
||||
if (polarity.isPositive())
|
||||
BUTTON_COOLDOWNS.put(posButton, 5);
|
||||
} else if (polarity.isPositive()) {
|
||||
STATES.put(posButton, ButtonState.REPEAT);
|
||||
if (BUTTON_COOLDOWNS.getOrDefault(posButton, 0) == 0) {
|
||||
BUTTON_COOLDOWNS.put(posButton, 5);
|
||||
}
|
||||
}
|
||||
|
||||
if (polarity.isNegative() != previousMinusState.isPressed()) {
|
||||
STATES.put(negButton, polarity.isNegative() ? ButtonState.PRESS : ButtonState.RELEASE);
|
||||
if (polarity.isNegative())
|
||||
BUTTON_COOLDOWNS.put(negButton, 5);
|
||||
} else if (polarity.isNegative()) {
|
||||
STATES.put(negButton, ButtonState.REPEAT);
|
||||
if (BUTTON_COOLDOWNS.getOrDefault(negButton, 0) == 0) {
|
||||
BUTTON_COOLDOWNS.put(negButton, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static double getDeadZoneValue(int axis) {
|
||||
return (axis == GLFW_GAMEPAD_AXIS_LEFT_X || axis == GLFW_GAMEPAD_AXIS_LEFT_Y) ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone;
|
||||
public boolean isLeftAxis() {
|
||||
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 enum Polarity {
|
||||
MINUS, ZERO, PLUS;
|
||||
public boolean isPositive() {
|
||||
return this == PLUS;
|
||||
}
|
||||
public boolean isNegative() {
|
||||
return this == MINUS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package eu.midnightdust.midnightcontrols.client.util.storage;
|
||||
|
||||
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 ButtonStorage(int button, int action, boolean state) {
|
||||
this.button = button;
|
||||
this.action = action;
|
||||
this.state = state;
|
||||
}
|
||||
public boolean isDpad() {
|
||||
return button >= GLFW_GAMEPAD_BUTTON_DPAD_UP && button <= GLFW_GAMEPAD_BUTTON_DPAD_LEFT;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user