Improve look keybinds

- Now work regardless of the controller camera mode
- Can now be found in MidnightControls category instead of Minecraft vanilla's
-
This commit is contained in:
Martin Prokoph
2024-07-21 19:25:01 +02:00
parent cd416cf022
commit 310af56162
5 changed files with 56 additions and 73 deletions

View File

@@ -49,22 +49,22 @@ import java.util.concurrent.atomic.AtomicReference;
/**
* Represents the midnightcontrols client mod.
*
* @author LambdAurora
* @version 1.7.0
* @author Motschen, LambdAurora
* @version 1.10.0
* @since 1.1.0
*/
public class MidnightControlsClient extends MidnightControls {
public static boolean lateInitDone = false;
public static final KeyBinding BINDING_LOOK_UP = InputManager.makeKeyBinding(id("look_up"),
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_8, "key.categories.movement");
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_8, "key.categories.midnightcontrols");
public static final KeyBinding BINDING_LOOK_RIGHT = InputManager.makeKeyBinding(id("look_right"),
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_6, "key.categories.movement");
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_6, "key.categories.midnightcontrols");
public static final KeyBinding BINDING_LOOK_DOWN = InputManager.makeKeyBinding(id("look_down"),
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_2, "key.categories.movement");
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_2, "key.categories.midnightcontrols");
public static final KeyBinding BINDING_LOOK_LEFT = InputManager.makeKeyBinding(id("look_left"),
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_4, "key.categories.movement");
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_4, "key.categories.midnightcontrols");
public static final KeyBinding BINDING_RING = InputManager.makeKeyBinding(id("ring"),
InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.getCode(), "key.categories.misc");
InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.getCode(), "key.categories.midnightcontrols");
public static final Identifier CONTROLLER_BUTTONS = id("textures/gui/controller_buttons.png");
public static final Identifier CONTROLLER_EXPANDED = id("textures/gui/controller_expanded.png");
public static final Identifier CONTROLLER_AXIS = id("textures/gui/controller_axis.png");
@@ -143,11 +143,11 @@ public class MidnightControlsClient extends MidnightControls {
if (!keyBinding.getTranslationKey().contains(MidnightControlsConstants.NAMESPACE)) {
AtomicReference<ButtonCategory> category = new AtomicReference<>();
InputManager.streamCategories().forEach(buttonCategory -> {
if (buttonCategory.getIdentifier().equals(Identifier.of("minecraft", keyBinding.getCategory())))
if (buttonCategory.getIdentifier().equals(Identifier.ofVanilla(keyBinding.getCategory())))
category.set(buttonCategory);
});
if (category.get() == null) {
category.set(new ButtonCategory(Identifier.of("minecraft", keyBinding.getCategory())));
category.set(new ButtonCategory(Identifier.ofVanilla(keyBinding.getCategory())));
InputManager.registerCategory(category.get());
}
ButtonBinding buttonBinding = new ButtonBinding.Builder(keyBinding.getTranslationKey()).category(category.get()).linkKeybind(keyBinding).register();

View File

@@ -103,14 +103,14 @@ public class MidnightInput {
// Handles the key bindings.
if (MidnightControlsClient.BINDING_LOOK_UP.isPressed()) {
this.handleLook(client, new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 2));
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 2));
} else if (MidnightControlsClient.BINDING_LOOK_DOWN.isPressed()) {
this.handleLook(client, new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 1));
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 1));
}
if (MidnightControlsClient.BINDING_LOOK_LEFT.isPressed()) {
this.handleLook(client, new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 2));
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 2));
} else if (MidnightControlsClient.BINDING_LOOK_RIGHT.isPressed()) {
this.handleLook(client, new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 1));
this.handleFlatLook(new AxisStorage(GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 1));
}
InputManager.INPUT_MANAGER.tick(client);
@@ -424,43 +424,13 @@ public class MidnightInput {
}
}
}
public void pressKeyboardKey(MinecraftClient client, int key) {
client.keyboard.onKey(client.getWindow().getHandle(), key, 0, 1, 0);
}
public void pressKeyboardKey(Screen screen, int key) {
screen.keyPressed(key, 0, 1);
}
/**
/**
* Tries to go back.
*
* @param screen the current screen
* @return true if successful, else false
*/
public boolean tryGoBack(@NotNull Screen screen) {
var set = ImmutableSet.of("gui.back", "gui.done", "gui.cancel", "gui.toTitle", "gui.toMenu");
return screen.children().stream().filter(element -> element instanceof PressableWidget)
.map(element -> (PressableWidget) element)
.filter(element -> element.getMessage() != null && element.getMessage().getContent() != null)
.anyMatch(element -> {
if (element.getMessage().getContent() instanceof TranslatableTextContent translatableText) {
if (set.stream().anyMatch(key -> translatableText.getKey().equals(key))) {
element.onPress();
return true;
}
}
return false;
});
}
private void handleAxe(@NotNull MinecraftClient client, AxisStorage storage) {
setCurrentPolarities(storage);
this.setCurrentPolarities(storage);
handleMovement(client, storage);
this.handleMovement(client, storage);
if (handleScreenScrolling(client.currentScreen, storage)) return;
if (this.handleScreenScrolling(client.currentScreen, storage)) return;
storage.absValue = (float) MathHelper.clamp(storage.absValue / MidnightControlsConfig.getAxisMaxValue(storage.axis), 0.f, 1.f);
if (client.currentScreen == null) {
@@ -762,16 +732,15 @@ public class MidnightInput {
else handleAdaptiveLook(storage);
}
public void handleFlatLook(AxisStorage storage) {
private void handleFlatLook(AxisStorage storage) {
if (storage.state != 0) {
double powValue = Math.pow(storage.value, 2.0) * (storage.state == 2 ? -1 : 1);
double rotation = (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) ? MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed : MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed * powValue * 0.11D;
double rotation = Math.pow(storage.value, 2.0) * 0.11D * (storage.state == 2 ? -1 : 1);
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation;
else this.targetYaw = rotation;
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_Y) this.targetPitch = rotation * MidnightControlsConfig.getRightYAxisSign() * MidnightControlsConfig.yAxisRotationSpeed;
else this.targetYaw = rotation * MidnightControlsConfig.getRightXAxisSign() * MidnightControlsConfig.rotationSpeed;
}
}
public void handleAdaptiveLook(AxisStorage storage) {
private void handleAdaptiveLook(AxisStorage storage) {
if (storage.axis == GLFW_GAMEPAD_AXIS_RIGHT_X) {
xValue = storage.value;
xState = storage.state;
@@ -834,12 +803,42 @@ public class MidnightInput {
return false;
}
/**
* Tries to go back.
*
* @param screen the current screen
* @return true if successful, else false
*/
public boolean tryGoBack(@NotNull Screen screen) {
var set = ImmutableSet.of("gui.back", "gui.done", "gui.cancel", "gui.toTitle", "gui.toMenu");
return screen.children().stream().filter(element -> element instanceof PressableWidget)
.map(element -> (PressableWidget) element)
.filter(element -> element.getMessage() != null && element.getMessage().getContent() != null)
.anyMatch(element -> {
if (element.getMessage().getContent() instanceof TranslatableTextContent translatableText) {
if (set.stream().anyMatch(key -> translatableText.getKey().equals(key))) {
element.onPress();
return true;
}
}
return false;
});
}
public static boolean isScreenInteractive(@NotNull Screen screen) {
return !(screen instanceof HandledScreen || MidnightControlsConfig.joystickAsMouse || MidnightControlsConfig.mouseScreens.stream().anyMatch(a -> screen.getClass().toString().contains(a))
|| (screen instanceof SpruceScreen && ((SpruceScreen) screen).requiresCursor())
|| MidnightControlsCompat.requireMouseOnScreen(screen));
}
public void pressKeyboardKey(MinecraftClient client, int key) {
client.keyboard.onKey(client.getWindow().getHandle(), key, 0, 1, 0);
}
public void pressKeyboardKey(Screen screen, int key) {
screen.keyPressed(key, 0, 1);
}
// Inspired from https://github.com/MrCrayfish/Controllable/blob/1.14.X/src/main/java/com/mrcrayfish/controllable/client/ControllerInput.java#L686.
private void moveMouseToClosestSlot(@NotNull MinecraftClient client, @Nullable Screen screen) {
// Makes the mouse attracted to slots. This helps with selecting items when using a controller.

View File

@@ -392,20 +392,6 @@ public class InputManager {
return CATEGORIES.stream();
}
/**
* Returns a new key binding instance.
*
* @param id the identifier of the key binding
* @param type the type
* @param code the code
* @param category the category of the key binding
* @return the key binding
* @see #makeKeyBinding(org.aperlambda.lambdacommon.Identifier, InputUtil.Type, int, String)
*/
public static @NotNull KeyBinding makeKeyBinding(@NotNull org.aperlambda.lambdacommon.Identifier id, InputUtil.Type type, int code, @NotNull String category) {
return makeKeyBinding(Identifier.of(id.getNamespace(), id.getName()), type, code, category);
}
/**
* Returns a new key binding instance.
*

View File

@@ -2,15 +2,12 @@ package eu.midnightdust.midnightcontrols.client.mixin;
import net.minecraft.client.Mouse;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(Mouse.class)
public interface MouseAccessor {
@Invoker("onCursorPos")
void midnightcontrols$onCursorPos(long window, double x, double y);
@Accessor
void setLeftButtonClicked(boolean value);
@Invoker("onMouseButton")
void midnightcontrols$onMouseButton(long window, int button, int action, int mods);
}

View File

@@ -23,10 +23,11 @@
"midnightcontrols.midnightconfig.enum.TouchMode.FINGER_POS": "Finger Position",
"midnightcontrols.midnightconfig.enum.CameraMode.FLAT": "Flat",
"midnightcontrols.midnightconfig.enum.CameraMode.ADAPTIVE": "Adaptive",
"key.midnightcontrols.look_down": "Look down",
"key.midnightcontrols.look_left": "Look left",
"key.midnightcontrols.look_right": "Look right",
"key.midnightcontrols.look_up": "Look up",
"key.categories.midnightcontrols": "MidnightControls",
"key.midnightcontrols.look_down": "Look Down",
"key.midnightcontrols.look_left": "Look Left",
"key.midnightcontrols.look_right": "Look Right",
"key.midnightcontrols.look_up": "Look Up",
"key.midnightcontrols.ring": "Open Unbound Keybind Ring",
"midnightcontrols.action.attack": "Attack",
"midnightcontrols.action.back": "Back",