Skip to content

Commit cefbcd4

Browse files
committed
clean: even more code improvements
1 parent 1f3993f commit cefbcd4

5 files changed

Lines changed: 46 additions & 33 deletions

File tree

src/main/java/eu/midnightdust/midnightcontrols/client/MidnightControlsClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static void onMcInit(@NotNull Minecraft client) {
125125
/**
126126
* Shows a toast popup to notify the user about an event.
127127
*/
128-
private static void showToastMessage(Component title, Component description) {
128+
public static void showToastMessage(Component title, Component description) {
129129
client.getToastManager().addToast(new SystemToast(SystemToast.SystemToastId.PERIODIC_NOTIFICATION, title, description));
130130
}
131131

@@ -198,7 +198,7 @@ public static void onTick(@NotNull Minecraft client) {
198198
Component.translatable("midnightcontrols.menu.title.controller")
199199
));
200200
MidnightControlsConfig.enableHints = false;
201-
MidnightControlsConfig.save();
201+
//MidnightControlsConfig.save();
202202
}
203203
TouchInput.tick();
204204
}
@@ -228,7 +228,7 @@ public static void onScreenOpen(Screen screen) {
228228
client.noRender = false;
229229
client.screen = screen;
230230
} else if (screen != null) {
231-
MidnightControlsClient.input.onScreenOpen(client.getWindow().getScreenWidth(), client.getWindow().getScreenHeight());
231+
input.onScreenOpen(client.getWindow().getScreenWidth(), client.getWindow().getScreenHeight());
232232
}
233233
}
234234

src/main/java/eu/midnightdust/midnightcontrols/client/controller/Controller.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import eu.midnightdust.midnightcontrols.MidnightControls;
1313
import eu.midnightdust.midnightcontrols.client.MidnightControlsClient;
1414
import eu.midnightdust.midnightcontrols.client.MidnightControlsConfig;
15-
import org.aperlambda.lambdacommon.utils.Nameable;
1615
import org.jetbrains.annotations.NotNull;
1716
import org.lwjgl.glfw.GLFW;
1817
import org.lwjgl.glfw.GLFWGamepadState;
@@ -29,11 +28,10 @@
2928
import java.util.Map;
3029
import java.util.Optional;
3130
import java.util.concurrent.CompletableFuture;
32-
import net.minecraft.client.Minecraft;
33-
import net.minecraft.client.gui.components.toasts.SystemToast;
3431
import net.minecraft.client.resources.language.I18n;
3532
import net.minecraft.network.chat.Component;
3633

34+
import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.showToastMessage;
3735
import static org.lwjgl.BufferUtils.createByteBuffer;
3836

3937
/**
@@ -146,6 +144,7 @@ private static ByteBuffer ioResourceToBuffer(String resource) throws IOException
146144
public static void updateMappings() {
147145
CompletableFuture.supplyAsync(Controller::updateMappingsSync);
148146
}
147+
149148
private static boolean updateMappingsSync() {
150149
try {
151150
MidnightControls.log("Updating controller mappings...");
@@ -168,11 +167,7 @@ private static boolean updateMappingsSync() {
168167
if (i != 0) {
169168
long l = pointerBuffer.get();
170169
var string = l == 0L ? "" : MemoryUtil.memUTF8(l);
171-
var client = Minecraft.getInstance();
172-
if (client != null) {
173-
client.getToastManager().addToast(SystemToast.multiline(client, SystemToast.SystemToastId.PERIODIC_NOTIFICATION,
174-
Component.translatable("midnightcontrols.controller.mappings.error"), Component.literal(string)));
175-
}
170+
showToastMessage(Component.translatable("midnightcontrols.controller.mappings.error"), Component.literal(string));
176171
MidnightControls.log(I18n.get("midnightcontrols.controller.mappings.error")+string);
177172
}
178173
} catch (Throwable e) {

src/main/java/eu/midnightdust/midnightcontrols/client/controller/InputManager.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import eu.midnightdust.midnightcontrols.client.mixin.MouseAccessor;
1616
import it.unimi.dsi.fastutil.ints.*;
1717
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
18-
import org.aperlambda.lambdacommon.utils.function.PairPredicate;
1918
import org.jetbrains.annotations.NotNull;
2019
import org.lwjgl.glfw.GLFW;
2120

@@ -55,15 +54,20 @@ protected InputManager() {
5554
}
5655

5756
public void tick() {
58-
if (MidnightControlsConfig.autoSwitchMode && !MidnightControlsConfig.isEditing && MidnightControlsConfig.controlsMode != ControlsMode.TOUCHSCREEN)
59-
if (MidnightControlsConfig.getController().isConnected() && MidnightControlsConfig.getController().isGamepad())
60-
MidnightControlsConfig.controlsMode = ControlsMode.CONTROLLER;
61-
else MidnightControlsConfig.controlsMode = ControlsMode.DEFAULT;
57+
handleAutoSwitch();
6258
if (MidnightControlsConfig.controlsMode == ControlsMode.CONTROLLER) {
6359
this.controllerTick();
6460
}
6561
}
6662

63+
private static void handleAutoSwitch() {
64+
if (MidnightControlsConfig.autoSwitchMode && !MidnightControlsConfig.isEditing && MidnightControlsConfig.controlsMode != ControlsMode.TOUCHSCREEN) {
65+
if (MidnightControlsConfig.getController().isConnected() && MidnightControlsConfig.getController().isGamepad())
66+
MidnightControlsConfig.controlsMode = ControlsMode.CONTROLLER;
67+
else MidnightControlsConfig.controlsMode = ControlsMode.DEFAULT;
68+
}
69+
}
70+
6771
public void controllerTick() {
6872
this.prevTargetMouseX = this.targetMouseX;
6973
this.prevTargetMouseY = this.targetMouseY;
@@ -136,12 +140,13 @@ public static boolean hasBinding(@NotNull String name) {
136140
public static boolean hasBinding(@NotNull Identifier identifier) {
137141
return hasBinding(identifier.getNamespace() + "." + identifier.getPath());
138142
}
143+
139144
private static ButtonBinding tempBinding;
140145
/**
141146
* Returns the binding matching the given string.
142147
*
143148
* @param name the name of the binding to get
144-
* @return true if the binding is registered, else false
149+
* @return the found binding
145150
*/
146151
public static ButtonBinding getBinding(@NotNull String name) {
147152
synchronized (BINDINGS) {
@@ -152,6 +157,7 @@ public static ButtonBinding getBinding(@NotNull String name) {
152157
}
153158
return tempBinding;
154159
}
160+
155161
private static List<ButtonBinding> unboundBindings;
156162
public static List<ButtonBinding> getUnboundBindings() {
157163
unboundBindings = new ArrayList<>();

src/main/java/eu/midnightdust/midnightcontrols/client/util/InventoryUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package eu.midnightdust.midnightcontrols.client.util;
22

3+
import net.minecraft.util.Tuple;
34
import org.aperlambda.lambdacommon.utils.Pair;
5+
import org.jetbrains.annotations.NotNull;
46
import org.jetbrains.annotations.Nullable;
57

68
import java.util.Comparator;
@@ -78,14 +80,14 @@ public static void moveMouseToClosestSlot(@Nullable Screen screen) {
7880
int mouseY = (int) (targetMouseY * (double) client.getWindow().getGuiScaledHeight() / (double) client.getWindow().getScreenHeight());
7981

8082
// Finds the closest slot in the GUI within 14 pixels.
81-
Optional<net.minecraft.util.Tuple<Slot, Double>> closestSlot = inventoryScreen.getMenu().slots.parallelStream()
83+
Optional<Tuple<@NotNull Slot, @NotNull Double>> closestSlot = inventoryScreen.getMenu().slots.parallelStream()
8284
.map(slot -> {
8385
int x = guiLeft + slot.x + 8;
8486
int y = guiTop + slot.y + 8;
8587

8688
// Distance between the slot and the cursor.
8789
double distance = Math.sqrt(Math.pow(x - mouseX, 2) + Math.pow(y - mouseY, 2));
88-
return new net.minecraft.util.Tuple<>(slot, distance);
90+
return new Tuple<>(slot, distance);
8991
}).filter(entry -> entry.getB() <= 14.0)
9092
.min(Comparator.comparingDouble(net.minecraft.util.Tuple::getB));
9193

src/main/java/eu/midnightdust/midnightcontrols/client/util/storage/AxisStorage.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@
1212
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y;
1313
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER;
1414

15+
/**
16+
* Stores information about the current axis state
17+
*/
1518
public class AxisStorage {
1619
public final int axis;
1720
public float value, absValue;
1821
public final double deadZone;
1922
public final Polarity polarity;
20-
public final boolean isTrigger;
2123
public final ButtonState buttonState;
2224

23-
// Used for joysticks
25+
/**
26+
* Create an AxisStorage instance with a configured deadzone (for left/right sticks)
27+
*/
2428
public static AxisStorage of(int axis, float value) {
2529
return new AxisStorage(axis, value, isLeftAxis(axis) ? MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone);
2630
}
31+
/**
32+
* Create an AxisStorage instance with an arbitrary deadzone (for triggers and internal use)
33+
*/
2734
public static AxisStorage of(int axis, float value, double deadZone) {
2835
return new AxisStorage(axis, value, deadZone);
2936
}
@@ -32,32 +39,28 @@ private AxisStorage(int axis, float value, double deadZone) {
3239
this.axis = axis;
3340
this.deadZone = deadZone;
3441

35-
if (axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER || axis == GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER
36-
|| axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER) || axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
37-
this.isTrigger = true;
42+
if (isTrigger(axis)) {
3843
if (value < -.5f) {
3944
value = 0f;
4045
}
41-
else {
42-
// Fixes Triggers not working correctly on some controllers
43-
if (MidnightControlsConfig.triggerFix) {
44-
value = 1.0f;
45-
}
46+
else if (MidnightControlsConfig.triggerFix) { // Fixes Triggers not working correctly on some controllers
47+
value = 1.0f;
4648
}
47-
} else isTrigger = false;
49+
}
4850

4951
this.value = value;
5052
this.buttonState = value > .5f ? ButtonState.PRESS : (value < -.5f ? ButtonState.RELEASE : ButtonState.NONE);
5153
this.absValue = Math.abs(value);
5254
boolean currentPlusState = value > deadZone;
5355
boolean currentMinusState = value < -deadZone;
54-
if (isTrigger) currentMinusState = false;
56+
if (isTrigger(axis)) currentMinusState = false;
5557
else if (!MidnightControlsConfig.isAnalogMovementAllowed() && isLeftAxis(axis)) {
5658
currentPlusState = buttonState == ButtonState.PRESS;
5759
currentMinusState = buttonState == ButtonState.RELEASE;
5860
}
5961
this.polarity = currentPlusState ? AxisStorage.Polarity.PLUS : currentMinusState ? AxisStorage.Polarity.MINUS : AxisStorage.Polarity.ZERO;
6062
}
63+
6164
/**
6265
* Returns the specified axis as a button.
6366
*
@@ -68,6 +71,10 @@ public int getButtonId(boolean positive) {
6871
return ButtonBinding.axisAsButton(axis, positive);
6972
}
7073

74+
/**
75+
* For button bindings making use of joysticks, this method updates the button values according to the axis value.
76+
* Each axis has two according buttons, plus (down or right) and minus (up or left).
77+
*/
7178
public void setupButtonStates() {
7279
var posButton = getButtonId(true);
7380
var negButton = getButtonId(false);
@@ -96,11 +103,14 @@ public void setupButtonStates() {
96103
}
97104
}
98105
}
106+
99107
public static boolean isLeftAxis(int axis) {
100108
return axis == GLFW_GAMEPAD_AXIS_LEFT_X || axis == GLFW_GAMEPAD_AXIS_LEFT_Y || axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER;
101109
}
102-
public static boolean isRightAxis(int axis) {
103-
return !isLeftAxis(axis);
110+
111+
public static boolean isTrigger(int axis) {
112+
return axis == GLFW_GAMEPAD_AXIS_LEFT_TRIGGER || axis == GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER ||
113+
axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER) || axis == ButtonBinding.controller2Button(GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER);
104114
}
105115

106116
public enum Polarity {

0 commit comments

Comments
 (0)